M
M
Maxim Ivanov2015-09-26 22:29:13
JavaScript
Maxim Ivanov, 2015-09-26 22:29:13

Is it possible to somehow override, inherit this for an anonymous function?

Let's say we have a code

var test = function(foo){
      /* тут действия */

      // формируем собственный this
      // функция foo должна уметь работать с нашим новым this
      foo();


    };


    test(function(){
      this.MyMethod();


    });

function foo should be able to work with our new this, can this be implemented somehow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MK, 2015-09-26
@omaxphp

Might try like this:

var test = function(foo){
    /* тут действия */
    var anythis=...; // формируем собственный this
    // функция foo должна уметь работать с нашим новым this
    foo.call(anythis); //Или apply, надо под ситуацию подстраиваться.
    //Или так:
    func=foo.bind(anythis);
    func();
};
test(function(){
    this.MyMethod();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question