alexino2
alexino2

Reputation: 101

Using this or new in JS?

I've got 3 codes :

var control = new Control();

function Control() {

   this.doSomethingElse = function() {...}
   this.doSomething = function () {
       control.doSomethingElse();
   }

}

Or

var control = new Control();

function Control() {
   var self = this;

   this.doSomethingElse = function() {...}
   this.doSomething = function () {
       self.doSomethingElse();
   }
}

Or

var control = Control();

function Control() {
   var self = this;

   this.doSomethingElse = function() {...}
   this.doSomething = function () {
       self.doSomethingElse();
   }

  return self;
}

Important : The function is a controller, and just declared once. Then I'm using "control" everywhere in my code...

I was wondering if the control.doSomethingElse() was slow ?

In the end, what is the right thing to do and/or the fastest code in those exemple ?

Thanks !

Upvotes: 5

Views: 102

Answers (2)

Tomáš Zato
Tomáš Zato

Reputation: 53119

Do not define methods in constructor - that means defining them every time an instance is created. Use Control.prototype.foo = function() {} instead. Also you do not need to return this if you're using new operator - that's the whole point of new operator.

The recommended approach is this:

function MyClass(param1) {
    // Here we're changing the specific instance of an object
    this.property1 = param1;
}
// Prototype will be shared with all instances of the object
// any modifications to prototype WILL be shared by all instances
MyClass.prototype.printProperty1 = function() {
    console.log(this.property1);
}

var instance = new MyClass("Hello world!");
instance.printProperty1(); // Prints hello world

To understand this code, you need to understand javascript's prototype-based inheritance model. When you create instance of MyClass, you get a new object that inherits any properties present in MyClass.prototype. Read more about it.


Also I wonder:

The function is a controller, and just declared once.

If you're not using this multiple times, you don't need to create something like class. You can do this instead:

var control = {doSomething:function() { ... }};

I assume you are used to Java, where everything must be a class, whether it makes sense or not. Javascript is different, you can also make single objects or functions as you need.

Upvotes: 2

Alnitak
Alnitak

Reputation: 339776

The first is wrong - an object should never internally use the variable name by which it is known outside. Other code could change that variable to point to something else, breaking this code.

The third is also wrong - when calling Control() without new the assignments to this.foo inside will end up getting attached to the global object (except in strict mode, where there's no implicit this on bare function calls, so the assignment to this.doSomethingElse tries to attach to undefined, causing a runtime error).

That only leaves the second as appropriate, but ultimately it's a question of correctness, not performance.

Upvotes: 3

Related Questions