简体   繁体   English

在构造函数上调用“new”而不将其分配给变量

[英]Call 'new' on a constructor without assigning it to a variable

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.我想知道下面的代码是否有任何区别,前提是我不需要在 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?是否允许在构造函数上调用“new”关键字而不将其分配给变量?

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. 考虑重构类并将实例化开始工作分成两个单独的方法。

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 . 你可能正在做一个“单身”模式,但你不应该强迫它使用new In my opinion, you should be using a function instead. 在我看来,你应该使用一个函数。

Using new Router(); 使用new Router();

new Router() is not linked to any instance variable. 新的Router()没有链接到任何实例变量。 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. 当GC检查这一行时,没有变量指向该类并保持其实例。 so it will collect all its memory and destroy it. 所以它会收集所有的记忆并将其摧毁。

Using var router=new Router() 使用var router=new Router()

But if you use var router=new Router(); 但是如果你使用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. 然后GC认为这个类有一个活着的实例,它会将该类保留在实例存活的实例中。 when you delete that instance by setting undefined or null or by using delete . 通过设置undefinednull或使用delete删除该实例时。 then GC collects all memory used by that class for that instance. 然后GC收集该类用于该实例的所有内存。

If you only call the constructor without assigning it to a variable, you can't manipulate the object. 如果只调用构造函数而不将其赋值给变量,则无法操作该对象。

However, if you only need to create a Router which then hooks itself into the correct events, Garbage Collection won't clean it up and it can function just as well. 但是,如果您只需要创建一个路由器,然后将其自身挂钩到正确的事件中,垃圾收集将不会清理它并且它也可以正常运行。

For anybody reading this in 2019, this is not only perfectly valid but it comes by default in a newly generated vue-cli project. 对于在2019年阅读此内容的任何人来说,这不仅完全有效,而且在新生成的vue-cli项目中默认出现。 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. 这背后的主要驱动力是摆脱代码中的IIFE(不像Webpack所需要的那样)并且不会污染全局命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM