简体   繁体   English

Javascript立即函数调用

[英]Javascript immediate function invocation

I am experimenting with different javascript function calls, I tried running the code below. 我正在尝试不同的javascript函数调用,我尝试运行下面的代码。 Here is the fiddle link https://jsfiddle.net/7de5vfpj/ 这是小提琴链接https://jsfiddle.net/7de5vfpj/

var app = function () {
    var ans = {
        power: alert("power"),
        wow: alert("wow")
    }
    return ans;
}();

From the fiddle it alerts both "power" and "wow", when I was just expecting it to alert "wow". 从小提琴中,它同时警告“力量”和“哇”,而我只是希望它能提醒“哇”。 Why does it happen this way? 为什么会这样发生?

Original Fiddle: 原始小提琴:

var app = function () {
var ans = {
    power: alert("power"),
    wow: alert("wow")
    }
  return ans;
}();

app.power();

The alert methods are triggered because of the object literal syntax. 由于对象文字语法而触发了警报方法。 calling the ans.power(); 调用ans.power(); on the bottom of the fiddle is causing the following exception to be thrown. 小提琴底部的引发以下异常。

Uncaught TypeError: app.power is not a function

the alert method does not have any return value. alert方法没有任何返回值。 This is causing the power and wow properties to have undefined values. 这导致powerwow属性具有undefined值。

This is also causing the invocation at the bottom of the fiddle to throw the exception above. 这也导致小提琴底部的调用引发上面的异常。

You're assigning to alert to a variable and upon initialization, this will trigger the alert. 您将警报分配给变量,初始化后将触发警报。

var x = alert("aa"); //this will trigger alert on initialization.

Probably a better solution would be to make both power and wow functions. 可能更好的解决方案是同时具有powerwow功能。

var app = function () {
var ans = {
    power: function(){alert("power")},
    wow: function(){alert("wow")}
}
return ans;
}();

Hope this helps. 希望这可以帮助。

You're actually immediately invoking both alerts. 实际上,您实际上是在同时调用这两个警报。 To get the expected behavior, you need to pass function references to the methods, not already-invoked functions (your IIFE is fine, it's your method declarations that are the cause of your problem): 为了获得预期的行为,您需要将函数引用传递给方法,而不是已经调用的函数(您的IIFE很好,这是导致问题的方法声明):

var app = function () {
    var ans = {
        power: function() { alert("power") },
        wow: function() { alert("wow") }
    }
    return ans;
}();

See the updated jsfiddle . 请参阅更新的jsfiddle

You can also, alternatively, take advantage of the fact that Function.prototype.bind returns a function reference: 另外,您也可以利用Function.prototype.bind返回函数引用这一事实:

var app = function () {
    var ans = {
        power: alert.bind(this, 'power'),
        wow: alert.bind(this, 'wow')
    }
    return ans;
}();

app.power();

Here's that jsfiddle . 就是jsfiddle

In your original code, if you open up your console, you'll see Uncaught TypeError: app.power is not a function . 在原始代码中,如果打开控制台,则会看到Uncaught TypeError: app.power is not a function That's because window.alert returns undefined , and the window.alert functions were already invoked... therefore, they return undefined , and when you try to call app.power() , you're trying to invoke the return value of that method... and obviously undefined isn't a function, as the error very semantically states. 这是因为window.alert返回undefined ,并且window.alert函数已经被调用...因此,它们返回undefined ,并且当您尝试调用app.power() ,您试图调用该方法的返回值 。 ...而且显然undefined不是函数,因为该错误在语义上很明显。

I created something that might be able to do what you wanted. 我创建了一些可能可以做您想要的东西的东西。

function app() {
    this.power = function(){
        alert('power');
    }
    this.wow = function(){
        alert('wow');
    }
};

var app = new app;

app.power()
app.wow()

you can use app.power() to call the power method, or app.wow() for the wow method. 您可以使用app.power()调用power方法,或使用app.wow()作为wow方法。 to create new methods, just follow the format of using 创建新方法,只需遵循使用的格式

this.methodName = function(){
    //code you want to execute
}


//to call that method, outside of the function, 
//and after you set a variable to new app, call this

app.methodName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM