Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there problems with calling JavaScript constructors as functions(without new?)

Tags:

javascript

Recently, I have got into a habit of calling things like RegExp, String, Number, Object, TypeError, etc without "new".

e.g:

throw (TypeError("Error"));
var regex = RegExp('^word$');

I know that this is bad for cases when "this" context is needed, since without "new", "this" wreaks havoc on your global scope unless you wrap your code in 'use strict', in which case it raises an error that you are trying to mutate 'undefined'. (I am not sure that this works in very very old browsers).

e.g:

var constructor = function() {
    // 'use strict'; /* uncomment this line to avoid the behavior and be warned */
    this.state = 'working as intended';
};

var foo = constructor();
console.log(foo.state); // undefined
console.log(window.state); // we just polluted our global scope.

whereas

var constructor = function() {
    this.state = 'working as intended';
};

var foo = new constructor;
console.log(foo.state); // "working as intended"
console.log(window.state); // we are clean.

But in cases like the ones above, is it okay to do this, or are there problems I am setting myself up for if I get into a habit of doing this?

Thanks ahead of time.

like image 930
Dmitry Avatar asked Aug 22 '16 19:08

Dmitry


People also ask

Can constructor be called without new?

Constructors requires the use of the new operator to create a new instance, as such invoking a class without the new operator results in an error, as it's required for the class constructor to create a new instance.

Is it possible to use function constructor to create a new function in JS?

The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval() .

How do you call a constructor in JavaScript?

In JavaScript, a constructor gets called when an object is created using the new keyword.

What is difference between constructor and function in JavaScript?

Using functions for creating objects is fairly common in Javascript, so Javascript provides shortcut that lets you write functions for creating objects. These special functions are called Constructor functions. Constructors are functions that lets you populate the object which you need to create.


1 Answers

Be aware the result can be different.

For example, the Number constructor creates Number objects, but when called as a function it only does type coercion to primitive Number.

new Number(123); // Number { 123 }
Number(123); // 123

But yes, there are lots of the cases in which it doesn't matter whether you use new or not. They exist because of backwards compatibility, but with recently introduced constructors like Set or Map do require new.

In general, I would recommend using new when you want to create a new object. Then,

  • If you only want to do type coercion, you must call Boolean, Number or String without new.
  • For the same reason, if you want coerce to Object type, I wouldn't use new, but it doesn't matter.
  • If you want to create a primitive which doesn't have literal syntax, you must call Symbol without new.
  • If you want to create an object wrapper of a primitive value, you must call Boolean, Number, String or Symbol with new.
  • If you want to instantiate an old constructor like Array, Object, RegExp, Error, etc., I would use new, but it doesn't matter.
  • If you want to instantiate a recently introduced constructor like Set, Map, WeakSet, WeakMap, typed arrays, etc., you must call it with new.

For the old constructors that it doesn't matter, it's like they call themselves with new if you omit it. For example, for RegExp,

When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus the function call RegExp(…) is equivalent to the object creation expression new RegExp(…) with the same arguments.

like image 181
Oriol Avatar answered Sep 28 '22 02:09

Oriol