A use for javascript's weird object behavior

12/03/2014 08:40:00 AM
Tweetable
In a previous post I posited a question: if you have a javascript object called myObject and create a new variable and set it equal to that: var newVariable=myObject;, why would javascript treat the new variable as a second name for the same object, rather than as an object in it's own right? What possible use could this have?

Here's one use for aliases. Suppose we have a function inside a class like this
function myClass(){
   this.property1;
   function setProperty1(){
      this.property1="some value";
   }
}
var myObject=new myClass();
The aim in the code above is to create an object called myObject with a property called myObject.property1, that is assigned the value "some value" value by the private method setProperty1 inside the class, which is something that might arise in real life if, for example, you want the class to depend on user input in some way. The problem is that the above code doesn't work. The this keyword tells the program to operate on the immediate "owner" of the object. Thus,
function myClass(){
   this.property1;
}
var myObject=new myClass();
creates an empty property attached to myObject while
function setProperty1(){
   this.property1="some value";
}
attempts to attach a property to the method setProperty1() inside of myObject, not to myObject itself.

The way out of this is to use an alias, like so:
function myClass(){
   this.property1;
   var that=this;
   function setProperty1(){
      that.property1="some value";
   }
}
var myObject=new myClass();
Inside of the class but outside the method in the class, we've created a private variable called that (my choice of name is convention, but the name can be anything you choose--that is not a javascript keyword), and set it equal to this, which is the javascript keyword telling the computer to operate on the object's owner, myObject. Thus when we call the constructor it creates a private variable inside the object and sets it equal to the object itself--but remember, javascript does not copy objects, so this private variable merely points to myObject. The result is that the method inside myObject now understands that it is supposed to set myObject.property1 and not myObject.setProperty1().property1.

So, there you go. This weird fact about javascript has at least one use. Still, seems like this could be designed better.
Harald Korneliussen 12/03/2014 09:54:00 AM
Maybe more on topic to the original post, but this is the way it works in Java, Python, and most languages I know of: primitive types are copied, complex types (i.e. objects) only the reference is copied.

In Java you would use Object.clone() to make a proper copy, in python you would use copy.copy(), or copy.deepcopy() if you want all sub-objects to be copied as well.

The reason it does shallow copying and copying of references by default is not semantic: it's about performance. Objects can get very big, you don't want to copy them if just handing out a reference to the same object will do.
Matthew Martin 12/03/2014 10:05:00 AM
My understanding, though, is that even when it doesn't copy the object, most languages will treat the new reference as if it was a separate copy. For example, in R if you have a large dataframe called x and save it to a new variable y, it won't literally copy the dataframe, but any changes you make to y have no effect on x. Is this the way it works in Java and Python?
Harald Korneliussen 12/04/2014 03:54:00 AM
R is a pretty strange language. It does indeed have copy-on-modify semantics you describe (I actually didn't know that, although I used it in a course!). You would indeed modify the original if you tried that in Java and Python.

These days, having many references to a thing and not having a clear image of when it can change, is recognized as a common source of bugs. Functional languages like Haskell try to do away with mutability entirely, and you get a more math-like way of thinking of things, where if a = 1 it won't ever later be 2.

I guess that was the goal of the R designers too, to make things less surprising for people from a math/statistics background. But the way they did it is pretty unusual.
Alin 1/08/2015 08:29:00 AM
Thanks for the information provided in this blog as it has been very helpful to what I'm doing with my own works. Appreciate your blog very much.