javascript语法:函数调用和使用括号

时间:2009-02-27 04:16:18

标签: javascript syntax

为什么这样做..

<script type="text/javascript">
<!-- 

function myAlert(){
    alert('magic!!!');
}


if(document.addEventListener){   
    myForm.addEventListener('submit',myAlert,false); 
}else{   
    myForm.attachEvent('onsubmit',myAlert); 
}
// -->
</script>

但不是这个????

<script type="text/javascript">
<!-- 

function myAlert(){
    alert('magic!!!');
}


if(document.addEventListener){   
    myForm.addEventListener('submit',myAlert(),false); 
}else{   
    myForm.attachEvent('onsubmit',myAlert()); 
}
// -->
</script>

区别在于调用myAlert函数时使用括号。

我得到的错误..

  

“htmlfile:类型不匹配。”通过VS2008进行编译时。

5 个答案:

答案 0 :(得分:30)

函数之后的()意味着执行函数本身并返回它的值。如果没有它,您只需拥有该函数,该函数可以作为回调传递。

var f1 = function() { return 1; }; // 'f1' holds the function itself, not the value '1'
var f2 = function() { return 1; }(); // 'f2' holds the value '1' because we're executing it with the parenthesis after the function definition

var a = f1(); // we are now executing the function 'f1' which return value will be assigned to 'a'
var b = f2(); // we are executing 'f2' which is the value 1. We can only execute functions so this won't work

答案 1 :(得分:5)

addEventListener函数需要一个函数或一个实现EventListener的对象作为第二个参数,而不是函数调用。

()添加到函数名称时,它是函数调用而不是函数本身。

编辑如其他回复和评论中所示,可以在Javascript中返回功能。

因此,对于有趣的事情,我们可以尝试以下方法。从原始myAlert开始,我们可以稍微更改一下,以返回不同的消息,具体取决于参数:

function myAlert(msg)
{
    return function()
    {
        alert("Message: " + msg);
    }
}

这里,注意函数实际返回一个函数。因此,为了调用该函数,将需要额外的()

我写了一些HTML和Javascript来使用上面的功能。 (请原谅我的不干净的HTML和Javascript,因为它不是我的域名):

<script type="text/javascript">

function myAlert(msg)
{
    return function()
    {
        alert("Message: " + msg);
    }
}

</script>

<html>
<body>

<form>
<input type="button" value="Button1" onclick="myAlert('Clicked Button1')()">
<input type="button" value="Button2" onclick="myAlert('Clicked Button2')()">
</form>

</body>
</html>

显示两个按钮,每个按钮将使用不同的参数调用myAlert函数。调用myAlert函数后,它本身将返回另一个function,因此必须使用一组额外的括号调用它。

最终结果是,点击Button1会显示一条消息框,其中包含Message: Clicked Button1消息,点击Button2会显示一个消息框,显示Message: Clicked Button2

答案 2 :(得分:2)

当您使用括号时,您实际上正在调用该函数,并且您正在发送函数结果(在这种情况下为undefined,因为myAlert没有返回值)作为参数。

答案 3 :(得分:0)

值得注意的是,函数是一流的对象。你可以像任何其他物体一样折腾它们。这是一个非常简洁的例子,说明为什么这很酷,来自Wikipedia

function makeDerivative( f, deltaX ) {
    var deriv = function(x) { 
       return ( f(x + deltaX) - f(x) )/ deltaX;
    }

    return deriv;
}

var cos = makeDerivative( Math.sin, 0.000001);

答案 4 :(得分:0)

if(document.addEventListener){   
    myForm.addEventListener('submit',myAlert(),false); 
}else{   
    myForm.attachEvent('onsubmit',myAlert()); 
}

在这里使用myAlert()给出函数的返回值,而不是函数本身。

考虑一下:

function bob() {
    return "hello world";
}

alert(bob());

警报将使用函数bob返回的值。

如果要在addEventListener中传递其他参数,请尝试以下方法:

if(document.addEventListener){   
    myForm.addEventListener('submit',function(event) {
        myAlert(event,myOtherParamater); 
    },false); 
}else{   
    myForm.attachEvent('onsubmit',function () {
        myAlert(window.event,myOtherParameter); 
    }); 
}

javascript使用第一类函数,因此可以作为参数传递给addEventListener和attachEvent方法所期望的函数。希望有所帮助。