Having received "this" with which the calling function was called in JavaScript

Is it possible to get thiswith which the function was called callerin JavaScript without passing thisarguments in a way that IE supports, as well as Firefox / Chrome et al?

For instance:

var ob = {
    callme: function() {
        doSomething();
    }
}
ob.callme();

function doSomething() {
    alert(doSomething.caller.this === ob); // how can I find the `this` that 
                                           // `callme` was called with 
                                           // (`ob` in this case) without 
                                           // passing `this` to `doSomething`?
}

I'm starting to suspect that this is not the case, but I thought I might ask how this would make my code much shorter and easier to read.

+3
source share
1 answer

Well, the closest thing I can think of without technical passing the value as an argument would be to set the value of the thisfunction doSomething.

doSomething - , , doSomething(); this Global, ...

:

var ob = {
  callme: function () {
    doSomething.call(this); // bind the `this` value of `doSomething`
  }
};

function doSomething () {
  alert(this === ob); // use the bound `this` value
}

ob.callme();
+3

Source: https://habr.com/ru/post/1747796/


All Articles