prototypeã§ãªãã¸ã§ã¯ãåããããã£ã¼ãå®ç¾©ãã¦ã¯ãããªã
function Class(){ } Class.prototype.p1=new Array();
ãã¦ä¸ã®ã³ã¼ãã«åé¡ããããã¨ããããã ãããï¼
- JavaScriptã§ã¯ã¯ã©ã¹å®ç¾©ã®ä»£ããã«prototypeã使ãã
- ãªãã¸ã§ã¯ãã¯èªä¿¡ã«å®ç¾©ããã¦ããªãããããã£ã¼ã¯prototypeãã§ã¼ã³ããã©ã£ã¦æ¤ç´¢ãããããèªä¿¡ã«å®ç¾©ããã¦ãããã®ããã«æ¯ãèãã
- Array.prototype.push ã¯ç ´å£çåä½ãããï¼èªåèªèº«ãå¤æ´ããï¼
ã¤ã¾ã
var instance=new Class(); var another_instance=new Class(); instance.p1.push("test"); alert(another_instance.p1.join(",") ); // ã³ã³
ãããæå¾
éãã®çµæããããããªãã®ã§ãããä¸è¨ã³ã¼ãã®3è¡ç®ã§ instance.p1ã«"test"ã追å ããããanother_instance.p1ã¯å¤æ´ããã¦ããªãã¯ããªã®ã§ join ãã¦ã空ã®æååããè¿ããªãã¯ãã ããªã®ã« another_instance.p1 ã«ã "test" ã追å ããã¦ããï¼
ã©ããã¦ãããªãã®ããinstance.p1 㯠å®éã«ã¯ãªãã¸ã§ã¯ã instance1 ã«ã¯å®ç¾©ããã¦ããªããå¾ã£ã¦ instance.p1 ã¯ãClass.prototype.p1 ãåç
§ãããanother_instance.p1 ãåæ§ã§ããã
è¦æ¹ãå¤ããã°ãnew Class ã«ãã£ã¦ä½æããããããããªãã¸ã§ã¯ã㯠Class.prototype.p1 ãå
±æããã®ã§ããã
ãã¦ãæå³ãããçµæãå°ãããã®ã§ããã°
function Class(){ this.p1=new Array(); }
ã¨ããªããã°ãªããªãã
ãã Array.prototype.push ãéç ´å£çåä½ãããã¤ã¾ããèªä¿¡ã®ãªã¹ãã®æå¾ã«ä¸ãããããªãã¸ã§ã¯ãã追å ãããã§ã¯ãªããèªä¿¡ã®ãªã¹ãã®æå¾ã«ãå¼æ°ã追å ãããèªä¿¡ã®ã³ãã¼ãè¿ããã¨ããåä½ãããé¢æ°ã§ãã£ãå ´åãã¤ã¾ãã³ã¼ãã
another_instance.p1 = another_instance.p1.push("hoge");
ã®æ§ã«ãªãå ´åã¯ãåé¡ã¯é¡å¨åããªãã