Baseline Widely available
Object.create()
éææ¹æ³ä»¥ä¸ä¸ªç°æå¯¹è±¡ä½ä¸ºååï¼å建ä¸ä¸ªæ°å¯¹è±¡ã
const person = {
isHuman: false,
printIntroduction: function () {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
},
};
const me = Object.create(person);
me.name = "Matthew"; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // Inherited properties can be overwritten
me.printIntroduction();
// Expected output: "My name is Matthew. Am I human? true"
è¯æ³
Object.create(proto)
Object.create(proto, propertiesObject)
åæ°
proto
æ°å建对象çåå对象ã
propertiesObject
å¯é
å¦æè¯¥åæ°è¢«æå®ä¸ä¸ä¸º undefined
ï¼åè¯¥ä¼ å
¥å¯¹è±¡å¯æä¸¾çèªæå±æ§å°ä¸ºæ°å建ç对象添å å
·æå¯¹åºå±æ§åç§°ç屿§æè¿°ç¬¦ãè¿äºå±æ§å¯¹åºäº Object.defineProperties()
ç第äºä¸ªåæ°ã
æ ¹æ®æå®çåå对象å屿§åå»ºçæ°å¯¹è±¡ã
å¼å¸¸ ç¤ºä¾ ç¨ 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); // è°ç¨ç¶ç±»æé 彿°ã
}
// å类继æ¿ç¶ç±»
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("rect æ¯ Rectangle ç±»çå®ä¾åï¼", rect instanceof Rectangle); // true
console.log("rect æ¯ Shape ç±»çå®ä¾åï¼", rect instanceof Shape); // true
rect.move(1, 1); // æå° 'Shape moved.'
éè¦æ³¨æçæ¯ï¼ä½¿ç¨ create()
乿ä¸äºè¦æ³¨æçå°æ¹ï¼æ¯å¦éæ°æ·»å constructor
屿§ä»¥ç¡®ä¿æ£ç¡®çè¯ä¹ãè½ç¶ Object.create()
被认为æ¯ä½¿ç¨ Object.setPrototypeOf()
ä¿®æ¹ååæ´å
·ææ§è½ä¼å¿ï¼ä½å¦ææ²¡æå建å®ä¾å¹¶ä¸å±æ§è®¿é®è¿æ²¡æè¢«ä¼åï¼å®ä»¬ä¹é´çå·®å¼å®é
䏿¯å¯ä»¥å¿½ç¥ä¸è®¡çãå¨ç°ä»£ä»£ç ä¸ï¼æ 论å¦ä½é½åºè¯¥ä¼å
使ç¨ç±»è¯æ³ã
Object.create()
æ¹æ³å
许对对象å建è¿ç¨è¿è¡ç²¾ç»çæ§å¶ãå®é
ä¸ï¼åé¢éåå§åå¯¹è±¡è¯æ³æ¯ Object.create()
çä¸ç§è¯æ³ç³ãä½¿ç¨ Object.create()
ï¼æä»¬å¯ä»¥å建å
·ææå®åååæäºå±æ§ç对象ã请注æï¼ç¬¬äºä¸ªåæ°å°é®æ å°å°å±æ§æè¿°ç¬¦ï¼è¿æå³çä½ è¿å¯ä»¥æ§å¶æ¯ä¸ªå±æ§ç坿䏾æ§ãå¯é
ç½®æ§çï¼èè¿å¨åé¢éåå§åå¯¹è±¡è¯æ³ä¸æ¯åä¸å°çã
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);
// çä»·äºï¼
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 })
// åè
å°å建ä¸ä¸ªåå为 { p: 42 } ç对象ã
ä½ å¯ä»¥ä½¿ç¨ Object.create()
æ¥æ¨¡ä»¿ new
è¿ç®ç¬¦çè¡ä¸ºã
function Constructor() {}
o = new Constructor();
// çä»·äºï¼
o = Object.create(Constructor.prototype);
å½ç¶ï¼å¦æ Constructor
彿°ä¸æå®é
çåå§å代ç ï¼é£ä¹ Object.create()
æ¹æ³å°±æ æ³åæ å®ã
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