gpt4 book ai didi

javascript - JavaScript 中的方法对象

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

我编写了以下代码,但它没有显示任何警报。

var test = function(message) {
this.show = function() {
alert(message)
}
}
(new test("hiii")).show();
(new test("helooo")).show();

当更改为以下时...删除了括号 - (new test("hiii")).show();

它同时显示“hiii”和“helooo”警报。

注意:我没有对 - (new test("helooo")).show(); 进行任何更改

var test = function(message) {
this.show = function() {
alert(message)
}
}
new test("hiii").show(); // was(new test("hiii")).show();
(new test("helooo")).show();

谁能解释一下为什么吗?

最佳答案

奇怪的是,问题在于您在函数表达式后面遗漏了分号:

var test = function(message){
this.show = function() {
alert(message)
}
} // <-- missing semicolon

这意味着函数表达式后面的( ... ) 被视为函数调用的参数列表。

添加缺少的分号,第一段代码就可以工作了。

关于javascript - JavaScript 中的方法对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406797/

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