[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor [Back]  [Original]

Object.prototype.constructor - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.prototype.constructor

Baseline

20157

constructor Object

: JavaScript constructor

Object.prototype.constructor

null [[Prototype]] Array

js
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

js
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

js
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

constructor

js
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]]

js
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

js
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

js
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()

js
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

js
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

Object.setPrototypeOf()

js
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

js
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

js
// 
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,
  },
});
// 

extends

js
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


Web Proxy Viewer  |  New URL  |  Original Page