简体   繁体   English

是否可以将参数传递给设置为对象文字值的函数?

[英]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.

相关问题 设置 object 属性的默认值传递给 function - Set default value of property of object passed on parameter to function 如何创建将传递的参数视为文字的 function? - How can I create a function which treats a passed parameter as a literal? 编写一个检查参数是否为对象文字或函数或字符串值的函数 - Writing a function that checks if the parameter is an object literal OR a function OR a string value 如何将函数参数值连接到对象文字属性名称 - How to concate function parameter value into object literal property name 如何设置 object 的 state 作为 function 中的参数传递? - How do you set the state of an object passed as a parameter in a function? 可以将对象文字中的多个属性设置为相同的值吗? - Can one set multiple properties inside an object literal to the same value? 设置函数对象参数的键的默认值 - Set the default value for the key of the function object parameter 函数未修改传递的参数值 - Function not modifying passed parameter value 如何检查传递给javascript函数的参数值是否为空? - How can I check if the value of a parameter passed to a javascript function is null? 有没有办法判断函数参数是作为文字还是作为变量传递? - Is there a way to tell whether a function parameter was passed as either a literal or as a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM