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

new - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

new

Baseline

20157

new

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);
// : "Eagle"

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

constructor

new

arg1, arg2, , argN

constructor new Foo new Foo() Foo

new new

  1. JavaScript newInstance

  2. newInstance [[Prototype]] prototype Object prototype newInstance Object.prototype [[Prototype]]

    : prototype

  3. newInstance this this newInstance

  4. new newInstance

new new TypeError

2

  1. Foo

    js
    function Foo(bar1, bar2) {
      this.bar1 = bar1;
      this.bar2 = bar2;
    }
    
  2. new

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

:

car1.color = "black" color car1 "black"

prototype Car color "original color" car1 "black"

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

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

Car.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'

: new this

new new.target new.target undefined new

js
function Car(color) {
  if (!new.target) {
    // 
    return `${color} car`;
  }
  // new 
  this.color = color;
}

const a = Car("red"); // a  "red car"
const b = new Car("red"); // b  `Car { color: "red" }`

ES6 JavaScript

ES6

Car makemodelyear

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

myCar

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

myCar myCar.make "Eagle"myCar.year 1993

new car

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

Person

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

Person 2

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

Car Person owner :

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

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

owner rand ken car2

js
car2.owner.name;

new

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

ECMAScript 2027 LanguageSpecification
# sec-new-operator


Web Proxy Viewer  |  New URL  |  Original Page