JavaScript:具有新关键字的构造函数自动执行

    技术2022-07-11  127

    JavaScript is full of small, interesting facets that can trip you up, make you laugh, or make you cry.  This post is about an interesting one.  Those of you that have worked with JavaScript functions, and in a way JavaScript "classes" (as you used with MooTools), you're well acquainted with the new keyword.  With the new keyword you get the ability to pass arguments with the function call, but did you know that if you have no arguments, you don't need the parens at all?

    JavaScript充满了很多有趣的小方面,可以使您绊倒,使您发笑或使您哭泣。 这篇文章是关于一个有趣的。 那些使用过JavaScript函数并以某种方式对JavaScript进行“类化”(与MooTools一起使用)的人,您对new关键字非常熟悉。 使用new关键字,您可以通过函数调用传递参数,但是您是否知道如果没有参数,就根本不需要parens?

    function MyClass() { console.log('Initialized!'); //Set a property, as an example this.dirty = true; } var instance = new MyClass; // >> "Initialized!''

    So why am I telling you this?  I have no idea.  It's just one of those fun tidbits that you can add to your brain. :)

    那我为什么要告诉你呢? 我不知道。 这只是您可以添加到大脑中的那些有趣的花絮之一。 :)

    翻译自: https://davidwalsh.name/javascript-constructor-autoexecution-keyword

    Processed: 0.008, SQL: 9