0


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

4
  • 1
    You create a User object and assign the object reference to the variable john. Then you proceed to reference an undeclared variable called user. Commented Jun 26, 2017 at 14:16
  • user.id and id within the constructor are two different things. One is what other languages call public, while the other one is private. They may share the name, but point towards different memory locations. Commented Jun 26, 2017 at 14:16
  • Sorry, got a typo. Updated the question. Commented Jun 26, 2017 at 14:20
  • I created a fiddle for you. jsfiddle.net/tqc1wrkp Commented Jun 26, 2017 at 14:23

5 Answers 5

1

What you've done is create a function closure. This means that inside the clear function, userId is created as a local variable and is separately scoped from the global User.userId.

What you need to do is use the this keyword to specify the userId you are trying to change is the global one.

var User = function () {
    var userId = 0;
    var clear = function () {
        this.userId = 0;
    }
    return{
        clear:clear,
        userId:userId,
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

although the var userId = 0; is redundant then.
@Sirko without that the code will error (at least in my runtime)! See error in the console: jsfiddle.net/r7mp7e6r
Sure it will as there is the reference in the returned object. You would have to set the zero there directly.
Ahh I misunderstood - you were saying the initialization in that place was pointless.
Having two pointers to the same value is pointless in this example, yes.
0

You are returning an object

 return{
    clear:clear,
    userId:userId,
}

which doesn't have an id property, but userId.

2 Comments

var john = new User(), and then u reference user? try john.clear()
try removing the var before defining the User 'class'. Just do User = function() {}
0

You are missing the this keyword.

var User = function () {
    var userId = 0;
    var clear = function () {
        this.userId = 0;
    }
    return{
        clear:clear,
        userId:userId,
    }
}

Comments

0

You need to use "this" when referencing the property in the clear method

var clear = function () {
    this.userId = 0;
}

Comments

0

There are several problems here.

1 : userId is undefined in your function definition.

2: user is undefined, so user.clear() is also undefined (actually, I'm surprised your code doen't just crash). You mean john.id and john.clear().

2 Comments

I think these were both typos - the asker edited his question to reflect this.
Indeed, while I was typing my answer. OP asks why their code doesn't work, but posts something full of typos.........

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.