Baseline Widely available
constructor
㯠Object
ã¤ã³ã¹ã¿ã³ã¹ã®ãã¼ã¿ããããã£ã§ãã¤ã³ã¹ã¿ã³ã¹ãªãã¸ã§ã¯ããçæããã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°ã¸ã®åç
§ãè¿ãã¾ãããªãããã®ããããã£ã®å¤ã¯é¢æ°ãã®ãã®ã¸ã®åç
§ã§ããã颿°åãå«ãã æååã§ã¯ããã¾ããã
ã¡ã¢: ãã㯠JavaScript ãªãã¸ã§ã¯ãã®ããããã£ã§ããã¯ã©ã¹ã® constructor
ã¡ã½ããã«ã¤ãã¦ã¯ããªãã¡ã¬ã³ã¹ãã¼ã¸ãåç
§ãã¦ãã ããã
ã¤ã³ã¹ã¿ã³ã¹ãªãã¸ã§ã¯ãã使ããã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°ã¸ã®åç §ã
æ¸è¾¼å¯è½ å¯ åæå¯è½ ä¸å¯ è¨å®å¯è½ å¯ã¡ã¢: ãã®ããããã£ã¯ãæ¢å®ã§ã¯ãã¹ã¦ã®ã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°ã® prototype
ããããã£ã«ä½æããããã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ä½æããããã¹ã¦ã®ãªãã¸ã§ã¯ãã«ç¶æ¿ããã¾ãã
ï¼null
ãããã¿ã¤ããªãã¸ã§ã¯ããé¤ãï¼ã©ããªãªãã¸ã§ã¯ããããã® [[Prototype]]
ã«ã³ã³ã¹ãã©ã¯ã¿ã¼ã®ããããã£ãæã£ã¦ãã¾ããä¾ãã°ãé
åãªãã©ã«ã¯ Array
ãªãã¸ã§ã¯ãã使ãããªãã¸ã§ã¯ããªãã©ã« ã¯ãã¬ã¼ã³ãªãã¸ã§ã¯ãã使ãã¾ãã
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 ããããã£ã®ä»£å ¥
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
ã«ã¼ãã§åç
§ããããã¨ã¯ãªããªãã¾ãã
ä¸è¨ã®ã³ã¼ããå®åæã®ããã«è¦ããå ´åã¯ã Object.setPrototypeOf()
ã使ç¨ãã¦ãããã¿ã¤ããã§ã¼ã³ãæä½ãããã¨ãã§ãã¾ãã
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,
},
});
// â¦
ããããããã«è¯ããã¨ã«ãã¯ã©ã¹ã® extends
ã®ããã«ãã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°èªä½ãäºãã«æ¡å¼µããããã¨ãã§ãã¾ãã
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
使ç¨ããããåå²ãå½ã¦ã®å¿
è¦ã¯ããã¾ããã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4