Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从回拨中指定呼叫者_Javascript - Fatal编程技术网

Javascript 如何从回拨中指定呼叫者

Javascript 如何从回拨中指定呼叫者,javascript,Javascript,是否可以将函数的“this”或调用者设置为回调 this.CallServer({ url: 'myurl', success: F.call(myDOMElement) }); F function(data){ $(this).text(data); } 我意识到我可以像下面那样包装回调函数并将domeElement作为调用F的参数传递,但我想知道是否有一种方法可以更接近上面的方法 this.CallServer({url: 'myurl', success: function(data

是否可以将函数的“this”或调用者设置为回调

this.CallServer({ url: 'myurl', success: F.call(myDOMElement) });

F function(data){ $(this).text(data); }
我意识到我可以像下面那样包装回调函数并将domeElement作为调用F的参数传递,但我想知道是否有一种方法可以更接近上面的方法

this.CallServer({url: 'myurl', success: function(data){F(data, myDOMElement);}});
F function(data, elem){ $(elem).text(data); }
bind
将返回相同的函数,只是每次调用时它都有一个固定值
this

简单地说,
bind
可以定义为:

func.bind = function(thisValue) {
    return function() {
        return func.apply(thisValue);
    };
};
例如

bind
将返回相同的函数,只是每次调用时它都有一个固定值
this

简单地说,
bind
可以定义为:

func.bind = function(thisValue) {
    return function() {
        return func.apply(thisValue);
    };
};
例如


精彩的!非常感谢您的仓促回复。太好了!非常感谢您的仓促回复。
var func = function() { return this };
var bound = func.bind([1, 2, 3]);
var result = bound(); // [1, 2, 3]