var fun = function(){}
fun.prototype = {
name : 'peter',
age : 25
}
var a = new fun();
var b = new fun();
console.log(a.name, b.name);//peter peter
fun.prototype.name = 'jack';
console.log(a.name, b.name);//jack jack
fun.prototype = {};
fun.prototype.name = 'tom';
console.log(a.name, b.name);//jack jack
b.constructor.prototype.name = 'kitty';
console.log(a.name, b.name);//jack jack
//输出些什么鬼?
对
fun.prototype = {};
fun.prototype.name = 'tom';
操作之后的输出很不理解,fun的prototype已经改变了,访问a的name属性,查找原型链不应该输出tom?
更新:
还有一个问题,由于fun.prototype = {}操作覆盖了原型,所以之后对fun.prototype的属性修改其实修改的是{}.于是把fun.prototype = {}操作去掉,代码如下:
var fun = function(){}
fun.prototype = {
name : 'peter',
age : 25
}
var a = new fun();
var b = new fun();
console.log(a.name, b.name);//peter peter
fun.prototype.name = 'jack';
console.log(a.name, b.name);//jack jack
fun.prototype.name = 'tom';
console.log(a.name, b.name);//tom tom
b.constructor.prototype.name = 'kitty';
console.log(a.name, b.name);//tom tom
//输出些什么鬼?
b.constructor.prototype.name = 'kitty'这一步问什么没有生效呢
例一:
例二:
区别在于图1多了一句
func.prototype = {}
。