Undefined variable, even if defined

Asked

Viewed 169 times

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)

2 answers

6


I believe the use of (login, password) => { makes behavior change instead of accessing this the this of the "object" the this is of the same value as global (Node.js) or window (if you are a browser), then to adjust do this:

function AccountManager() {
    this.accounts = {};
}
AccountManager.prototype.createAccount = function(login, password) {
    this.accounts[login] = password;
};

AccountManager.prototype.test = function() {
    console.log(this.accounts);
};

var x = new AccountManager();
x.createAccount("test", "test");
x.createAccount("foo", "bar");
x.test("test", "test");

  • 1

    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.

  • 1

    @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

  • Okay, thank you very much.

2

Is that code inside a class? The this when within a class is the scope of class properties, commonly defined in constructor() class...

class Teste {
    constructor() {
        this.title = 'Mensageria';
    }
    teste(){
        this.title = 'teste' // isso funciona
    }
}

If there is no class, this becomes the function, therefore the mistake.

  • interesting, thought that already putting this.property would already be accessible. I really still don’t know how to work with javascript classes.

  • There’s only one problem with that code, teste() or function teste() ?

  • 1

    test() without Function :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.