gpt4 book ai didi

javascript - "new Dog"或 "new Dog()"

转载 作者:行者123 更新时间:2023-11-28 08:28:40 25 4
gpt4 key购买 nike

使用或不使用括号“实例化”JavaScript“类”之间有什么区别?

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

Animal.prototype.sayName = function() {
console.log(this.name);
};

function Dog(name) {
Animal.call(this, name);
}

Dog.prototype = new Animal(null);
Dog.prototype.bark = function() {
console.log("Woof!");
};

var d = new Dog

var d = new Dog()

最佳答案

这个问题已经被问过here

它们之间没有区别,只要构造函数不带参数即可。也就是说:调用 new Dog(不带括号)就像调用不带参数的 new Dog() 一样。

关于javascript - "new Dog"或 "new Dog()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22115880/

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