JavaScript中的调用函数与带括号或不带括号的区别() [英] what is the difference between calling function in JavaScript with or without parentheses ()

查看:180
本文介绍了JavaScript中的调用函数与带括号或不带括号的区别()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以一个简单的例子就是

  function a(){
alert(something);
}

anything.onclick = a; //这是没有括号

anything.onclick = a(); //这是带括号的

两者有什么区别?



还有一件事:如果我定义了相同的函数,但是这次返回false,它会起作用吗?

  function a(){
alert(something);
返回false;


解决方案

区别在于 a()调用该函数,而 a 是该函数。

  console.log(a()); // false 
console.log(a);为了清楚说明在使用这些技术时会发生什么样的技术问题你的例子的第二部分,让我们重新定义 a 像这样:

  a = function(){
return 100;
};

并设置事件处理程序:

  anything.onclick = a(); 

f()不仅调用函数 f 但返回其返回值。因此,当为函数调用设置变量或对象属性时,函数调用的返回值将被分配。因此,上述声明有效地等同于:

  anything.onclick = 100; 

这是没有意义的,可能会导致错误。如果一个函数没有返回值,它的返回值隐含地是 undefined



然而,如果你在没有调用它的情况下设置了一个变量等于 ,这与为该变量设置常规函数表达式相同:

  var a = function(){...},
b = a; // b = function(){...}

b code>将执行与 a 相同的操作。

所以在你的示例中,首先使用第一个一个是因为它是有道理的!唯一的情况下,您将函数调用的返回值分配给事件处理程序是否该函数返回另一个函数。例如:

  var x = function(xyz){
return function(){
console。日志(XYZ);
};
};

anything.onclick = x(Hello World); // = function(){
// console.log(Hello World);
//}


so a simple example would be

function a() {
    alert("something");
}

anything.onclick = a; // this is without parentheses

anything.onclick = a(); // this is with parentheses 

What is the difference between the two?

And one thing more: if I define the same function but this time return false, will it work?

function a(){
    alert("something");
    return false;
}

解决方案

The difference is that a() calls the function while a is the function.

console.log( a() ); // false
console.log(  a  ); // function() {...}

To make it clear what technically happens when you use the second part of your example, let's redefine alike this:

a = function() {
    return 100;
};

and set the event handler:

anything.onclick = a();

f() not only calls the function f but returns its return value. So when setting a variable or object property to a function call, the return value of the function call will be assigned. So the above statement is effectlively equivalent to:

anything.onclick = 100;

which doesn't make sense and might cause an error. If a function doesn't have a return value, its return value is implicitly undefined.

However, if you had set a variable equal to a without calling it, it would be the same as setting a regular function expression to that variable:

var a = function() { ... },
    b = a; // b = function() { ... }

b would perform the same operation as a would.

So in your example go with the first one because it makes sense! The only case in which you would assign the return value of the function call to an event handler is if the function returns another function. For instance:

var x = function(xyz) {
    return function() {
        console.log(xyz);
    };
};

anything.onclick = x("Hello World"); // = function() {
                                     //       console.log("Hello World");
                                     //   }

这篇关于JavaScript中的调用函数与带括号或不带括号的区别()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆