| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor | [Back] [Original] |
Get to know MDN better
Object.prototype.constructor | |
|---|---|
const o1 = {};
o1.constructor === Object; // true
const o2 = new Object();
o2.constructor === Object; // true
const a1 = [];
a1.constructor === Array; // true
const a2 = new Array();
a2.constructor === Array; // true
const n = 3;
n.constructor === Number; // true
constructor prototype constructor
const o = new TypeError(); // : TypeError -> Error -> Object
const proto = Object.getPrototypeOf;
Object.hasOwn(o, "constructor"); // false
proto(o).constructor === TypeError; // true
proto(proto(o)).constructor === Error; // true
proto(proto(proto(o))).constructor === Object; // true
(Tree) (theTree) theTree constructor
function Tree(name) {
this.name = name;
}
const theTree = new Tree("Redwood");
console.log(`theTree.constructor is ${theTree.constructor}`);
theTree.constructor is function Tree(name) {
this.name = name;
}
constructor
const arr = [];
arr.constructor = String;
arr.constructor === String; // true
arr instanceof String; // false
arr instanceof Array; // true
const foo = new Foo();
foo.constructor = "bar";
foo.constructor === "bar"; // true
// etc.
constructor [[Prototype]]
const arr = [];
Object.hasOwn(arr, "constructor"); // false
Object.hasOwn(Object.getPrototypeOf(arr), "constructor"); // true
arr.constructor = String;
Object.hasOwn(arr, "constructor"); // true
Object.getPrototypeOf(a).constructor instanceof constructor Symbol.hasInstance
const arr = [];
arr.constructor = String;
arr instanceof String; // false
arr instanceof Array; // true
constructor instanceof Symbol.toStringTag typeof
prototype new ([[Prototype]]) ConstructorFunction.prototype.constructor [[Prototype]]
ConstructorFunction.prototype constructor
function Parent() {
//
}
Parent.prototype.parentMethod = function () {};
function Child() {
Parent.call(this); //
}
//
Child.prototype = Object.create(Parent.prototype);
Child constructor Child.prototype Parent
constructor [Symbol.species] extends
constructor Child.prototype.constructor Child create()
function Parent() {
//
}
function CreatedConstructor() {
Parent.call(this);
}
CreatedConstructor.prototype = Object.create(Parent.prototype);
CreatedConstructor.prototype.create = function () {
return new this.constructor();
};
new CreatedConstructor().create().create(); // TypeError: new CreatedConstructor().create().create undefined constructor === Parent
constructor Parent
function Parent() {
//
}
function CreatedConstructor() {
//
}
CreatedConstructor.prototype = Object.create(Parent.prototype, {
// Child
constructor: {
value: CreatedConstructor,
enumerable: false, // `for...in`
writable: true,
configurable: true,
},
});
CreatedConstructor.prototype.create = function () {
return new this.constructor();
};
new CreatedConstructor().create().create(); //
constructor constructor for...in
function Parent() {
//
}
function CreatedConstructor() {
//
}
Object.setPrototypeOf(CreatedConstructor.prototype, Parent.prototype);
CreatedConstructor.prototype.create = function () {
return new this.constructor();
};
new CreatedConstructor().create().create(); //
Object.setPrototypeOf() Parent CreatedConstructor
function ParentWithStatic() {}
ParentWithStatic.startPosition = { x: 0, y: 0 }; //
ParentWithStatic.getStartPosition = function () {
return this.startPosition;
};
function Child(x, y) {
this.position = { x, y };
}
Child.prototype = Object.create(ParentWithStatic.prototype, {
// Return original constructor to Child
constructor: {
value: Child,
enumerable: false,
writable: true,
configurable: true,
},
});
Child.prototype.getOffsetByInitialPosition = function () {
const position = this.position;
// getStartPosition this.constructor
const startPosition = this.constructor.getStartPosition();
return {
offsetX: startPosition.x - position.x,
offsetY: startPosition.y - position.y,
};
};
new Child(1, 1).getOffsetByInitialPosition();
// Error: this.constructor.getStartPosition is undefined, since the
// constructor is Child, which doesn't have the getStartPosition static method
Parent Child
//
Object.assign(Child, ParentWithStatic); // Notice that we assign it before we create() a prototype below
Child.prototype = Object.create(ParentWithStatic.prototype, {
// Return original constructor to Child
constructor: {
value: Child,
enumerable: false,
writable: true,
configurable: true,
},
});
//
function ParentWithStatic() {}
ParentWithStatic.startPosition = { x: 0, y: 0 }; //
ParentWithStatic.getStartPosition = function () {
return this.startPosition;
};
function Child(x, y) {
this.position = { x, y };
}
// Properly create inheritance!
Object.setPrototypeOf(Child.prototype, ParentWithStatic.prototype);
Object.setPrototypeOf(Child, ParentWithStatic);
Child.prototype.getOffsetByInitialPosition = function () {
const position = this.position;
const startPosition = this.constructor.getStartPosition();
return {
offsetX: startPosition.x - position.x,
offsetY: startPosition.y - position.y,
};
};
console.log(new Child(1, 1).getOffsetByInitialPosition()); // { offsetX: -1, offsetY: -1 }
Object.setPrototypeOf()
:
constructor constructor
| ECMAScript 2027 LanguageSpecification # sec-object.prototype.constructor |
Objectassign()create()defineProperties()defineProperty()entries()freeze()fromEntries()getOwnPropertyDescriptor()getOwnPropertyDescriptors()getOwnPropertyNames()getOwnPropertySymbols()getPrototypeOf()groupBy()hasOwn()is()isExtensible()isFrozen()isSealed()keys()preventExtensions()seal()setPrototypeOf()values()Object/Function| Web Proxy Viewer | New URL | Original Page |