[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/new [Back]  [Original]

new - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

new

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

( ) new , , .

In this article

new constructor[([arguments])]

constructor

, .

arguments

, .

, , :

  1. , .
  2. , new.

, , . . .

new Foo(...) , :

  1. , Foo.prototype.
  2. Foo this, . new Foo new Foo(), , Foo .
  3. new , . , . 1. ( , , .)

. , car1.color = "black" color car1, "black". . , Car.

Function.prototype. , , - . color null car, "black" car1. prototype.

js
function Car() {}
car1 = new Car();

console.log(car1.color); // undefined

Car.prototype.color = null;
console.log(car1.color); // null

car1.color = "black";
console.log(car1.color); // black

, . car, : , .

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

car:

js
var mycar = new Car("Eagle", "Talon TSi", 1993);

mycar . , mycar.make "Eagle", mycar.year 1993, .

car new. :

js
var kenscar = new Car("Nissan", "300ZX", 1992);

, person:

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

:

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

car, , owner:

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

car:

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

. car2, :

js
car2.owner.name;

Specification
ECMAScript 2027 LanguageSpecification
# sec-new-operator


Web Proxy Viewer  |  New URL  |  Original Page