Is it possible to find out if a function is called from a link?

Both of these calls will warn hi, but is it possible to find out how it was called?

function Test() {
    this.foo = this;
}

Test.prototype.bar = function (value) {
    // Check if it was called through foo
    alert(value);
}

var test = new Test();

test.foo.bar('hello');
test.bar('hello');

Basically, I want to know if it was called from test or test.foo.

Is it possible?

+4
source share
1 answer

Comments first, but in fact, that the answer is XD

No, It is Immpossible. The reason is that writing this.foo = this, you warrant that this.fooand this- it is the same. Since they are one and the same thing, trying to tell them apart, it looks like this surreal joke:

Q: What is the difference between duck?
A: One of his legs is the same.

+5
source

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


All Articles