3种定义方式:
function a(x){ //方式1 return xx; } var b=function(x){ //方式2 return xx; } var c=new Function('x','return xx;'); //方式3
要比较不同点当然要首先来看一下他们的构造函数有什么区别
//都是function类型 console.log(typeof a); //function console.log(typeof b); //function console.log(typeof c); //function //都是Function对象的一个实例 console.log(a instanceof Function);//true console.log(b instanceof Function);//true console.log(c instanceof Function);//true //也都是Object对象的一个实例 //(因为在他们的构造函数中他们的__proto__都指向的是Object) console.log(a instanceof Object);//true console.log(b instanceof Object);//true console.log(c instanceof Object);//true //相同的构造函数 console.log(a.constructor);//function Function() { [native code] } console.log(b.constructor);//function Function() { [native code] } console.log(c.constructor);//function Function() { [native code] } console.log(a.constructor==b.constructor);//true console.log(a.constructor==c.constructor);//true console.log(b.constructor==c.constructor);//true //不同的prototype,相同的__proto__ console.log(a.prototype);//a console.log(a.__proto__);//function Empty() {} console.log(b.prototype);//Object console.log(b.__proto__);//function Empty() {} console.log(c.prototype);//anonymous console.log(c.__proto__);//function Empty() {} console.log(a.prototype==b.prototype);//false console.log(a.prototype==c.prototype);//false console.log(b.prototype==c.prototype);//false console.log(a.__proto__==b.__proto__);//true console.log(a.__proto__==c.__proto__);//true console.log(b.__proto__==c.__proto__);//true console.log(a.prototype.__proto__==b.prototype.__proto__);//true console.log(a.prototype.__proto__==c.prototype.__proto__);//true console.log(b.prototype.__proto__==c.prototype.__proto__);//true //他们的__proto__都是指向Function的原形 console.log(a.__proto__===Function.prototype);//true console.log(b.__proto__===Function.prototype);//true console.log(c.__proto__===Function.prototype);//true //他们的原形的__proto__都是指向Object的原形 console.log(a.prototype.__proto__===Object.prototype);//true console.log(b.prototype.__proto__===Object.prototype);//true console.log(c.prototype.__proto__===Object.prototype);//true console.log(b.prototype==Object);//false b的原形并不等于Object(虽然构造函数中的写的是Object,我想可能是由于匿名函数没有名字,所以就用了Object代替
console.log(typeof b.prototype);//object
console.log(Function.prototype.__proto__===Object.prototype);//true 说明 Function 是从 Object 继承过来的