简体   繁体   中英

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. One of the pairs in the options object is a callback which points to a function defined in 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.

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?); as well as once myObject.init(); is caled. During the first executions this is myObject , but subsequent calls through the functionWithDifferentScope identify this as window .

Is there a way to pass the myObject namespace to the myObject.options.callback value as a parameter?

I think what you are looking for is prototype style "bind"

Basically "this.myCallback(this)" is a call to the function.

this.myCallback is the function itself. (It is an object with the type function).

You can call it using the method 'call' or 'apply' that you can use on functions. Which will call these functions.

See:

https://developer.mozilla.org/en-US/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

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)

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.

http://prototypejs.org/doc/latest/language/Function/prototype/bind/

https://developer.mozilla.org/en-US/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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM