seasick
seasick

Reputation: 1225

Call anonymous function with 'this' from function

How can I execute this anonymous function with the context of the function that's supplied from call?

function test(text) {
    this.first = 'test';
    console.log(this.first);
}


(function(val){
    return function(val) {
        console.log(this.first);
    }
}()).call(test)

Upvotes: 1

Views: 56

Answers (2)

seasick
seasick

Reputation: 1225

I did not incluse var self = this; before entering the return function:

(function(val){
    var self = this;   //add this line
    return function(val) {
        console.log(self.first);
    }
}()).call(test)

Upvotes: 2

Stuart P. Bentley
Stuart P. Bentley

Reputation: 10755

I believe you get the value of what this would be within the context of test with test.prototype.

Upvotes: 0

Related Questions