55
11
u/muh2k4 May 24 '21
When you return "b" you return a copy of the value 1. Basically {b: 1}. So not a reference. It will always stay 1 in your example. But when you use the getter function, it is referencing the local b from IIFE.
6
u/jon216999 May 25 '21
Does someone know where I can find similar questions with explanations to study?
10
May 24 '21 edited May 24 '21
In setB(c) function, you have to say both “b = c” and “this.b = c”
Edit:
The “b” in “b = c” is the local variable in the IIFE (closure).
The “this.b” in “this.b = c” is the property of variable a.
The property is not updated when you change the value of the local variable. You have to change them both because they are not reference types
15
u/rauschma May 24 '21
In other words: The property
a.bis a snapshot of the local variablebthat isn’t updated along with it.You can fix this as follows:
let a = (function () { let b = 1; return { getB() { return b; }, setB(c) { b = c; }, get b() { // getter return b; }, }; })(); a.setB(100); console.log(a.b); // 100 console.log(a.getB()); // 100Another possible solution – turn
binto a property:let a = { b: 1, getB() { return this.b; }, setB(c) { this.b = c; }, }; a.setB(100); console.log(a.b); console.log(a.getB());
1
u/EmperorMing101 May 25 '21
As someone from a python background this is so weird to me you can create an entire class on the fly. Though the idea is still the same (in python it would be self.b = c to update b attribute of a)
5
u/delventhalz May 25 '21
There isn't really a class here. It's more like a dictionary with a number property and two function properties.
This would be the equivalent more or less in Python:
def b_func(): b = 1 def get_b(): return b def set_b(c): nonlocal b b = c return { 'b': b, 'get_b': get_b, 'set_b': set_b } a = b_func() a['set_b'](100) print(a['b']) # prints 1 print(a['get_b']()) # prints 1001
u/equitable_emu May 25 '21
There isn't really a class here. It's more like a dictionary with a number property and two function properties.
To be fair, that's basically all that classes are in JS.
1
u/delventhalz May 25 '21 edited May 25 '21
We’re confusing terms here I think. A class is what generates an object. It’s not the object itself. Arguably the function in OP’s code is “a class” in a very broad sense, but I think that’s a particularly bad comparison in this case. OP’s function is an IIFE and can never be used to generate another object again.
Anyway, my original point being: there is nothing particularly special to JavaScript here. Any language that supports closure variables and function properties on dictionaries (like Python) can write very similar code.
1
May 25 '21
Oh now i get it, since we aren't using " this " and that b is a primitive, the functions still has a reference to the b declared with let while " a " is totally different and just copied b's value.
You can also set " b " to an object or an array and you can see that both " a.b " and the functions reference the same thing.
1
u/SlowerThanLightSpeed May 26 '21
To see the top answer in action, and to generally visualize what is happening in your code, check out tools like:http://pythontutor.com/javascript.html#mode=edit
<edit>Click the 'Visualize code' button, then, at about step 4, the code you've shared and the top answer should start to make sense... especially if you accept that the returned object's b value was set to a primitive (and thus isn't set by reference)</edit>
1
1
115
u/delventhalz May 24 '21
There are two b’s here: the closure variable defined with
let, and the property with the same name on the returned object.When you write this:
It is just a shorthand for this:
In other words, you are creating a new object with a property named "b", and the value of that property is copied from the variable with the same name at return time. There is no other connection between the variable and the object property.
So when you use
setBto change the value of the variable, it will not have any effect on the object. However, when you callgetByou fetch the current value of the variable.