I'm new to javascript could someone please explain why this code doesn't work?
var User = function () {
var userId = 0;
var clear = function () {
userId = 0;
}
return{
clear:clear,
userId:userId,
}
}
...
// in mocha test:
var john = new User();
john.userId = 666;
john.userId.should.equal(666); // true
john.clear()
john.userId.should.equal(0); // false
Regards
Userobject and assign the object reference to the variablejohn. Then you proceed to reference an undeclared variable calleduser.user.idandidwithin the constructor are two different things. One is what other languages callpublic, while the other one isprivate. They may share the name, but point towards different memory locations.