-1
var getarticles = function(subject){
      console.log("found your articles about: " + subject);   
}


var rl = rl || {};
rl.utils = (function () {
    var callApadter = function(adapter){
        setTimeout(function() {
            console.log("inside the first timeout");
            adapter
        }, 4000);

    };
    return{
        callApadter: callApadter
    }})();


rl.utils.callApadter(getarticles("JS"));

 setTimeout(function() {
            console.log("this is 4 seconds");
       }, 4000);

谁能告诉我为什么 getarticles 会立即被调用?

多谢你们。

小提琴http://jsfiddle.net/UkAeV/

4

3 回答 3

1

您正在执行getarticles并将返回值传递给rl.utils.callApadter.

但是,如果您只传递对它的引用,rl.utils.callApadter它将起作用:jsFiddle

var getarticles = function (subject) {
    alert("found your articles about: " + subject);
};


var rl = rl || {};
rl.utils = (function () {
    var callApadter = function (adapter) {
        var _this = this,
            _arguments = Array.prototype.slice.call(arguments, 1);
        setTimeout(function () {
            alert("inside the first timeout");
            adapter.apply(_this, _arguments);
        }, 4000);

    };
    return {
        callApadter: callApadter
    };
})();


rl.utils.callApadter(getarticles, "JS");

setTimeout(function () {
    alert("this is 3 seconds");
}, 3000);
于 2013-07-17T08:05:44.380 回答
0

它实际上是在第 20 行被调用,而不是被传递:

rl.utils.callApadter(getarticles("JS"));

我想你想做类似的事情

rl.utils.callApadter(getarticles, "JS");
于 2013-07-17T08:06:57.567 回答
0

尝试这个:

var getarticles = function(subject){
    return function(){
        console.log("found your articles about: " + subject); 
    }  
}
于 2013-07-17T08:07:11.783 回答