ě´ęą°ëł´ęł prototype ě´í´ 몝í늴 ę°ěě ě ě°¸ęł
'ě ě ě'ëźęł ě´í´í ę˛
ëśëި Meë name = 'syong'ě ę°ě§. ëśëި Meě ěí´ íě´ë ěě Ië name = 'syong'ě.
// íëĄí íě
function Me () {
this.a = 'a';
this.b = 'b';
}
Me.prototype.name = 'syong';
const I = new Me();
console.log(I); // {a = 'a', b = 'b'}
console.log(I.name); // syong
ěíě˝ëŠ 2014 prototype ę°ě ě°¸ęł
prototypeě ě ěĽë ěěąë¤ě
ěěąě뼟 íľí´ě ę°ě˛´ę° ë§ë¤ě´ě§ ë
꡸ ę°ě˛´ě ě°ę˛°ëë¤.
[ ěě 1 ]
function Ultra () {}
Ultra.prototype.ultraProp = true;
function Super () {}
Super.prototype = new Ultra();
function Sub () {}
Sub.prototype = new Super();
Sub.prototype.ultraProp = 2;
var o = new Sub();
console.log(o.ultraProp); // 2
ěěąě Subě prototype ę°ě˛´
뼟 ë¤ě§ë¤. ěěąě Subě prototype ę°ě˛´ěě ultraProp íëĄíźí°ëĽź ë°ę˛Źíęł ęˇ¸ ę°ě ěśë Ľíë¤.[ ěě 2 ]
function Ultra () {}
Ultra.prototype.ultraProp = true;
function Super () {}
Super.prototype = new Ultra();
function Sub () {}
var s = new Super();
s.ultraProp = 3;
Sub.prototype = s;
var o = new Sub();
console.log(o.ultraProp); // 3
ěěąě Subě prototype ę°ě˛´
뼟 ë¤ě§ë¤. ěěąě Subě prototype ę°ě˛´ě ëłě sę° í ëšëě´ ěë¤. ëłě sěë ěěąě Superę° ë§ë ę°ě˛´ę° í ëšëě´ ěë¤. ꡸ ę°ě˛´ě prototype íëĄíźí°ę° 3ě´ëŻëĄ ě´ëĽź ěśë Ľíë¤.[ ěě 3 ]
function Ultra () {}
Ultra.prototype.ultraProp = true;
function Super () {}
var t = new Ultra();
t.ultraProp = 4;
Super.prototype = t;
function Sub () {}
var s = new Super();
Sub.prototype = s;
var o = new Sub();
console.log(o.ultraProp); // 4
ěěąě Superě prototype ę°ě˛´
뼟 ë¤ě§ë¤. ěě ę°ě ęłźě ě ęą°ěł 4뼟 ěśë Ľíë¤.đĄ 죟ě !
Sub.prototype = new Super(); // (o) Sub.prototype = Super.prototype; // (x)
ë ë˛ě§¸ě˛ëź ěěąí늴, Subě prototype ę°ě˛´ě ě´ë¤ íëĄíźí°ëĽź ěśę°í ë Superě prototype ę°ě˛´ë ěíĽě ë°ę˛ ëë¤. ěŚ, ěě ę°ě˛´ě ě´ë í 기ëĽě ěśę°í늴 ëśëި ę°ě˛´ěë ę°ě 기ëĽě´ ěśę°ëě´ ëŹ¸ě ę° ë°ěí ě ěë¤.
ěë°ě¤íŹëŚ˝í¸ë ěë°ě¤íŹëŚ˝í¸ę° ëěíë í¸ě¤í¸ í경ě´, ę°ë°ěë¤ěę˛ ę¸°ëł¸ě ěźëĄ ě ęłľíë, 미댏 ë§ë¤ě´ě ¸ ěë ę°ě˛´
Object, Function, Array, String, Boolean, Number, Math, Date, RegExp(ě ęˇííě)
// ěě 1 - ë°°ě´ ěě ëë¤íę˛ ę°ě ¸ě¤ę¸°
{
const arr = new Array('I', 'my', 'me', 'mine');
function getRandomValueFromArray () {
const index = Math.floor(arr.length * Math.random());
return arr[index];
}
console.log(getRandomValueFromArray());
}
// ěě 1 - íě¤ ë´ěĽ ę°ě˛´ě íěĽ
Array.prototype.random = function () {
const index = Math.floor(this.length * Math.random());
return this[index];
}
const arr = new Array('I', 'my', 'me', 'mine');
console.log(arr.random());
prototype
ě ě´ěŠí´ 모ë ë°°ě´ě ęłľíľě ěźëĄ ě¸ ě ěë API
뼟 ěŹěŠěę° ě§ě ě ěí ě ěë¤.
Array ěěąě í¨ěëĄ ë§ë 모ë ë°°ě´ ę°ě˛´ě random() ëŠěë뼟 ěŹěŠí ě ěëëĄ, í¨ěěěë arr ëě this
ëźęł ě¨ě¤ěź íë¤.