1
I made a class, in which saved inside a login and user password array:
function AccountManager() {
this.accounts = {};
}
AccountManager.prototype.createAccount = (login, password) => {
this.accounts[login] = password;
};
module.exports = AccountManager;
And at index.js, I created a test account:
var accountManager = new lib.AccountManager();
accountManager.createAccount("test", "test");
But it says that the accounts
is undefined.
this.accounts[login] = password; ^ TypeError: Cannot set property 'test' of undefined at AccountManager.createAccount (c:\users\natha\documents\visual studio 2017\Projects\WarfaceXmpp\WarfaceXmpp\lib\Utils\AccountManager.js:6:23) at Object.<anonymous> (c:\users\natha\documents\visual studio 2017\Projects\WarfaceXmpp\WarfaceXmpp\app.js:6:16) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Timeout.Module.runMain [as _onTimeout] (module.js:605:10) at ontimeout (timers.js:386:14) at tryOnTimeout (timers.js:250:5)
Your reply and @Walter Gms worked perfectly, unfortunately I can not vote for two answers, but as I do not know much about class structure in javascript I will mark this.
– FRNathan13
@sysWOW32 I consider his correct, but mine was the one that explained the question of the behavior of Arrow Function, not just give a code that works, explain where it failed is also important to help who asks and this is what I did =) ... then I’ll edit the answer to add details about ES6
– Guilherme Nascimento
Okay, thank you very much.
– FRNathan13