# 例题1

function foo() {
        bar.apply(null,arguments);
        //bar(arguments); 12345 apply把12345传到了bar里面
    }
    function bar() {
        console.log(arguments);
    }
    foo(1,2,3,4,5);
1
2
3
4
5
6
7
8

parseInt(3,8) 以后面的目标进制为基底转换为10进制 8进制的3转换为10进制 3

parseInt(3,0) 没有0进制 就会把0认为是10

# 进制:

123456789abcdef 十分位加个分位 1f十六进制 16 + 15 二进制 没有2 只有0和1 10二进制 2+0 以 10 进制为例: 1 = 1 10 = 10 100 = 10 ^ 2 1000 = 10 ^ 3 1000 = 10 ^ 4

2进制: 1 = 1 10 = 2 100 = 2 ^ 2 1000 = 2 ^ 3 10000 = 2 ^ 4

# 例题2

var f = (
        function f() {
            return "1";
        },
        function g() {
            return 2;
        }
    )();
    typeof f;
1
2
3
4
5
6
7
8
9

, (逗号操作符),如果1,2 则会打印2 var num = 1,2; 会报错 var num = (1,2); 打印2 如果要用逗号操作符,就拿括号括起来

# 例题3

var x = 1;
    if(function f() {}) {
        x+= typeof f;
    }
    console.log(x);     //"1undefined"
1
2
3
4
5

解析

function为true所以一定会执行

f成为了未定义 typeof里面写undefined可以输出"undefined"字符串1+"undefined" 所以等于"1undefined"

小tip

{ } == { } false,原始值比对的是值,对象比对的是引用。

undefined == null ,返回 true

undefined === null 返回 false

NaN == NaN 返回 false

# this

this

1.函数预编译过程 this-- >window

2.全局作用域里 this-- >window

3.call/apply可以改变函数运行时this指向

4.obj.func(); func()里面的this指向obj

# 函数预编译过程

function test(c) {
        //var this = Object.create(test.prototype);
            var a = 123;
            function b() { }
}
test(1);            //默认指向window
new test();     //如果new test() 会改变test的指向
1
2
3
4
5
6
7
AO {
        arguments : [1],
        this : window,
        c : 1,
        a : undefined,
        b : function () { }
    }
1
2
3
4
5
6
7
function test() {
        console.log(this);
    }
    test();  //输出window
1
2
3
4

# 全局作用域

控制台打印this-- >window

# 谁调用this指向谁

var obj = {
        a : function () {
            console.log(this.name)
        },
        name : 'abc'
    }
    obj.a();
//a函数里面的this指向的就是他的调用者也就是obj
//谁调用了这个方法,这个方法里面的this就是谁
1
2
3
4
5
6
7
8
9

# this 题

var name = "222";
var a = {
     name : "111",
     say : function () {
     console.log(this.name);
     }
}

var fun = a.say;  
fun()       //222
a.say()     //111 a调用a的 say 方法
//a.say代表的function的函数引用,a 在全局上
//所以此时fun是全局的,姑 fun() =a.say 222

var b = {
    name : "333",
    say : function (fun) {
        //this --- > b
        //console.log(this) --- > b
        fun();  //没有人调用fun()的话fun就是指全局222,有人调用它的this才指向谁
        //test(); window
    }
}
b.say(a.say);   //222    
//b调用say 此时this指向b 执行fun() fun在此时全局winodw的 所以是222
b.say = a.say; // 333 
//把a.say的函数写到b.say 打印this.name 就是333
b.say(); // 333
//function test() {console.log(this)}window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# arguments.callee

arguments 上面有两个属性,一个 arguments.callee,一个 arguments.length

arguments.callee 指向的是函数的引用,也就是它自己。

function test() {
    console.log(arguments.callee);
}
test();   //输出function test() {}
1
2
3
4

# 立即执行函数例子(callee)

var num = (function(n) {
    if(n == 1) {
        return 1;
    }
    //return n * 阶乘(n - 1);
    return n * arguments.callee(n - 1);
}(100))
1
2
3
4
5
6
7

因为阶乘= n * n阶乘(n-1)的阶乘,现在n阶乘不知道,要往里面function里面写n阶乘,因为function是立即执行函数没有名字,所以要用arguments.callee来代替方可完成。所以把阶乘换成arguments.callee

# caller

caller 是函数自己身上的属性,就像 prototype 一样是自有的。

function test() {
        demo();
    }
function demo() {
        console.log(demo.caller);  //test(){}
    }
test();
//test执行,谁调用的,谁执行的demo() demo.caller就是谁
1
2
3
4
5
6
7
8

# this 题

var foo = 123;
function print() {
        this.foo = 234;
        console.log(foo);
  }
print();  //234
//console.log(foo)  foo没有定义,会去全局上找
//var foo 是全局的 this也是全局的 相当于var foo被this.foo改变了
new print();    //123 没定义去全局找,123.
1
2
3
4
5
6
7
8
9
var a = 5;
function test() {
    a = 0;
    alert(a);  //0      //0
    // var this = {
    //      this 里面没 a
    //     __proto__ : test.prototype
    // }
    alert(this.a); //5      //undefined
    var a;
    alert(a);       //0         //0
}
test();
new test();

AO  {
    a : undefined  -- > //执行第一步a = 0,
    this : window   
}
    
new test()
AO {
    a : 0,
    this : {}   空对象   //结果是打印0  undefined  0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var bar = {a:"002"};  //{a : 'a'}
    function print() {
        bar.a = 'a';
        Object.prototype.b = 'b';
        return function inner() {
            console.log(bar.a);     //a
            console.log(bar.b);     //原型链上的b
        }
    }
    print()();  //第一个括号返回的是一个函数,第二个括号是函数执行
1
2
3
4
5
6
7
8
9
10