简体   繁体   中英

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)

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)

我相信你会得到什么样的价值this将是该范围内testtest.prototype

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