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?
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).
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.
The new operator instantiates the class in JavaScript: instance = new Class() . const myUser = new User(); new User() creates an instance of the User class.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With