我遇到了NPM模块的对象属性问题。更改“父”属性的属性
我有这样的代码:
var service = { //In my code this lane is: module.exports = { because is for a npm module so the client will call it using require();
username: 'test',
serviceFunction: function(){
console.log(this.username); //Prints service.username
},
serviceObject: {
getUsername: function() {
console.log(this.username); //THIS IS CAUSING THE FAIL, ITS UNDEFINED
}
}
}
我可以用this.username
如果它位于一个serviceFunction
访问service.username
。
我该怎么做,而我在serviceObject.getUsername
?