JavaScriptã§ï¼ã¯ã©ã¹ãç¶æ¿ããæ¹æ³ãï¼è¤æ°ã®ãµãã¯ã©ã¹ããå ±éã¯ã©ã¹ã®ãããã¿ã¤ããåç §ããï¼
JavaScriptã®ã³ã¼ãã£ã³ã°ã§ãªãã¸ã§ã¯ããæ±ãéï¼
ãã¤ã³ã¹ã¿ã³ã¹ãã¡ãã£ã¨ãã¤ç°ãªã£ã¦ãããã¨ããå ´åã¯ï¼ãã®èªç±åº¦ã«è¦åã£ãã¯ã©ã¹ï¼ï¼ãããã¿ã¤ãï¼ãï¼ã¤ã ãå®ç¾©ãã¦ããã°äºè¶³ããã
ãããï¼ããã«å ãã¦ãã¯ã©ã¹ãè¤æ°ãã£ã¦ï¼ã¡ãã£ã¨ãã¤ç°ãªã£ã¦ãããã¨ããå ´åï¼
è¤æ°ã®ã¯ã©ã¹ã®å ±éé¨åã親ã¯ã©ã¹ã«å®ç¾©ãï¼åã¯ã©ã¹ã§ç¶æ¿ããï¼ã¨ããæç¶ããå¿ è¦ã«ãªãã
親ã¯ã©ã¹ï¼åã¯ã©ã¹ï¼ã¤ã³ã¹ã¿ã³ã¹ï¼ã¨ããé ã«å®ç¾©ãå®è£
ããã¨ï¼ã³ã¼ãã®æµãã¯ä¸è¨ã®ããã«ãªãã
- (1)å ±éå®ç¾©
- â ã¯ã©ã¹éã§å ±éã®ãã¼ã¿ãå®ç¾©ããã
- (2)åå¥å®ç¾©ï¼ã¯ã©ã¹çæã³ã¼ãï¼
- â ã¯ã©ã¹éã§ç°ãªããã¼ã¿ãå®ç¾©ããã
- â¡åä¸ã¯ã©ã¹å ã®ã¤ã³ã¹ã¿ã³ã¹éã§å ±éã®ãã¼ã¿ãå®ç¾©ããã
- (3)ã¡ã¤ã³å¦çï¼ã¤ã³ã¹ã¿ã³ã¹çæã³ã¼ãï¼
- â åã¯ã©ã¹ãå©ç¨ãããåä¸ã¯ã©ã¹å ã®ã¤ã³ã¹ã¿ã³ã¹éã§ç°ãªããã¼ã¿ãå®ç¾©ããã
- â¡åã¤ã³ã¹ã¿ã³ã¹ãå©ç¨ããã
ãã®æµãã«æ²¿ã£ã¦ï¼JavaScriptã§ã¯ã©ã¹ã®ç¶æ¿ãè¡ãªããµã³ãã«ã³ã¼ãã以ä¸ã«æ²è¼ããã
æ¬æ¥è¨è¿°ãããã³ã¼ãã¯æµãã®æçµé¨åã®ï¼ï¼ï¼ã§ãã£ã¦ï¼ãã®ããã«ï¼ï¼ï¼ã¨ãï¼ï¼ï¼ã®ãããªä¸æºåãå¿ è¦ã«ãªãã
ãªã®ã§ï¼ï¼ï¼ï¼ããèªã¿é²ããã¨ããããã¦ã³ã§ãããã«ããã¨æãããã
ï¼ï¼ï¼ã®é¨åããèªã¿é²ããã¨ç解ãããããããããªãã
// ãã®è¡ã®ã³ã¡ã³ãã¢ã¦ããå¤ãã°ï¼jsãã¡ã¤ã«ãç´æ¥å®è¡ã§ãã¾ãã //function alert(s){ WScript.Echo(s) }; // ------------------- (1)å ±éå®ç¾© --------------------- // åç©ã®åºåºã¯ã©ã¹ // ï¼å ·ä½çãªåç©ã¯ã©ã¹ãå®ç¾©ããããã«ä½¿ãå ±éã®ã¯ã©ã¹ï¼ var BaseAnimal = function( class_init_hash ) { // ã¯ã©ã¹ã®å±æ§ãã¾ã¨ãã¦å®ç¾© this.type_name = class_init_hash[ "type_name" ]; this.legs_num = class_init_hash[ "legs_num" ]; }; BaseAnimal.prototype = { // ã¯ã©ã¹ãã¨ã«ç°ãªãçµæãè¿ããã describe_animal_type : function() { alert( "種é¡ã¯" + this.type_name + "ï¼è¶³ã¯" + this.legs_num + "æ¬ã§ãã" ); } , // ã¤ã³ã¹ã¿ã³ã¹ãã¨ã«ç°ãªãçµæãè¿ããã introduce : function() { alert( "ååã¯" + this.personal_name + "ï¼å¹´ã¯" + this.age + "æ³ã§ãã" ); } }; // å ·ä½çãªåç©ã¤ã³ã¹ã¿ã³ã¹ã®å ±éã³ã³ã¹ãã©ã¯ã¿ var common_constructor_for_animal_instance = function( instance_init_hash ) { // ã¤ã³ã¹ã¿ã³ã¹ã®å±æ§ãã¾ã¨ãã¦åæå this.personal_name = instance_init_hash[ "personal_name" ]; this.age = instance_init_hash[ "age" ]; }; // ------------------- (2)åå¥å®ç¾©ï¼ã¯ã©ã¹çæã³ã¼ãï¼ --------------------- // äºã¯ã©ã¹ var Turtle = function( instance_init_hash ) { // åç©ã¤ã³ã¹ã¿ã³ã¹ã®å ±éã³ã³ã¹ãã©ã¯ã¿ãï¼ // æ°è¦ãªãã¸ã§ã¯ãã®ããã«å¼ã³åºã common_constructor_for_animal_instance.call( this, instance_init_hash ); } Turtle.prototype = new BaseAnimal({ type_name : "äº", legs_num : 4 }); // ãããã¯ã©ã¹ var Rabbit = function( instance_init_hash ) { // åç©ã¤ã³ã¹ã¿ã³ã¹ã®å ±éã³ã³ã¹ãã©ã¯ã¿ãï¼ // æ°è¦ãªãã¸ã§ã¯ãã®ããã«å¼ã³åºã common_constructor_for_animal_instance.call( this, instance_init_hash ); // ãããã¯æ¯ã®è²ãåæåæã«æå®ã§ããäºã«ãã this.color = instance_init_hash[ "color" ] } Rabbit.prototype = new BaseAnimal({ type_name : "ããã", legs_num : 4 }); // ãããã¯è·³ãããã¨ãå¯è½ã¨ãã Rabbit.prototype.jump = function() { alert(this.personal_name + "ãã¸ã£ã³ããã¾ããã"); } // ------------------- (3)ã¡ã¤ã³å¦çï¼ã¤ã³ã¹ã¿ã³ã¹çæã³ã¼ãï¼ --------------------- // äº var kame1 = new Turtle({ personal_name : "äºå¤«", age : 1 }); var kame2 = new Turtle({ personal_name : "äºå", age : 2 }); // ã¯ã©ã¹ã«åºæã®ããããã£ãå¼ã³åºã kame1.describe_animal_type(); // 種é¡ã¯äºï¼è¶³ã¯4æ¬ã§ãã kame2.describe_animal_type(); // 種é¡ã¯äºï¼è¶³ã¯4æ¬ã§ãã // ã¤ã³ã¹ã¿ã³ã¹ã«åºæã®ããããã£ãå¼ã³åºã kame1.introduce(); // ååã¯äºå¤«ï¼å¹´ã¯1æ³ã§ãã kame2.introduce(); // ååã¯äºåï¼å¹´ã¯2æ³ã§ãã // ããã var usa1 = new Rabbit({ personal_name : "ãã夫", age : 3, color : "ç½" }); var usa2 = new Rabbit({ personal_name : "ããå", age : 4, color : "é»" }); // ã¯ã©ã¹ã«åºæã®ããããã£ãå¼ã³åºã usa1.describe_animal_type(); // 種é¡ã¯ãããï¼è¶³ã¯4æ¬ã§ãã usa2.describe_animal_type(); // 種é¡ã¯ãããï¼è¶³ã¯4æ¬ã§ãã // ã¤ã³ã¹ã¿ã³ã¹ã«åºæã®ããããã£ãå¼ã³åºã usa1.introduce(); // ååã¯ãã夫ï¼å¹´ã¯3æ³ã§ãã usa2.introduce(); // ååã¯ããåï¼å¹´ã¯4æ³ã§ãã // ãã®åç©ã¯ã©ã¹ã«åºæã®ã¡ã½ãããå¼ã¹ã usa1.jump(); // ãã夫ãã¸ã£ã³ããã¾ããã usa2.jump(); // ããåãã¸ã£ã³ããã¾ããã
ããã ã¢ããã§è§£èª¬ãæ²è¼ã
ï¼ï¼ï¼â ï¼åä¸ã¯ã©ã¹å ã®ã¤ã³ã¹ã¿ã³ã¹éã§ç°ãªããã¼ã¿ãå®ç¾©ããããã«ã¯
åã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã«ããã·ã¥ã渡ãããã«ããã
newæã«ï¼ãã®ããã·ã¥ã®å
容ã«å¿ããæ°è¦ãªãã¸ã§ã¯ããè¿ããã¨ãã§ããã
ï¼ï¼ï¼â¡ï¼åä¸ã¯ã©ã¹å ã®ã¤ã³ã¹ã¿ã³ã¹éã§å ±éã®ãã¼ã¿ãå®ç¾©ããããã«ã¯
ï¼ãéçãåæåã®å ´åï¼
åã¯ã©ã¹ã®prototypeå ã«è¨è¿°ããã°ããã
ããã¯ã©ã¹å
ã®å
¨ã¦ã®ã¤ã³ã¹ã¿ã³ã¹ãï¼åä¸ã®prototypeãå
±æããã®ã§ã
ï¼ãåçãåæåã®å ´åï¼
åã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿å
ã«å¦çãæ¸ãã°ããã
ï¼ï¼ï¼â ï¼ã¯ã©ã¹éã§ç°ãªããã¼ã¿ãå®ç¾©ããããã«ã¯
åã¯ã©ã¹éã§ï¼prototypeã®å 容ãç°ãªãã°ããã
ãã ãï¼ããã¯ã©ã¹éã§å
±éåãããprototypeã®ã³ãã¼ã使ã£ã¦ããå ´åã¯ï¼
該å½ããprototypeã®å®ç¾©å¾ã«ï¼ãããè¨æ³ã«ãã£ã¦å·®åãã追è¨ãããäºã«ãã£ã¦å·®ç°åããã
â»ããããã¯è·³ãããã¨ãå¯è½ã¨ãããã®é¨åã
ï¼ï¼ï¼â ï¼ã¯ã©ã¹éã§å ±éã®ãã¼ã¿ãå®ç¾©ããããã«ã¯
ããããã¢ã
赤åã®ï¼ç¹ããã¤ã³ãã«ãªãã
ï¼ãéçãåæåã®å ´åï¼
ï¼aï¼è¦ªã¯ã©ã¹ã«ã¯ã©ã¹éã®å
±éã®ãã¼ã¿ãæ¸ãï¼
ï¼bï¼ããã¦åã¯ã©ã¹ã親ã¯ã©ã¹ãç¶æ¿ããã°ããã
å ·ä½çã«è¨ãã¨
- ï¼aï¼ããã¯ã©ã¹ãåºåºã¯ã©ã¹ã¨èãããã¨ã«ãï¼åºåºã¯ã©ã¹ã®prototypeå ã«ãã¯ã©ã¹éå ±éãã¼ã¿ããæ¸ãã
- ï¼bï¼newã§çã¾ããåºåºã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãï¼åã¯ã©ã¹ã®prototypeã«ä»£å ¥ããã
ããããã°ï¼ããåãã«è¦ãããã©ãªãã¸ã§ã¯ãã®å®ä½ã¯ç°ãªãããããªprototypeããè¤æ°çæãããã¨ãã§ããã
ã¤ã¾ãï¼äºãã«ç¬ç«ãã¦ã«ã¹ã¿ãã¤ãºå¯è½ãªããã£ããããã¯ã©ã¹ããè¤æ°å®ç¾©ã§ããã
ãããã¿ã¤ãç¶æ¿ã«ããã¯ã©ã¹ã®ç¶æ¿ ã JavaScript ã«ãããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°
http://keicode.com/script/scr24f.php
- 親ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãï¼åã¯ã©ã¹ã®prototypeã«ä»£å ¥ããã
- ããã®æ¸ãæ¹ã«ãã£ã¦ã åã¯ã©ã¹ ã® prototype ãªãã¸ã§ã¯ã㨠親ã¯ã©ã¹ ã® prototype ãªãã¸ã§ã¯ãããªã³ã¯ããã¾ããã
- ã¤ã¾ãï¼ãæ製ã®ãããã¿ã¤ããã§ã¼ã³ãä½ã£ãï¼ã¨ãããã¨ã
Javascriptã®ç¶æ¿
http://sein-und-zeit.seesaa.net/artic...
- Mozillaã®Javascript解説ã§ãããªã©ã¤ãªã¼ã®ä¾ã®æ¬ã§ãåæ§ã«è¡ãªãããã¦ããç¶æ¿æ¹æ³ãã B.prototype = new A;
ï¼ãåçãåæåã®å ´åï¼
åã¯ã©ã¹éã§ï¼ã³ã³ã¹ãã©ã¯ã¿ãå ±æããã°ããã
ãã®ããã®ã²ã¨ã¤ã®æ¹æ³ã¯ï¼
- åã¯ã©ã¹éã§å ±éã®ã¤ã³ã¹ã¿ã³ã¹åæåå¦çãç¬èªã®é¢æ°ã¨ãã¦å®ç¾©ãï¼
- ãã®é¢æ°ãï¼åã¯ã©ã¹ã®å®ã³ã³ã¹ãã©ã¯ã¿å ã§ï¼callãapplyä»ãã§å¼ã³åºãã
ãªãcallãapplyãå¿
è¦ãã¨ããã¨ï¼newä»ãã§å¼ã³åºãããé¢æ°ï¼ï¼ã³ã³ã¹ãã©ã¯ã¿ï¼ã®å
é¨ã§ã¯thisã®æå³ãå¤ããï¼thisãæ°è¦ãªãã¸ã§ã¯ããæãããã«ãªãããã
JavaScriptã§ï¼ãªãã¸ã§ã¯ããã¯ã©ã¹ã®åæ©ãç解ãã¦ãããï¼å®åã確ãããããã®ï¼ã¤ã®è³ªåãï¼ãµã³ãã«ã³ã¼ãä»ãï¼
http://language-and-engineering.hatenablog.jp/entry/20100921/p1
ï¼åç®ã®è§£èª¬ãåç §
ã¯ã©ã¹ã®ç¶æ¿ã®ä»æ¹
http://oshiete.goo.ne.jp/qa/1617352.html
- ãã³ã³ã¹ãã©ã¯ã¿ãã親ã®ã³ã³ã¹ãã©ã¯ã¿ãå¼ã³åºããã¨ã§ç¶æ¿ãã¾ãããã ãããã®é this ã®ã¡ã½ããã¨ãã¦å¼ã°ãªãã¨ã親ã³ã³ã¹ãã©ã¯ã¿ã§ã®åæåãèªåã«åæ ããã¾ãããã
è£è¶³
ä¸è¨ã®ä¾ãè¶ ãã¦æ¬æ ¼çã«ããå ´åã¯ï¼ä»¥ä¸ã®amachangã®ãã¼ã¸ã¨ï¼ãããã辿ããã¦ãããªã³ã¯ãå ¨é¨çèªããå¿ è¦ãããã
æ¬å½ã¯ï¼ã¯ã©ã¹ãªãã¦ãã®ã¯ãªãã
ãããã¿ã¤ããã¼ã¹ã®ç¶æ¿ï¼é¢æ°ä¸çºã§ãããã¿ã¤ããã§ã¼ã³ã«ç¹ãã¦ããªãã¸ã§ã¯ããã¯ãã¼ã³ãããï¼
http://d.hatena.ne.jp/amachang/200610...
ã¨ã¯è¨ã£ã¦ãï¼
- ã·ã³ãã«ãªè¨è¨ã§çç£æ§ã®é«ãWebã¢ããªéçºãè¡ãªããã¨ãç®çã§ï¼
- ãµã¼ããµã¤ããã·ã¹ãã å¢çå ã§ããï¼
- Javaã®ããã¯ã°ã©ã¦ã³ããæã£ãæè¡è ãéããããç¶æ³
ã¨ãããããªå ´åï¼ä»ååãä¸ãããµã³ãã«ã§äºè¶³ããã¨æãããã
ï¼ãã¯ã©ã¹ãã¨ããè¨èã使ã£ã¦èª¬æãã¦ããã®ã¯ãã®ããï¼
ã