我遇到了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
?