gpt4 book ai didi

javascript - 如何从构造函数中删除函数?

转载 作者:数据小太阳 更新时间:2023-10-29 06:03:05 27 4
gpt4 key购买 nike

如何从构造函数中删除一个函数?

如果 Person 构造函数中有一个名为 greet 的函数,我该如何删除该函数?

function Person(name)
{
this.name = name;
this.greet = function greet()
{
alert("Hello, " + this.name + ".");
};
}

我想要的结果是:

function Person(name)
{
this.name = name;
}

最佳答案

delete this.greet

var personInstance = new Person();
delete personInstance.greet // to remove it from the instance from the outside

delete Person.prototype.greet // if doing prototypes and inheritance

delete 是一个您很少见到的关键字,但我向您保证,它存在 :P

关于javascript - 如何从构造函数中删除函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590674/

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