function a(){
console.log('bbb');
}
(2)函数表达式:var a = function(){
console.log('aaa');
}
(3)声明式变体:
var a = function a(){
console.log('bbb');
}
完全等价于 function b(){}
var a = function(){
console.log('aaa');
}
function a(){
console.log('bbb');
}
a(); //结果:aaa
示例2:
(function() {
console.log(noSuchVariable);
})();
//报错,因为noSuchVariable变量根本没定义
(function() { console.log(declaredLater); //"undefined" var declaredLater = "Now it's defined!"; console.log(declaredLater); //"Now it's defined!" })(); //上面这段代码是正确的,没有任何问题。
示例3:
var a; // 声明一个变量,标识符为a
function a() {} // 声明一个函数,标示符也为a
alert(typeof a); // function
function a() {}
var a;
alert(typeof a); //function
function a() {}
var a = 1;
alert(typeof a); //number
var a = 1;
function a() {}
alert(typeof a); //number
总结:
typeof 2; //'number'
typeof '2'; //'string'
typeof true; //'boolean'
typeof []; //'object'
typeof null; //'object'
typeof undefined; //'undefined'
var a = function(){}; typeof a; //'function'
对于所有内置可执行对象,typeof一律返回“function”
typeof new Number(2); //'object'
typeof new String('2'); //'object'
typeof new Boolean(true); //'object'
(2)instanceof[对象 instanceof 类]判断一个对象是否为类的实例
[] instanceof Object; //true
2 instanceof Number; //false
new Number(2) instanceof Number; //true
示例:
var a = function(){};
var b = function(){};
a.prototype = new b();
new a() instanceof b; //true
new a() instanceof Object; //true
首先确定b.prototype,如果b.prototype在a对象的原型链中,返回true,否则返回false。
var b = {};
b.prototype.bbb='bbb'; //报错b.prototype为undefined
function b(){}
b.prototype.bbb='bbb';
console.log(b); //结果:function b(){}
console.log(b.prototype); //结果:b {bbb: "bbb"}
console.log(b.bbb); //结果:undefined
console.log(new b().bbb); //结果:bbb
(2)prototype是函数的内置属性(用于设置函数的原型),__proto__是对象的内置属性(用于设置对象的原型)。
function test(){}
test.prototype; //结果:test {}
new test().prototype; //结果:undefined,对象没有prototype
new test().__proto__; //结果:test {}
function a(){
this.v='vvv';
}
a.prototype; //a {}
new a.__proto__; //a {}
new a().__proto__ === a.prototype; //true 实例对象的__proto__,指向函数的原型
(3)每个JavasScript函数都自动拥有一个prototype属性。这个属性的值是一个对象,这个对象包含唯一一个不可枚举属性constructor。constructor属性的值是一个函数对象。
function a(){
this.v='vvv';
}
a.prototype.constructor === a; //true[物理地址一样]
a.prototype.constructor; //constructor属性指代这个类
a.prototype.constructor.__proto__ === a.__proto__ === Function.prototype; //true
Object.prototype.constructor.__proto__ === Object.__proto__ === Function.prototype; //true
PS:所有构造器、函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)
function a(){}
function b(){}
a.prototype = {aaa:'aaa'};
b.prototype = a.prototype;
var aa1 = new a();
var bb1 = new b();
//aa1.prototype.aaa='xxx'; //错误,对象没有原型
a.prototype.aaa = 'xxx';
console.log( aa1.aaa ); //xxx
console.log( bb1.aaa ); //xxx
补充:a===b; //false a.prototype === b.prototype; //true
function a(){}
function b(){ this.bbb='bbb'}
a.prototype = new b(); //相当于a.prototype = {bbb: "bbb"};
console.log(new a().bbb ); //结果:bbb
补充:a.prototype === b.prototype; //false
function a(){}
a.prototype.count = 0;
var a1 = new a();
a1.count++;
var a2 = new a();
a2.count++;
a.prototype.count; //0
PS:方法类似 a.prototype.method = function(){}
function a(){}
a.count = 0;
new a().count(); //undefined,只能使用a.count调用
PS:方法类似 a.method = function(){}
var scope = "global scope";
function checkScope(){
var scope = "local scope";
function f(){
return scope;
}
return f();
}
checkScope(); //local scope
示例:更为常用的闭包
function counter(){
var n=0;
return{
count:function(){
return ++n;
},
reset:function(){
n=0;
}
};
}
var c1 = new counter();
c1.count(); //1
var c2 = new counter();
c2.count(); //1
c1.reset(); //c1置为0,c2不受影响
c2.count(); //2
PS:实例对象互相不影响,单个实例对象内的方法,共享私有变量。
function a(){
console.log(this.x+this.y);
}
a.call({x:100,y:100}); //200
function C(){
this.m = function(){
console.log(this.x+this.y);
}
}
var c = new C();
c.m.call({x:100,y:100}); //200
call&apply第一个实参是要调用函数的母对象,会变为this的值。
function a(){
console.log(this); //a {}
function b(){
console.log(this); //window
}
b();
}
new a();
特别注意:函数内部函数this指向window