| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/new | [Back] [Original] |
Get to know MDN better
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"
new constructor
new constructor()
new constructor(arg1)
new constructor(arg1, arg2)
new constructor(arg1, arg2, /* , */ argN)
constructorarg1, arg2, , argNconstructor new Foo new Foo() Foo
new new
JavaScript newInstance
newInstance [[Prototype]] prototype Object prototype newInstance Object.prototype [[Prototype]]
:
prototype
newInstance this this newInstance
2
Foo
function Foo(bar1, bar2) {
this.bar1 = bar1;
this.bar2 = bar2;
}
new
const myFoo = new Foo("Bar 1", 2021);
:
car1.color = "black" color car1 "black"
prototype Car color "original color" car1 "black"
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
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
Car makemodelyear
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
myCar
const myCar = new Car("Eagle", "Talon TSi", 1993);
myCar myCar.make "Eagle"myCar.year 1993
new car
const kensCar = new Car("Nissan", "300ZX", 1992);
Person
function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
Person 2
const rand = new Person("Rand McNally", 33, "M");
const ken = new Person("Ken Jones", 39, "M");
Car Person owner :
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
const car1 = new Car("Eagle", "Talon TSi", 1993, rand);
const car2 = new Car("Nissan", "300ZX", 1992, ken);
owner rand ken car2
car2.owner.name;
new 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 |