2012-05-19 44 views
3

因此在之前的问题中,我被告知调用/执行/启动函数,如thisFunc;而不是thisFunc();什么时候在调用javascript函数时使用()

而且我发现有时候这种方法有效,有时却不行。

<script type='text/javascript'> 
var valgo = 0; 
var thing = ""; 
var lastPost = document.getElementById(<? echo $_SESSION['countything']; ?>); 
lastPost.style.opacity = valgo; 

function valgogoer(thing){ 
valgo += .05; 
if (lastPost.style.opacity < 1){ 
lastPost.style.opacity = valgo; 
} 
} 


setInterval(function(){valgogoer(<? echo $_SESSION['countything']; ?>)}, 50); 

// Somethings are leftover from when I was messing with it, like the parameter thing. 
</script> 

在这个代码(请告诉我,如果这太可怕了),因为我使用的setInterval调用带有参数的功能,我发现通过研究它必须调用它上面是这样的。

所以两个问题

  1. 我什么时候实际上应该在调用函数使用()?

  2. 在上面的代码中,我怎样才能让它在不透明度点击1后停止执行该函数。目前它被限制为1,但仍然被调用,而且我感觉它更好停止被调用的函数,而不是被调用,但没有做任何事情。

谢谢!

回答

4

当您要调用该函数时使用方括号。但是如果只想传递函数的内容,则不会。例子:

var a = function(){ 
    return "I'm a function"; 
} 
var b = a;//Equals to function(){return "I'm a function";} 
var c = a();//Equals to "I'm a function" 

在事件处理程序,你千万不要因为你不得不说为执行功能的内容,浏览器使用的支架。如果你把他们来说,浏览器会调用该函数的返回值,这可能会导致错误:

var a = function(){ 
    alert("Welcome to my site"); 
} 
window.onload = a();//Wrong, equals to undefined, since the a function doesn't return any value 
window.onload = a;//Correct, calls the function a when the event is fired 

当你调用一个函数作为参数的setInterval方法同样的事情发生了。这就是为什么括号内是如此重要

+0

哦~~我看。谢谢,所以如果没有括号,它就等于函数的写法......你为什么要这么做? – user1159454

+0

传递[callback](http://en.m.wikipedia.org/wiki/Callback)是最常见的原因。事实上,正如他的例子所示,你只需要为''onload''事件回调做准确的事情(只要浏览器触发''onload''事件,它也会执行存储在窗口中的函数。 onload'',如果有的话) –

0

您使用()当你想另一个函数来执行你的函数

function log(arg) { console.log(arg); } 

setTimeout(log, 1000) // Logs undefined after 1 second 


log("hi"); // logs the String hi 

功能是可重复使用的,所以你实际上可以自己使用它

function logUsingTheLogMethod(callback) { 
    if (typeof callback === "function") { 
     callback("This will log to the console!"); 
     callback(log === callback); // Logs true 
    } 
} 
logUsingTheLogMethod(log); 

这在JS中的常见模式,在方法中使用函数作为回调

假设你有一些函数做了数学运算,但你不想写w为他们所有的日志记录方法。

function add(a,b,fn) { 
    if (fn === log) { 
     fn(a + b); 
    } 
} 
function subtract(a,b,fn) { 
    if (fn === log) { 
     fn(a - b); 
    } 
} 

add(1, 2, log); // logs 3 
subtract(5, 4, log) // logs 1 

或修改功能,所以它可以确保它是一个函数,而不是日志功能,您可以与响应

function add(a,b,fn) { 
    if (typeof fn === "function") { 
       fn(a + b); 
    } 
} 

// answer is automatically passed in by the calling add method 
add(a, b, function (answer) { 
    // do ssomething with the answer 
    alert(answer); 
}); 
2

时要调用的函数可以使用thisFunc()做任何事情。如果要将函数的引用作为值,则使用thisFunc

当你的函数没有参数,你可以使用一个回调参考:

function thisFunc() { 
    // do something 
} 

window.setTimeout(thisFunc, 1000); 

当你的函数有一个参数,你需要用它在另一个函数的参数值来调用它:

function thisFunc(param1) { 
    // do something 
} 

window.setTimeout(function(){ thisFunc(42); }, 1000); 

当然,你可以在一个函数包装一个参数的功能太:

function thisFunc() { 
    // do something 
} 

window.setTimeout(function(){ thisFunc(); }, 1000); 

你不需要使用匿名函数来包装的功能,你可以使用一个命名函数,并获得参考到:

function thisFunc(param1) { 
    // do something 
} 

function callFunc() { 
    thisFunc(42); 
} 

window.setTimeout(callFunc, 1000); 
相关问题