JavaScript-“this”指针在函数内部时未定义

JavaScript-“this”指针在函数内部时未定义,javascript,Javascript,我已经定义了这个.username;然而,当它在一个函数中时,它是未定义的,我怎样才能检索它的值呢 Parse.User.logIn(this.username, this.password, { success: function(user) { //gets undefined console.log('username is : ', this.username); }}); 这是因为回调与外部函数的作用域不同 在函数外部保存此函数的引用

我已经定义了这个.username;然而,当它在一个函数中时,它是未定义的,我怎样才能检索它的值呢

 Parse.User.logIn(this.username, this.password, {
   success: function(user) {

       //gets undefined
       console.log('username is : ', this.username);

    }});

这是因为回调与外部函数的作用域不同

在函数外部保存此函数的引用

var self = this;

然后在函数中使用self。

明白了!它成功了,非常感谢!没问题。不客气: