gpt4 book ai didi

Javascript : Modifying Prototype doesn't affect existing Instances

转载 作者:数据小太阳 更新时间:2023-10-29 04:41:42 26 4
gpt4 key购买 nike

<分区>

我创建了原型(prototype)的 2 个实例,更改了原型(prototype)中的函数,更改反射(reflect)在两个实例中(很棒)。但是,当我通过删除该函数修改原型(prototype)时,该函数对于现有实例仍然存在。

function A() {
this.name = "cool";
}

A.prototype = {
howCool: function() {
return this.name + "er";
}
};

var a1 = new A(),
a2 = new A();

a1.name = "hot";
//line1
console.log(a1.howCool());
//line2
console.log(a2.howCool());

A.prototype = {};

//line3
console.log(a1.howCool());

//line4
var a3 = new A();
console.log(a3.howCool());

第 1 行和第 2 行按预期工作,将原型(prototype)设置回空后,第 4 行显示预期的未定义。然而,第 3 行仍然显示函数定义。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com