简体   繁体   English

函数以及何时使用括号/括号

[英]functions and when to use brackets/parenthesis

I'm new to javascript and having difficulty understanding why/when to use brackets/parenthesis. 我是javascript的新手,很难理解为什么/何时使用括号/括号。

If you have a button called clicker, You can write... 如果你有一个名为clicker的按钮,你可以写...

    document.getElementById("clicker").onclick=function(){
        alert("Hi")
    }

function has () but... 函数有()但......

       function myFunction(){
        alert("Hi")
    }
    document.getElementById("clicker").onclick=myFunction;

myFunction has no () or it won't work (Makes the alert appear when page loads instead of on click). myFunction没有()或者它不起作用(当页面加载而不是单击时出现警报)。

myFunction has no () myFunction没有()

Oh yes it does: 哦,是的,它确实:

function myFunction(){
//                 ^^ right here
    alert("Hi")
}

And you can easily verify that both examples are equivalent by substituting the variable with the function definition: 您可以通过使用函数定义替换变量来轻松验证这两个示例是否相同:

function myFunction(){
    alert("Hi")
}
document.getElementById("clicker").onclick = myFunction;

// substitute "myFunction"

document.getElementById("clicker").onclick = function myFunction(){
    alert("Hi")
};

// remove function name 

document.getElementById("clicker").onclick = function(){
    alert("Hi")
};

See, we didn't touch any of the () . 看,我们没有触及任何() We were able to convert the second example into the first one by simply substituting a variable with its value. 我们能够通过简单地用变量替换变量将第二个例子转换成第一个例子。


You seem to mix the parenthesis inside a function definition : 您似乎在函数定义中混合括号:

function foo() { ... }
            ^^

with the parenthesis used to call a function: 用于调用函数的括号:

foo();
   ^^

Those are are two different kind of parenthesis and in neither of your examples are you using the second ones (which is correct). 这些是两种不同的括号,在你的例子中你都没有使用第二种(这是正确的)。

Lets see what would happen if we did the above substitution with myFunction() : 让我们看看如果我们使用myFunction()进行上述替换将会发生什么:

function myFunction(){
    alert("Hi")
}
document.getElementById("clicker").onclick = myFunction();

// substitute "myFunction"

document.getElementById("clicker").onclick = function myFunction(){
    alert("Hi")
}();

// remove function name 

document.getElementById("clicker").onclick = function(){
    alert("Hi")
}();

Notice the dangling () in the last line? 注意最后一行中的悬空() This doesn't look like your first example. 这看起来不像是你的第一个例子。 So you can see that calling the function is not equivalent to defining it. 所以你可以看到调用函数并不等同于定义它。

myFunction()

When you use myFunction() in regular code, it is always a call to execute the function and the parentheses may enclose a set of inputs to the functions arguments. 当你在常规代码中使用myFunction()时,它始终是一个执行函数的调用,括号可以包含一组函数参数的输入。

function myFunction()

When declaring a function the parentheses denote a list of arguments being defined. 声明函数时,括号表示正在定义的参数列表。 If they are empty it means there are no arguments. 如果它们是空的,则意味着没有参数。

document.getElementById("clicker").onclick = myFunction;

When you are setting the onclick property of an element it expects a reference to a function. 设置元素的onclick属性时,它需要引用函数。 This allows it to use the reference to execute the function later when the actual onclick event is fired. 这允许它在稍后触发实际的onclick事件时使用引用来执行该函数。 If you include the parentheses it tries to execute the function immediately and then set onclick to the result returned by the function. 如果包括括号,它会尝试立即执行该函数,然后设置onclick到函数返回的结果。

<button id="clicker" onclick="myFunction()">Click Me!</button>

Functions in the onclick attribute of an HTML tag include the parentheses because the browser is basically wrapping your function call for you. HTML标记的onclick属性中的函数包括括号,因为浏览器基本上是为您包装函数调用。 It's roughly equivalent to the JavaScript below. 它大致相当于下面的JavaScript。

document.getElementById("clicker").onclick = function(){ myFunction() };

Where function() is a declaration for an anonymous function. 其中function()是匿名函数的声明。

Functions Eloquent JavaScript is a reference that may help you understand functions better. 函数Eloquent JavaScript是一个可以帮助您更好地理解函数的参考。

In JavaScript, functions are first-class citizens, which in short means that a function can behave like a regular variable. 在JavaScript中,函数是一等公民,简而言之,函数可以像常规变量一样运行。

Consider: 考虑:

var x = 5; // x holds a reference to 5
foo(x); // Send whatever is inside x to foo, so it may do something with it.

Similarly, we can do this: 同样,我们可以这样做:

// Again, x holds a reference to something
var x = function () {
    console.log("hello");
}
foo(x); // And again, we send that something into foo

In the second example, x and function () {...} are the same. 在第二个例子中, xfunction () {...}是相同的。 Writing one is the same as writing the other, and just like x in the first example "really is" the number 5, x in the second example "really is" the function. 写一个与写另一个相同,就像第一个例子中的x “真的”是数字5,第二个例子中的x “真的是”函数。

Now, when we want a function to run, we call it using a reference + parenthesis. 现在,当我们想要运行一个函数时,我们使用引用+括号来调用它。 Consider these examples, which all do the same: 考虑这些例子,它们都是一样的:

// foo is the reference to the function, just like x was. 
// and we call it with parenthesis:
function foo () 
{
    console.log("Hello");
}
foo(); // Logs "hello".

// reference to function (still foo) + parenthesis
var foo = function () 
{
    console.log("Hello");
}
foo(); // Logs "Hello"

// Now: A function is called by a reference to a function followed by parenthesis
// So, we can also do this rather nifty thing:
function foo () 
{
    console.log("Hello");
}()
// A reference to a function, followed by parenthesis! 

So the short answer, as others have pointed out is that parentheses are used when declaring a function and calling a function, not when passing around a reference. 所以简短的回答,正如其他人所指出的那样,在声明函数和调用函数时使用括号,而不是在传递引用时。

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

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