Baseline Widely available
Object.create()
ë©ìëë ì§ì ë íë¡í íì
ê°ì²´ ë° ìì±(property)ì ê°ë ì ê°ì²´ë¥¼ ë§ëëë¤.
Object.create(proto[, propertiesObject])
매ê°ë³ì
proto
ìë¡ ë§ë ê°ì²´ì íë¡í íì ì´ì´ì¼ í ê°ì²´.
propertiesObject
ì íì¬í. ì§ì ëê³ undefined
ê° ìëë©´, ìì ì ìì±(ì¦, ìì²´ì ì ìëì´ ê·¸ íë¡í íì
ì²´ì¸ìì ì´ê±°ê°ë¥íì§ ìì ìì±)ì´ ì´ê±°ê°ë¥í ê°ì²´ë í´ë¹ ìì±ëª
ì¼ë¡ ìë¡ ë§ë ê°ì²´ì ì¶ê°ë ìì± ì¤ëª
ì(descriptor)를 ì§ì í©ëë¤. ì´ë¬í ìì±ì Object.defineProperties()
ì ë ë²ì§¸ ì¸ìì í´ë¹í©ëë¤.
ì§ì ë íë¡í íì ê°ì²´ì ìì±ì ê°ë ìë¡ì´ ê°ì²´.
ìì¸proto
매ê°ë³ìê° null
ëë ê°ì²´ê° ìë ê²½ì° TypeError
ìì¸ê° ë°ì(throw).
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); // super ìì±ì í¸ì¶.
}
// íìí´ëì¤ë ììí´ëì¤ë¥¼ íì¥
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var 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); // Outputs, 'Shape moved.'
ì¬ë¬ ê°ì²´ìì ììíê³ ì¶ì ê²½ì°ì mixinì´ ì¬ì©ê°ë¥í©ëë¤.
function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
}
MyClass.prototype = Object.create(SuperClass.prototype); // ìì
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin
MyClass.prototype.myMethod = function () {
// ê¸°ë¥ ìí
};
mixin
í¨ìë ìì(super)í´ëì¤ íë¡í íì
ìì íì(sub)í´ëì¤ íë¡í íì
ì¼ë¡ í¨ì를 ë³µì¬íê³ , mixin í¨ìë ì¬ì©ìì ìí´ ê³µê¸ë íìê° ììµëë¤. mixin ê°ì í¨ìì ìë jQuery.extend()ì
ëë¤.
Object.create()
ì í¨ê» propertiesObject
ì¸ì ì¬ì©í기
var o;
// íë¡í íì
ì´ nullì¸ ê°ì²´ ìì±
o = Object.create(null);
o = {};
// ìë ìëì ê°ìµëë¤:
o = Object.create(Object.prototype);
// ìí ìì± ëê°ë¥¼ ê°ë ê°ì²´ë¥¼ ë§ëë ì.
// (ë ë²ì§¸ 매ê°ë³ìë í¤ë¥¼ *ìì± ì¤ëª
ì*ì ë§µíí¨ì 주ìíì¸ì.)
o = Object.create(Object.prototype, {
// fooë ì ê· 'ê° ìì±'
foo: { writable: true, configurable: true, value: "hello" },
// barë ì ê·¼ì(accessor, getter-ë°-setter) ìì±
bar: {
configurable: false,
get: function () {
return 10;
},
set: function (value) {
console.log("Setting `o.bar` to", value);
},
/* ES5 ì ê·¼ìë¡ ì½ëë ì´ë ê² í ì ììµëë¤
get function() { return 10; },
set function(value) { console.log('setting `o.bar` to', value); } */
},
});
function Constructor() {}
o = new Constructor();
// ìë ìëì ê°ìµëë¤:
o = Object.create(Constructor.prototype);
// ë¬¼ë¡ , ìì±ì í¨ìì ì¤ì ì´ê¸°í ì½ëê° ìë¤ë©´
// Object.create()ë ê·¸ê²ì ë°ìí ì ììµëë¤
// ë¹ ì ê°ì²´ê° íë¡í íì
ì¸ ì ê°ì²´ë¥¼ ë§ë¤ê³
// ê°ì´ 42ì¸ ë¨ì¼ ìì± 'p' ì¶ê°.
o = Object.create({}, { p: { value: 42 } });
// 기본ì¼ë¡ writable, enumerable ëë configurable ìì±ì false:
o.p = 24;
o.p;
// 42
o.q = 12;
for (var prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false
// ES3 ìì±ì ì§ì í기 ìí´
o2 = Object.create(
{},
{
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true,
},
},
);
í´ë¦¬í
ì´ í´ë¦¬íììë ì ê°ì²´ì ëí íë¡í íì ì´ ì íëìì§ë§ ëë²ì§¸ ì¸ìê° ìì´ ê°ì²´ë¥¼ ìì±íë ì¬ë¡ë¥¼ ë³´ì¬ì¤ëë¤.
[[Prototype]]
ì null
ì ì¤ì íë ê²ì´ ì¤ì ES5 Object.create
ììë ì§ìëì§ë§, ECMAScript 5 ë³´ë¤ ë®ì ë²ì ììë ììì ì íì´ ì기 ë문ì ì´ í´ë¦¬íììë ì§ìí ì ììì 주ìíì¸ì.
if (typeof Object.create != "function") {
Object.create = (function (undefined) {
var Temp = function () {};
return function (prototype, propertiesObject) {
if (prototype !== Object(prototype) && prototype !== null) {
throw TypeError("Argument must be an object, or null");
}
Temp.prototype = prototype || {};
if (propertiesObject !== undefined) {
Object.defineProperties(Temp.prototype, propertiesObject);
}
var result = new Temp();
Temp.prototype = null;
// Object.create(null)ì¸ ê²½ì° ëª¨ë°©
if (prototype === null) {
result.__proto__ = null;
}
return result;
};
})();
}
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ì°¸ì¡°
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