SoFunction
Updated on 2025-04-05

How to use new in JavaScript and precautions

JavaScript is a prototype-based language, but it has a new operator that makes it look like a classic facing object language. This also confuses programmers and leads to some problematic programming patterns.

Actually, you never need to use new Object() in JavaScript. Replace it with the literal form {}.

Similarly, don't use new Array() , but instead use literal []. Arrays in JavaScript do not work like arrays in Java, and using Java-like syntax will only make you confused.

Similarly, you don't need to use new Number, new String, or new Boolean. These usages will only produce useless type encapsulation objects. Just use simple literals.

Do not use new Function to create function objects. It's better to use function expressions. for example:

frames[0].onfocus = new Function(”='antiquewhite'”)

A better way to write it is:

frames[0].onfocus = function () { = ‘antiquewhite';};

The second form allows the script compiler to see the function body faster, so the syntax errors in it will be detected faster. Sometimes programmers use new Function because they don't understand how internal functions work.

= new Function(”dynamicOptionListObjects[”+
       +”].change(this)”);

If we let strings be used as function bodies, the compiler cannot see them. If we use string expressions as function bodies, we will not see them either. A better way is not to program blindly. By creating a function call with the return value of the function, we can explicitly pass the value we want to bind by value. This allows us to initialize a series of selObj objects in a loop.

= function (i) {
   return function () {
       dynamicOptionListObjects[i].change(this);

   };
}();

It is never a good idea to use new directly on a function. For example, new function does not provide any advantage in constructing new objects.

myObj = new function () {
    = ‘core';
};

A better way is to use object literals, which is lighter and faster.

myObj = {
   type: ‘core'
};

If the method we need to create objects contains methods that require access to private variables or functions, a better way is to avoid using new.

var foo = new function() {
   function processMessages(message) {
       alert(”Message: ” + );
   }
    = function() {
       subscribe(”/mytopic”, this, processMessages);
   }
}

By calling the function using new, the object will hold a meaningless prototype object. This will only waste memory without any benefit. If we don't use new, we don't have to maintain a useless prototype object in the object chain. So we can use() to correctly call factory functions.

var foo = function () {
   function processMessages(message) {
       alert(”Message: ” + );
   }
   return {
       init: function () {
           subscribe(”/mytopic”, this, processMessages);
       }
   };
}();

So the principle is very simple: The only place where the new operator should be used is when an ancient constructor function is called. When calling a constructor function, new is mandatory. Sometimes you can come and new, but sometimes you shouldn't.