第一种写法var stone={ weight:100}// function stone() {// this.weight=100;// }function Bench() { this.legth=100; this.width=50; this.weight=60;}Bench.prototype=stone;var b=new Bench();console.log(b);console.log(Object.getPrototypeOf(b));结果:第二种写法:function stone() { this.weight=100;
}function Bench() { this.legth=100; this.width=50; this.weight=60;
}
Bench.prototype=new stone;var b=new Bench();console.log(b);console.log(Object.getPrototypeOf(b));为什么会这样,第二种前面会带stone??
1 回答

倚天杖
TA贡献1828条经验 获得超3个赞
我在chrome运行的结果和你的不一样……
第一种的运行结果
Bench {legth: 100, width: 50, weight: 60}Object {weight: 100}
第二种运行结果
Bench {legth: 100, width: 50, weight: 60}stone {weight: 100}
这之中的区别很简单啊,console.log
对象的时候,前面那串就是它构造函数的名字,第一种代码里面,stone
对象使用对象直接量创建的,构造函数当然是Object
;然而在第二种代码里面,stone
成了个构造函数,打印它的实例,前面显示的当然是它这个构造函数的名字。
添加回答
举报
0/150
提交
取消