Object.create()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
Object.create() ã¯éçã¡ã½ããã§ãæ¢åã®ãªãã¸ã§ã¯ããæ°ããçæããããªãã¸ã§ã¯ãã®ãããã¿ã¤ãã¨ãã¦ä½¿ç¨ãã¦ãæ°ãããªãã¸ã§ã¯ããçæãã¾ãã
試ãã¦ã¿ã¾ããã
const person = {
isHuman: false,
printIntroduction() {
console.log(`ç§ã¯ ${this.name} ã§ããç§ã¯äººéã§ããããï¼ ${this.isHuman}`);
},
};
const me = Object.create(person);
me.name = "Matthew"; // "name" 㯠"me" ã«è¨å®ããããã®ã§ã "person" ã«ã§ã¯ãªã
me.isHuman = true; // ç¶æ¿ãããããããã£ã¯ä¸æ¸ãå¯è½
me.printIntroduction();
// äºæ³ãããçµæ: "My name is Matthew. Am I human? true"
æ§æ
Object.create(proto)
Object.create(proto, propertiesObject)
弿°
proto-
æ°ãã«çæããããªãã¸ã§ã¯ãã®ãããã¿ã¤ãã«ãªãã¹ããªãã¸ã§ã¯ãã§ãã
propertiesObjectçç¥å¯-
æå®ããã¦ãã¦ã
undefinedã§ãªãå ´åãããèªèº«ã®åæå¯è½ãªããããã£ãããããã®ããããã£åãä¼´ãä¸é£ã®ããããã£è¨è¿°åãæå®ããæ°ãã«çæããããªãã¸ã§ã¯ãã«è¿½å ããããã¨ã«ãªãã¾ãããããã®ããããã£ã¯ãObject.defineProperties()ã® 2 çªç®ã®å¼æ°ã«å¯¾å¿ãããã®ã§ãã
è¿å¤
æå®ãããããã¿ã¤ããªãã¸ã§ã¯ãã¨ããããã£ãæã¤æ°ãããªãã¸ã§ã¯ãã§ãã
ä¾å¤
ä¾
>Object.create() ãç¨ããå¤å ¸çãªç¶æ¿
以ä¸ã®ä¾ã¯ãå¤å
¸çãªç¶æ¿ãããããã® Object.create() ã®ä½¿ç¨æ¹æ³ã§ããããã¯ããã¹ã¦ã® JavaScript ã対å¿ãã¦ããåä¸ç¶æ¿ã§ãã
// Shape - ã¹ã¼ãã¼ã¯ã©ã¹
function Shape() {
this.x = 0;
this.y = 0;
}
// ã¹ã¼ãã¼ã¯ã©ã¹ã®ã¡ã½ãã
Shape.prototype.move = function (x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};
// Rectangle - ãµãã¯ã©ã¹
function Rectangle() {
Shape.call(this); // call super constructor.
}
// ãµãã¯ã©ã¹ã¯ã¹ã¼ãã¼ã¯ã©ã¹ãæ¡å¼µãã
Rectangle.prototype = Object.create(Shape.prototype, {
// Rectangle.prototype.constructor ã Rectangle ã«è¨å®ããªãã¨ã
// Shape (親) ã® prototype.constructor ãåããã¨ã«ãªãã¾ãã
// ãããé²ãããã«ã prototype.constructor ã Rectangle (å) ã«è¨å®ãã¾ãã
constructor: {
value: Rectangle,
enumerable: false,
writable: true,
configurable: true,
},
});
const rect = new Rectangle();
console.log("Is rect an instance of Rectangle?", rect instanceof Rectangle); // true
console.log("Is rect an instance of Shape?", rect instanceof Shape); // true
rect.move(1, 1); // 'Shape moved.' ã¨åºå
create() ã使ç¨ããéã«ã¯ã確å®ã«é©åãªæå³ã¥ããããããã« constructor ããããã£ã追å ãç´ããªã©ã®æ³¨æç¹ããããã¨ã«æ³¨æãã¦ãã ããã Object.create() 㯠Object.setPrototypeOf() ã§ãããã¿ã¤ãã夿´ãããããããã©ã¼ãã³ã¹ãè¯ãã¨ä¿¡ãããã¦ãã¾ãããã¤ã³ã¹ã¿ã³ã¹ã使ããã¦ããããããããã£ã¢ã¯ã»ã¹ãã¾ã æé©åããã¦ããªãå ´åããã®å·®ã¯å®éã«ã¯ãããããã§ããç¾è¡ã® JavaScript ã³ã¼ãã§ã¯ãã©ã®ãããªå ´åã§ã class æ§æãæ¨å¥¨ãã¾ãã
Object.create() 㨠propertiesObject 弿°ã®ä½µç¨
Object.create() ã¯ãªãã¸ã§ã¯ãã®ä½æããã»ã¹ãç´°ããå¶å¾¡ãããã¨ãã§ãã¾ãããªãã¸ã§ã¯ãåæåæ§æã¯ Object.create()ã®ç³è¡£æ§æã§ãã Object.create() ã使ãã¨ãæå®ãããããã¿ã¤ãã¨ããããã£ãæã¤ãªãã¸ã§ã¯ãã使ãããã¨ãã§ãã¾ãã 2 ã¤ç®ã®å¼æ°ã¯ ããããã£è¨è¿°å ã«å²ãå½ã¦ããããã¼ã§ãããã¨ã«æ³¨æãã¦ãã ãããããã¯ããªãã¸ã§ã¯ãåæååã§ã¯ã§ããªããåããããã£ã®åæå¯è½æ§ãè¨å®å¯è½æ§ãªã©ãå¶å¾¡ã§ãããã¨ãæå³ãã¦ãã¾ãã
o = {};
// ããã¯æ¬¡ã®ãã®ã¨åç
o = Object.create(Object.prototype);
o = Object.create(Object.prototype, {
// foo ã¯æ®éã®ãã¼ã¿ããããã£
foo: {
writable: true,
configurable: true,
value: "hello",
},
// bar ã¯ã¢ã¯ã»ãµã¼ããããã£
bar: {
configurable: false,
get() {
return 10;
},
set(value) {
console.log("Setting `o.bar` to", value);
},
},
});
// ãããã¿ã¤ããæ°è¦ã®ç©ºãªãã¸ã§ã¯ãã§ããæ°ãããªãã¸ã§ã¯ãã
// çæããåä¸ã®ãããã㣠'p' ãå¤ã 42 ã«ãã¦è¿½å ãã¾ãã
o = Object.create({}, { p: { value: 42 } });
Object.create() ã使ç¨ããã¨ã null ããããã¿ã¤ãã¨ãããªãã¸ã§ã¯ãã使ãããã¨ãã§ãã¾ãããªãã¸ã§ã¯ãã®åæååã§ã¯ã __proto__ ãã¼ãåçã®æ§æã«ãªãã¾ãã
o = Object.create(null);
// Is equivalent to:
o = { __proto__: null };
æ¢å®ã§ã¯ãããããã£ã¯æ¸ãè¾¼ã¿å¯è½ã§ããåæå¯è½ã§ããè¨å®å¯è½ã§ãããã¾ããã
o.p = 24; // 峿 ¼ã¢ã¼ãã§ã¯ä¾å¤çºç
o.p; // 42
o.q = 12;
for (const prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false; 峿 ¼ã¢ã¼ãã§ã¯ä¾å¤çºç
åæååã¨åã屿§ãæã¤ããããã£ãæå®ããã«ã¯ãwritableãenumerableãconfigurable ãæç¤ºçã«æå®ãã¦ãã ããã
o2 = Object.create(
{},
{
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true,
},
},
);
// ããã¯æ¬¡ã®ãã®ã¨ç価ã§ãã
// o2 = Object.create({ p: 42 })
// which will create an object with prototype { p: 42 }
Object.create() ã使ç¨ã㦠new æ¼ç®åã®åä½ã模å£ãããã¨ãã§ãã¾ãã
function Constructor() {}
o = new Constructor();
// 次ã®ãã®ã¨ç価ã§ãã
o = Object.create(Constructor.prototype);
ãã¡ããã constructor 颿°ã®ä¸ã«å®éã®åæåã³ã¼ããããå ´åã Object.create() ã¡ã½ããã«ãããåæ ããããã¨ã¯ã§ãã¾ããã
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-object.create> |