Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call 'new' on a constructor without assigning it to a variable

Tags:

javascript

I wonder if it there is any difference in the following code, provided that I do not need to call any method on an object. I just want to instantiate it.

var router = new Router();

vs

new Router();

Is it allowed to call 'new' keyword on a constructor without assigning it to a variable?

like image 478
user2814599 Avatar asked May 19 '15 07:05

user2814599


People also ask

Can constructors be called without new?

No, this is not possible. Constructors that are created using the class keyword can only be constructed with new , if they are [[call]]ed without they always throw a TypeError 1 (and there's not even a way to detect this from the outside).

What happens when you call a constructor?

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.

How to instantiate a class JavaScript?

The new operator instantiates the class in JavaScript: instance = new Class() . const myUser = new User(); new User() creates an instance of the User class.


4 Answers

The object will be instantiated and any code in its constructor will be executed, yes. After that the object will be garbage collected since there isn't any reference pointing to it (barring any use of closures inside the constructor).

So, yes, it's perfectly fine to do this. However, it's somewhat weird. If you just want code to run, you're rather use a function. There's little to no point in using an object in your case.

Or, if your constructor is already "doing enough" and you don't need to call additional methods on the object, then your constructor is probably doing too much! Consider refactoring the class and separating instantiation and starting of work into two separate methods.

like image 181
deceze Avatar answered Oct 17 '22 15:10

deceze


For anybody reading this in 2019, this is not only perfectly valid but it comes by default in a newly generated vue-cli project. The main drive behind this is to get rid of IIFEs in the code (not like they were needed with Webpack anyways) and not pollute the global namespace.

like image 42
Andrés Castillo Avatar answered Oct 17 '22 17:10

Andrés Castillo


Using new Router();

new Router() is not linked to any instance variable. so you can't access its methods. But this is not only drawback of using this method. when GC checks this line, there is no variable pointing this class and holding its instance. so it will collect all its memory and destroy it.

Using var router=new Router()

But if you use var router=new Router(); then GC thinks that there is a alive instance of this class and it will keep that class in memory that instance is alive. when you delete that instance by setting undefined or null or by using delete. then GC collects all memory used by that class for that instance.

like image 42
Laxmikant Dange Avatar answered Oct 17 '22 15:10

Laxmikant Dange


Yes, that works. But I wonder why you want to do this. Your object is created, and almost instantly disposed (unless there are some event listeners created or other references made). There is obviously a design flaw in your code, since you usually only instantiate an object if you intend to use it.

It is possible you are doing a 'singleton' pattern, but you shouldn't force that by using new. In my opinion, you should be using a function instead.

like image 1
Patrick Hofman Avatar answered Oct 17 '22 16:10

Patrick Hofman