Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

new

BaselineWidely available

Thenew operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

Try it

function Car(make, model, year) {  this.make = make;  this.model = model;  this.year = year;}const car1 = new Car("Eagle", "Talon TSi", 1993);console.log(car1.make);// Expected output: "Eagle"

Syntax

js
new constructornew constructor()new constructor(arg1)new constructor(arg1, arg2)new constructor(arg1, arg2, /* …, */ argN)

Parameters

constructor

A class or function that specifies the type of the object instance. The expression can be anything with sufficientprecedence, including an identifier, aproperty access, or anothernew expression, butoptional chaining is not allowed.

arg1,arg2, …,argN

A list of values that theconstructor will be called with.new Foo is equivalent tonew Foo(), i.e., if no argument list is specified,Foo is called without arguments.

Description

When a function is called with thenew keyword, the function will be used as a constructor.new will do the following things:

  1. Creates a blank, plain JavaScript object. For convenience, let's call itnewInstance.

  2. PointsnewInstance's [[Prototype]] to the constructor function'sprototype property, if theprototype is anObject. Otherwise,newInstance stays as a plain object withObject.prototype as its [[Prototype]].

    Note:Properties/objects added to the constructor function'sprototype property are therefore accessible to all instances created from the constructor function.

  3. Executes the constructor function with the given arguments, bindingnewInstance as thethis context (i.e., all references tothis in the constructor function now refer tonewInstance).

  4. If the constructor function returns anon-primitive, this return value becomes the result of the wholenew expression. Otherwise, if the constructor function doesn't return anything or returns a primitive,newInstance is returned instead. (Normally constructors don't return a value, but they can choose to do so to override the normal object creation process.)

Classes can only be instantiated with thenew operator — attempting to call a class withoutnew will throw aTypeError.

Creating an object with a user-defined constructor function requires two steps:

  1. Define the object type by writing a function that specifies its name and properties.For example, a constructor function to create an objectFoo might look like this:

    js
    function Foo(bar1, bar2) {  this.bar1 = bar1;  this.bar2 = bar2;}
  2. Create an instance of the object withnew.

    js
    const myFoo = new Foo("Bar 1", 2021);

Note:An object can have a property that is itself another object. See the examples below.

You can always add a property to a previously defined object instance. For example, the statementcar1.color = "black" adds a propertycolor tocar1, and assigns it a value of"black".

However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the constructor'sprototype property. This defines a property that is shared by all objects created with that function, rather than by just one instance of the object type. The following code adds acolor property with value"original color" to all objects of typeCar, and then overwrites that value with the string"black" only in the instance objectcar1. For more information, seeprototype.

js
function Car() {}const car1 = new Car();const car2 = new Car();console.log(car1.color); // undefinedCar.prototype.color = "original color";console.log(car1.color); // 'original color'car1.color = "black";console.log(car1.color); // 'black'console.log(Object.getPrototypeOf(car1).color); // 'original color'console.log(Object.getPrototypeOf(car2).color); // 'original color'console.log(car1.color); // 'black'console.log(car2.color); // 'original color'

Note:While the constructor function can be invoked like any regular function (i.e., without thenew operator),in this case a new object is not created and the value ofthis is also different.

A function can know whether it is invoked withnew by checkingnew.target.new.target is onlyundefined when the function is invoked withoutnew. For example, you can have a function that behaves differently when it's called versus when it's constructed:

js
function Car(color) {  if (!new.target) {    // Called as function.    return `${color} car`;  }  // Called with new.  this.color = color;}const a = Car("red"); // a is "red car"const b = new Car("red"); // b is `Car { color: "red" }`

Prior to ES6, which introducedclasses, most JavaScript built-ins are both callable and constructible, although many of them exhibit different behaviors. To name a few:

  • Array(),Error(), andFunction() behave the same when called as a function or a constructor.
  • Boolean(),Number(), andString() coerce their argument to the respective primitive type when called, and return wrapper objects when constructed.
  • Date() returns a string representing the current date when called, equivalent tonew Date().toString().

After ES6, the language is stricter about which are constructors and which are functions. For example:

  • Symbol() andBigInt() can only be called withoutnew. Attempting to construct them will throw aTypeError.
  • Proxy andMap can only be constructed withnew. Attempting to call them will throw aTypeError.

Examples

Object type and object instance

Suppose you want to create an object type for cars. You want this type of object to becalledCar, and you want it to have properties for make, model, and year.To do this, you would write the following function:

js
function Car(make, model, year) {  this.make = make;  this.model = model;  this.year = year;}

Now you can create an object calledmyCar as follows:

js
const myCar = new Car("Eagle", "Talon TSi", 1993);

This statement createsmyCar and assigns it the specified values for itsproperties. Then the value ofmyCar.make is the string "Eagle",myCar.year is the integer 1993, and so on.

You can create any number ofcar objects by calls tonew. Forexample:

js
const kensCar = new Car("Nissan", "300ZX", 1992);

Object property that is itself another object

Suppose you define an object calledPerson as follows:

js
function Person(name, age, sex) {  this.name = name;  this.age = age;  this.sex = sex;}

And then instantiate two newPerson objects as follows:

js
const rand = new Person("Rand McNally", 33, "M");const ken = new Person("Ken Jones", 39, "M");

Then you can rewrite the definition ofCar to include anowner property that takes aPerson object, as follows:

js
function Car(make, model, year, owner) {  this.make = make;  this.model = model;  this.year = year;  this.owner = owner;}

To instantiate the new objects, you then use the following:

js
const car1 = new Car("Eagle", "Talon TSi", 1993, rand);const car2 = new Car("Nissan", "300ZX", 1992, ken);

Instead of passing a literal string or integer value when creating the new objects, theabove statements pass the objectsrand andken as theparameters for the owners. To find out the name of the owner ofcar2, youcan access the following property:

js
car2.owner.name;

Usingnew with classes

js
class Person {  constructor(name) {    this.name = name;  }  greet() {    console.log(`Hello, my name is ${this.name}`);  }}const p = new Person("Caroline");p.greet(); // Hello, my name is Caroline

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-new-operator

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp