最近面试遇到各种javascript变态的原型链问题,总结如下(注释的是执行结果):
测试环境:360快速浏览器,firefox
console.log("//函数表达式/"); var testProto = function(){ console.log("testProto.111.."); } console.log("-11111---"+(typeof testProto));//-11111---function console.log("-11111--aaa-"+(testProto.prototype));//-11111--aaa-[object Object] console.log("-11111--bbbb-"+(testProto.constructor));//-11111--bbbb-function Function() { [native code] } console.log("-11111--cccc-"+(testProto instanceof Function));//-11111--cccc-true var testProto = 1; console.log("-22222---"+(typeof testProto));//-22222---number console.log("-22222--aaa-"+(testProto.prototype));//-22222--aaa-undefined console.log("-22222--bbbb-"+(testProto.constructor));//-22222--bbbb-function Number() { [native code] } console.log("-22222--cccc-"+(testProto instanceof Object));//-22222--cccc-false console.log("//函数声明/"); function testProto(){ console.log("testProto..2222."); } console.log("-11111---"+(typeof testProto));//-11111---number console.log("-11111--aaa-"+(testProto.prototype));//-11111--aaa-undefined console.log("-11111--bbbb-"+(testProto.constructor));//-11111--bbbb-function Number() { [native code] } console.log("-11111--cccc-"+(testProto instanceof Function));//-11111--cccc-false console.log("//实例化/"+testProto); try{ var vtp = new testProto(); console.log("-11111---"+(typeof vtp)); console.log("-11111--aaa-"+(vtp.prototype)); console.log("-11111--bbbb-"+(vtp.constructor)); console.log("-11111--cccc-"+(vtp instanceof testProto)); }catch(e){ console.log("error::"+e.message);//error::number is not a function } var testProto = function(){ console.log("testProto.3333..");//testProto.3333.. } console.log("//覆盖变量/"+testProto); /** //覆盖变量/function (){ console.log("testProto.111.."); } */ try{ var vtp = new testProto(); console.log("-11111---"+(typeof vtp));//-11111---object console.log("-11111--aaa-"+(vtp.prototype));//-11111--aaa-undefined console.log("-11111--bbbb-"+(vtp.constructor)); /** -11111--bbbb-function (){ console.log("testProto.3333.."); } */ console.log("-11111--cccc-"+(vtp instanceof testProto));//-11111--cccc-true }catch(e){ console.log("error::"+e.message); } var testProto = 1; console.log("-22222---"+(typeof testProto));//-22222---number console.log("-22222--aaa-"+(testProto.prototype));//-22222--aaa-undefined console.log("-22222--bbbb-"+(testProto.constructor));//-22222--bbbb-function Number() { [native code] } console.log("-22222--cccc-"+(testProto instanceof Object));//-22222--cccc-false