[英]Can a parameter be passed to a function that is set as an object literal value?
I have a function, functionWithDifferentScope , that takes an object, myobject.options , as a parameter. 我有一个函数functionWithDifferentScope ,该函数将对象myobject.options作为参数。 One of the pairs in the options object is a callback which points to a function defined in myObject : myCallback . options对象中的一对对象是回调,它指向myObject中定义的函数: myCallback 。
What I'm trying to achieve is injection of the myObject namespace into the callback of a function that is defined (by a 3rd party) at the global level. 我要实现的目标是将myObject 命名空间注入到在全局级别(由第3方定义)的函数的回调中。
A simplified example: 一个简化的例子:
var myObject = {
options: {
callback: this.myCallback(this),
...,
},
init: function() {
// functionWithDifferentScope operates in the 'window' context
functionWithDifferentScope(this.options);
},
myCallback: function(namespace) {
// 'this' is window
// 'namespace' is myObject
}
}
myObject.init();
When executing this script, this.myCallback(this) appears to be executed at definition (due to the parenthesis?); 执行此脚本时, this.myCallback(this)似乎在定义时执行(由于括号?); as well as once myObject.init(); 以及一次myObject.init(); is caled. 被校准。 During the first executions this is myObject , but subsequent calls through the functionWithDifferentScope identify this as window . 在第一次处决这是myObject的 ,而是通过functionWithDifferentScope后续调用标识为窗口 。
Is there a way to pass the myObject namespace to the myObject.options.callback value as a parameter? 有没有办法将myObject 命名空间作为参数传递给myObject.options.callback值?
I think what you are looking for is prototype style "bind" 我认为您正在寻找的是原型样式“绑定”
Basically "this.myCallback(this)" is a call to the function. 基本上,“ this.myCallback(this)”是对该函数的调用。
this.myCallback is the function itself. this.myCallback是函数本身。 (It is an object with the type function). (它是带有类型函数的对象)。
You can call it using the method 'call' or 'apply' that you can use on functions. 您可以使用可以在函数上使用的方法“ call”或“ apply”来调用它。 Which will call these functions. 将调用这些函数。
See: 看到:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fapply https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply?redirectlocale=zh-CN&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fapply
The first argument is the object context to work in. What I think you mean by object namespace. 第一个参数是要使用的对象上下文。我认为您所说的对象名称空间是什么。
so: a.callback(5)
is the same as a.callback.call(a,5)
因此: a.callback(5)
与a.callback.call(a,5)
However please note that these days if you are working with most javascript libraries you probably have a 'bind' function that will do the work for you. 但是请注意,这些天,如果您使用的是大多数javascript库,则可能有一个“绑定”功能可以为您完成工作。
http://prototypejs.org/doc/latest/language/Function/prototype/bind/ http://prototypejs.org/doc/latest/language/Function/prototype/bind/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
the idea is that this.callback.bind(this)
returns a Function object you can call that will inject the correct context automatically so you can pass the return value of bind alone as a callback and be assured that the method will be executed on the correct object. 这个想法是this.callback.bind(this)
返回一个您可以调用的Function对象,该对象将自动注入正确的上下文,因此您可以单独将bind的返回值作为回调传递,并确保该方法将在正确的对象。
Do you mean this? 你是这个意思吗
var myObject = new (function() {
var t = this;
vac callback = function() {
// t equals to the myObject-instance
// this equals to window
}
this.init = function() {
funcWithDifferencScope(callback);
}
})();
myObject.init();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.