cfipilot
cfipilot

Reputation: 76

this.constructor.SomeVarible vs this.SomeVarible in OOP JS

So what is the difference between the following 2 examples?

var SomeFn = function (name){this.constructor.SomeVarible = name}

And

var SomeFn = function (name){this.SomeVarible = name}

Upvotes: 1

Views: 134

Answers (1)

Felix Kling
Felix Kling

Reputation: 816452

Assuming you have

var obj = new SomeFn('foo');

In the first case, the value will be assigned to SomeFn.SomeVarible, since this.constructor refers to SomeFn. I actually don't see a reason why one would do something like this, but if anyone has an idea, please let me know.

In the second case, it will be assigned to obj.SomeVariable, since this refers to the newly created object and is assigned to obj.

Upvotes: 4

Related Questions