function Class () {
var self = this;
this.hi = true; // this { hi: true }, self { hi: true }
self.hi = false; // this { hi: false }, self { hi: false }
}
Why doesn't self behave like a normal var?
2 Answers 2
It's because this is (always) an object, not a primitive.
When you assign a variable containing (or more accurately "pointing at") an object to a new variable, both variables point at the same object - it's "copy by reference". Changes to the contents of that object will be visible through both variables.
When you assign a variable containing a primitive to a new variable, you assign a copy of that value to the new variable. Changes to the original variable do not affect the new variable.
1 Comment
There's nothing special about self here. Try it with any two variable names. This is how most modern languages work.
var x = {};
var y = x;
x.hi = true; // x { hi: true }, y { hi: true }
y.hi = false; // x { hi: false }, y { hi: false
As Jack pointed out, the key thing to realize here is that both variables are referencing the same object.
Note that if you want to "copy by value" in cases like this (copying objects), you do have options. Some libraries, such as Underscore and Lo-Dash, have a clone method that you can use to create a new object and populate it with all the same properties as another:
var y = _.clone(x);
2 Comments
this - it's an object, not a primitive.self (by which I mean, the actual word "self"). Obviously you're right that objects are different from so-called "primitives"; but there'd be no opportunity for the OP to get confused by that in this way since you can't mutate primitives to begin with.
var x = {}; var y = x; y.z = "1"; alert(x.z);