- 1.ãJSã®ãã¹ãŠããªããžã§ã¯ãã§ããããšããåŸæ¥ã®ç¥æµã«ãããããã-ããã¯ããã§ã¯ãããŸãããããã°ã©ããŒãå©çšã§ãã6ã€ã®ããŒã¿åã®ãã¡ã5ã€ãããªããã£ãã§ããã1ã€ã ãããªããžã§ã¯ãåãè¡šãããšãããããŸããã
- 2.ãªããžã§ã¯ãã«ã€ããŠãããã¯ããŒãšå€ã®ãã¢ãå«ããã®ãããªããŒã¿æ§é ã§ããããšãåŠã³ãŸããã å€ã¯ãä»»æã®ããŒã¿åïŒãªããžã§ã¯ãã®ããããã£ã«ãªããŸãïŒãŸãã¯é¢æ°ïŒãªããžã§ã¯ãã®ã¡ãœããã«ãªããŸãïŒã®ããããã§ãã
- 3.ããããããªããã£ãã¯ãªããžã§ã¯ãã§ã¯ãããŸããã ããªãã¯ãããããªããžã§ã¯ããšããŠæ±ãããšãã§ããŸããïŒãããŠããã¯ããªããã£ãããªããžã§ã¯ãã§ãããšèª€è§£ãæããŸãïŒãããã...
- 4.å€æ°ã¯ãåçŽïŒãªãã©ã«ïŒïŒvar a = 'str'ïŒãšã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ïŒã©ãããŒïŒïŒvar a = new StringïŒ 'str'ïŒïŒã®äž¡æ¹ã§å®£èšã§ããŸãã 2çªç®ã®ã±ãŒã¹ã§ã¯ãããªããã£ããååŸããã®ã§ã¯ãªããStringïŒïŒã³ã³ã¹ãã©ã¯ã¿ãŒã«ãã£ãŠäœæããããªããžã§ã¯ããååŸããŸãã ïŒæ°ããéæ³ã®æŒç®åã®çš®é¡ãšãåŸã§èŠã€ããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšã¯ïŒã
- 5.ããªããã£ãïŒæ°ããæååïŒ 'str'ïŒïŒã§ã©ãããŒãäœæããããšã§ããªããžã§ã¯ãã®ããã«æäœã§ããããšãããããŸããã ã€ã³ã¿ããªã¿ããªããžã§ã¯ããšããŠæäœããããšãããšãã«ããªããã£ãã®åšãã«äœæããã®ã¯ãã®ã©ãããŒã§ãããæäœãå®äºãããšç Žæ£ãããŸãïŒãããã£ãŠãããªããã£ãã¯å²ãåœãŠãããããããã£ãèšæ¶ã§ããªããªããŸãa.test = 'test'-ãã¹ãããããã£ã¯ã©ãããŒãšãšãã«æ¶ããŸãïŒ
- 6.ãªããžã§ã¯ãã«ã¯ããªããžã§ã¯ãã®æååè¡šçŸãè¿ãtoStringïŒïŒã¡ãœãããããããšãåŠã³ãŸããïŒtype number valueOfïŒïŒã®å Žåãæ°å€ãè¿ããŸãïŒã
- 7.é£çµæäœãŸãã¯æ°åŠçæäœãå®è¡ãããšããããªããã£ãã¯ãã®åãç®çã®åã«åå®çŸ©ã§ããããšã«æ°ä»ããŸããã ãããè¡ãã«ã¯ããããã®ã¿ã€ãã®ã©ãããŒé¢æ°ã䜿çšããŸãããnewæŒç®åã¯äœ¿çšããŸããïŒstr = StringïŒstrïŒïŒïŒéãã¯äœã§ããïŒ
- 8.ãããŠæåŸã«ãtypeofã¯åºå®ãããããŒãã«ããå€ãååŸããããšãåŠã³ãŸããïŒããã¯typeof null //ãªããžã§ã¯ãã«åºã¥ãå¥ã®èª€è§£ãããããæ¥ãŠããå Žæã§ãïŒã
ä»æ¥ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšprototypeããããã£ã®æäœãããã³ããããçæãããªããžã§ã¯ããš__proto__ããããã£ã®æäœã«ã€ããŠã話ããããšæããŸãã ããã¯ãJSã§ç¶æ¿ãæŽçããJSã§çºçããããã»ã¹ã®äžéšãç解ããããšããå Žåã«ãã°ãã°å¿ èŠã§ãã ãŸãããããã¿ã€ãç¶æ¿ã®äžéšã®ç解ãæäŸããŸãã 解æã®äŸãšããŠïŒãã¹ãŠã®äººã«æ確ã§ã¯ãªããããããŸãããïŒã次ã®ã³ãŒããèŠãŠã¿ãŸãããã
äŸ1ïŒ
function A() {} A.prototype.x = 10; a = new A(); console.log(ax); //10 console.log(ay); //undefined A.prototype.y = 20; console.log(ay); //20 /* A.prototype.<> */ 2: function B() {} B.prototype.x = 10; b = new B(); console.log(bx); //10 console.log(by); //undefined B.prototype = {constructor:B, x:10, y:20}; console.log(bx); //10 console.log(by); //undefined /* B.prototype = {constructor:B, x:10, y:20}; */ b1 = new B(); console.log(b1.x); //10 console.log(b1.y); //20 /* */ b instanceof B; //false b1 instanceof B //true /* */
ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã¯ããªããžã§ã¯ããšããŠããããã¿ã€ãã§ãã
é çªã«å§ããŸãããã äžèŠã·ã³ãã«ãªã³ãŒãè¡ãèããŠã¿ãŸãããã
function A() {}
æåã«èšããããšã¯ããAãšããååã®é¢æ°ã宣èšããŸãããã§ãã ããã§ãã ãããã埮åŠãªéãããããŸãã
1. JSã§ã¯ãã»ãšãã©ãã¹ãŠããªããžã§ã¯ãã§ããããšãå¿ããªãã§ãã ããã å€æããããã«ãé¢æ°ã¯äŸå€ã§ã¯ãããŸããïŒãããã¯ãªã³ã¯ã§ãªã³ã¯ããã2ã€ã®ãªããžã§ã¯ãã§ãïŒã
2.ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšããŠäœ¿çšã§ããŸãã
JavaScriptã«ã¯äžè¬ã«ã¯ã©ã¹ãšåŒã°ãããã®ã¯ãããŸããã JavaScriptã®ã¯ã©ã¹ã¯ãç¹å®ã®æå®ãããããããã£ãæã€ãªããžã§ã¯ããäœæããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã«ãã£ãŠå®è¡ãããŸãã
äžè¬ã«ãJSã®é¢æ°ãªããžã§ã¯ãã¯ã³ã³ã¹ãã©ã¯ã¿ãŒã«ãªããŸãïŒãŠãŒã¶ãŒå®çŸ©é¢æ°ã«ã€ããŠè©±ããŸãïŒã ãããã¯æ¡ä»¶ä»ãã§3ã€ã«åå²ã§ããŸãïŒDFïŒé¢æ°å®£èšïŒãFEïŒé¢æ°åŒïŒãFunctionïŒïŒã³ã³ã¹ãã©ã¯ã¿ãŒã«ãã£ãŠäœæãããé¢æ°ïŒã ãããã®æ©èœã¯ãã¹ãŠç¬èªã®ç¹æ§ãæã£ãŠããŸãïŒãã®ãããç°ãªãã°ã«ãŒãã«åããããŠããŸãïŒããããã§ãããã«ã€ããŠã¯èª¬æããŸãããèå³ããã人ã¯å人çã«çããããå¥ã®æ©äŒã«åå¥ã«æžããŸãã ãã ãããããã«ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒã«ãªãå ±éç¹ã1ã€ãããŸããå éš[[Construct]]ããããã£ãš[[Call]]ããããã£ã®ã»ããæ瀺çãªãããã¿ã€ãããããã£ïŒããã«ã€ããŠïŒã§ãã
æ°ãããªããžã§ã¯ãã«ã¡ã¢ãªãå²ãåœãŠãŠåæåããã®ã¯ãå éšã¡ãœãã[[Construct]]ã§ãã ãã ããããã¯é¢æ°åŒã³åºãããªããžã§ã¯ãã®äœæã«ã€ãªããããšãæå³ãããã®ã§ã¯ãããŸããããã¡ããããã§ã¯ãããŸããã ãããè¡ãã«ã¯ãé¢æ°ãåŒã³åºãåã«newæŒç®åãåŒã³åºããŸãã [[Construct]]ã¡ãœãããšé¢é£ããã»ã¹ãèµ·åããæ°ãããã®ã§ãã
function A(){} A(); // var a = new A(); // -. (a) - (A)
3.ããã¯é¢æ°å®£èšïŒDFïŒãªã©ãšèšãããšãã§ããŸãããæ®ãã¯ãŸã éèŠã§ã¯ãããŸããã
ãããã£ãŠããæåã®äŸã®æåã®è¡ããã®ããAãé¢æ°ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ããã³ããŒãã¿ã€ã ãªããžã§ã¯ãã§ãã ããã¯ãªããžã§ã¯ãã§ãããããããããã£ãæã€ããšãã§ããŸãã ããã§ãã ãŸããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã§ãããããprototypeããããã£ããããŸãã prototypeããããã£ã¯ããã®ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã«ãã£ãŠäœæãããã€ã³ã¹ã¿ã³ã¹ã«ç§»åããããããã£ãšã¡ãœãããæ ŒçŽãããªããžã§ã¯ããžã®åç §ã§ãã ããããã¹ãŠãã°ã©ãã£ã«ã«ã«è¡šç€ºããŠã¿ãŸãããã
ããã©ã«ãã§ã¯ããããã¿ã€ããªããžã§ã¯ãã¯ã空ãã§ãïŒãŸããã»ãšãã©ç©ºã§ããã以äžã§ããã«è©³ãã説æããŸãïŒã äžèšã§ãç§ã¯ãã®ãªããžã§ã¯ãã«ãããã¹ãŠã®ãã®ãã€ã³ã¹ã¿ã³ã¹ã«å ¥ããåå«ã«ãå©çšå¯èœã«ãªããšèšããŸããã ã€ãŸããããã©ã«ãã§ã¯ïŒãããã¿ã€ãã«äœãè¿œå ãããŠããªãå ŽåïŒãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãAãããã€ã³ã¹ã¿ã³ã¹ã«äœã移åããŸããã ã€ãŸããã³ãŒããå®è¡ãããšãïŒ
function A(){} var a = new A();
ãéåžžã®ãïŒJSã§å¯èœãªéãïŒãªããžã§ã¯ããaããååŸããŸãã
JSã«ã¯ãã§ã«å€ãã®çµã¿èŸŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ããããŸãã ããã¯ãããšãã°ãNumberïŒïŒãStringïŒïŒãªã©ã§ãããã®äŸããå°ãè±ç·ããŠãçµã¿èŸŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšäžè¬çãªãªããžã§ã¯ãã«ã€ããŠè©±ããŸãããã
ãªããžã§ã¯ãïŒ__ proto__ïŒã
ååã®èšäºãããçµã¿èŸŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒNumberïŒïŒãStringïŒïŒããŸãã¯BooleanïŒïŒã®ããããã䜿çšããŠïŒæ瀺çãŸãã¯æé»çã«ïŒãªããžã§ã¯ããäœæãããšãã€ã³ã¹ã¿ã³ã¹ã¯ãã®ã¿ã€ãã«åºæã®ã¡ãœããã«ã¢ã¯ã»ã¹ã§ããããšãããããŸãã ããšãã°ãNumberïŒïŒã«ã¯toPrecisionïŒïŒã¡ãœããããããŸãã ã³ã³ã¹ãã©ã¯ã¿ãŒnew NumberïŒ2ïŒã«ãã£ãŠäœæããããªããžã§ã¯ãã®ã³ã³ãœãŒã«ãèŠããšãããã«ãã®ã¡ãœããã¯èŠã€ãããŸããïŒã¡ãœããã¯ãŸã£ããèŠã€ãããŸããïŒã 圌ã¯ã©ãããæ¥ãã®ã§ããïŒ èŠªã®ãããã¿ã€ããªããžã§ã¯ãã«å«ãŸããã®ã¯ã圌ãšåæ§ã®ã¡ãœããïŒåå«ãã¢ã¯ã»ã¹ããå¿ èŠãããã¡ãœããïŒã§ãã ããããã€ã³ã¹ã¿ã³ã¹ã¯ã©ã®ããã«ãããã«ã¢ã¯ã»ã¹ããŸããïŒ ã€ã³ã¹ã¿ã³ã¹ã«ã¯__proto__ããããã£ããããŸã-ããã¯èŠªã®ãããã¿ã€ããªããžã§ã¯ããžã®åç §ã§ãã ã¡ãœãããåŒã³åºããšãã«ã¡ãœãããã€ã³ã¹ã¿ã³ã¹èªäœã«ãªãå Žåã__ proto__ãªã³ã¯ã¯èŠªãããã¿ã€ããªããžã§ã¯ãã«ç§»åããããã§æ€çŽ¢ãç¶è¡ãããŸãã å®éãããã¯nullãèŠã€ãããŸã§ç¹°ãè¿ãããŸãã
ããããã¹ãŠæããŠã¿ãŸãããã
èŠçŽãããšããããŸã§ã®ãšãããã¹ãŠãè€éã§ã¯ãªããšããããšãã§ããŸãïŒãããã¿ã€ãããããã£ã«ãåå«ãã¢ã¯ã»ã¹ãããã¹ãŠã®ã¡ãœãããšããããã£ãæ ŒçŽãããç¹å®ã®ãªããžã§ã¯ããžã®ãªã³ã¯ãæã€èŠªïŒã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ïŒããããŸãã ãããŠå®éã«ã¯ãæ°ããåŒã³åºãã«ãã£ãŠäœæããããšãã«ãå ±éã®ããããã£ãšã¡ãœãããæã€åããªããžã§ã¯ãã®èŠªãã__proto__ããããã£ãžã®ãªã³ã¯ãæž¡ãããåå«ããããŸãã
çµ±åããããã«ãäŸãèããŠã¿ãŸãããã
function A() {} // - ( «» prototype) A.prototype.x = 10;// prototype( ) (x) 10 a = new A(); // __proto__ prototype (x==10) console.log(ax); // (x) (a), __proto__ prototype. console.log(ay); // (y) . A.prototype.y = 20; // (y) prototype( ) console.log(ay); //20 // __proto__
ã³ã³ã¹ãã©ã¯ã¿ãŒã
ç§ã¯ãã空ã®ããããã¿ã€ããšèšã£ããšããåžžã«ã空ã®ããšããèšèãåŒçšç¬Šã§å²ã¿ãŸãã åæ§ã«ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°function AïŒïŒ{}ãäœæãããšãã空ã®ããããã¿ã€ããªããžã§ã¯ããåç §ããŠãããã¿ã€ãããããã£ãäœæãããŸãã ããã§ããªãã ãããã¿ã€ãã«ã¯ãŸã äœãããããŸãã 第äžã«ããããã¿ã€ãã¯ãåçŽãªããªããžã§ã¯ãã§ãããããObjectïŒïŒã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã®ãããã¿ã€ããžã®åç §ãæã€__proto__ããããã£ã¯ããã«ãããŸãïŒãã¹ãŠã®ãåçŽãªãæãåºæ¬çãªãªããžã§ã¯ããäœæããŸãïŒãã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ããããŸãã ã³ã³ã¹ãã©ã¯ã¿ãŒã¯ããªããžã§ã¯ãã ãã§ãªããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãäœæãããŠããããšãèªèãããšãã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ãããã«è¿œå ããŸãæåã«ãããã2ã€ã®äºå®ã念é ã«çœ®ããŠæåã®æç»ãè£å®ããŸãããã
ç°è²ã§æãããŠãããã®ã¯ãã¹ãŠãä»ã®ãšããæ¬åœã«å¿ èŠãããŸãããããã¯ãããå®å šãªç»åãåŸãããã§ãã ã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ã«æ³šç®ããŸãããã å³ãããããããã«ãã³ã³ã¹ãã©ã¯ã¿ãŒã¯ããã®ããªããžããªãŒããã€ãŸããã®ãªããžã§ã¯ããæåã«äœæãããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°èªäœãæããŸãã ã€ãŸããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã®ãããã¿ã€ãããããã£ãšãããã¿ã€ããªããžã§ã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ã®éã«ã¯ã埪ç°æ§ãçŸããŸãããããã¯äºãã®ãªããžã§ã¯ããæããŸãã
ã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ã䜿çšããŠïŒã³ã³ã¹ãã©ã¯ã¿ãŒããŸã ãã€ã³ãããã³ã³ã¹ãã©ã¯ã¿ãŒã®ãããã¿ã€ãããããã£ãå ã®ãããã¿ã€ãããã€ã³ãããŠããå ŽåïŒããªããžã§ã¯ããããã¿ã€ããžã®åç §ãéæ¥çã«ååŸã§ããŸãïŒa.constructor.prototype.xã ãŸããã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°èªäœãšããããã¿ã€ããªããžã§ã¯ãã§ã¯ãªããå ·äœçã«å²ãåœãŠããããã®ããããã£ãžã®ãªã³ã¯ãååŸã§ããŸãã äŸïŒ
function A(){} A.own = 'I am A!'; // â a = new A(); a.own //undefined a.constructor.own // 'I am A!';
= {}-ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšããŠïŒæ°ãããªããžã§ã¯ãïŒïŒïŒã
çŽ æŽãããããã¹ãŠãé©åã«èœã¡ãããã§ãã ãå ±éã¹ãã¬ãŒãžããããã芪ãšåã«ã¯ãã®ã¹ãã¬ãŒãžãžã®ãªã³ã¯ããããŸããããããã£ãã€ã³ã¹ã¿ã³ã¹èªäœã«ãªãå Žåãã€ã³ã¿ãŒããªã¿ãŒã¯ãªã³ã¯ãã¯ãªãã¯ããŠãå ±éã¹ãã¬ãŒãžããæ¢ããŸãã ãã£ããã¯äœã§ãã?? äŸ2ãèŠãŠã¿ãŸãããã
function B() {} B.prototype.x = 10; b = new B(); console.log(bx); //10 console.log(by); //undefined B.prototype = {constructor:B, x:10, y:20}; console.log(bx); //10 console.log(by); //undefined
ãã¹ãŠãæ©èœããããã§ãã ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãäœæãããå ±éã¹ãã¬ãŒãžãïŒãããã¿ã€ãïŒãªã³ã¯çµç±ïŒïŒããããã£ïŒxïŒãèšå®ããã€ã³ã¹ã¿ã³ã¹ãäœæããããããã£ïŒxïŒããããŸã-ãã¹ãŠåé¡ãããŸããã 次ã«ãéåžžããããã¿ã€ãã®èŠªããããã£ãåå®çŸ©ããããããã£ïŒxïŒãšïŒyïŒãè¿œå ããŠæ£ããã³ã³ã¹ãã©ã¯ã¿ã瀺ããŸããã ãã¹ãŠããããã®ããããã£ã®äž¡æ¹ã®ãå ±æãªããžããªãã§åäœããã¯ãã§ãããããããïŒyïŒã€ã³ã¿ããªã¿ã¯ãããèŠã€ããŸããã WTFïŒïŒïŒ
ããã§ã¯ã©ã®ãããªéæ³ãèµ·ãã£ãŠããŸããïŒ ãã®ã³ã³ã¹ãã©ã¯ã¿ã®åå«ãããããã®å€æŽã衚瀺ãããªãã®ã¯ãªãã§ããïŒ åå«ã«yã衚瀺ãããªãã®ã¯ãªãã§ããïŒ ãŸããæåã«ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ïŒBïŒã®ãããã¿ã€ãããããã£ããªãŒããŒã©ã€ãããæ°ãããªããžã§ã¯ããåç §ãå§ããŸãïŒå ã®ãããã¿ã€ããªããžã§ã¯ããšã®æ¥ç¶ãåæãããŸãïŒã 第äºã«ãã€ã³ã¿ããªã¿ã«ããvar a = {}ãªã©ã®ãªããžã§ã¯ãå€æ°ã®éåžžã®å²ãåœãŠã¯ãå®éã«ã¯var a = new ObjectïŒïŒãšããŠå®è¡ãããŸãã ããã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã®ãããã¿ã€ãããããã£ã«ãã³ã³ã¹ãã©ã¯ã¿ãŒãªã³ã¯ãæ¬ èœããŠããå®å šã«æ°ãããªããžã§ã¯ããå«ãŸããããã«ãªãã芪ã倱ãããªãããã«ãã³ã³ã¹ãã©ã¯ã¿ãŒããããã£ãåå¥ã«è¿œå ããŠã芪ãå²ãåœãŠãŸãã
ãŸããå ã»ã©äœæããã€ã³ã¹ã¿ã³ã¹ã«ã¯ãããããã£ïŒyïŒããªãå€ããããã¿ã€ããªããžã§ã¯ããžã®ãªã³ã¯__proto__ãå«ãŸããŠããŸãã ã€ãŸããäŸ1ãšã¯ç°ãªããããã§ã¯ããªããžããªã«ããããã£ãè¿œå ããããããªããžããªãå床æžãæãããããšããããæ°ãããã®ãäœæããå€ããã®ãšã®æ¥ç¶ãåæããŸããããã€ã³ã¹ã¿ã³ã¹ã¯ããã«ã€ããŠäœãç¥ãããå€ããã®ãåŒãç¶ã䜿çšããŸã__proto__ããªã³ã¯ããŸãã 次ã®ããã«ãªããŸãã
é»-ããã¯ãB.prototype = {constructorïŒBãxïŒ10ãyïŒ20};ã®åŸã«å€æŽãããŠããªããã®ã§ãã
èµ€-äœãæ¶ãã
ç·-è¿œå ããããã®
instanceofã«ã€ããŠå°ãè¿œå ããããšãã§ããŸãã å¥åŠãªããšã«ããã®äŸã§ã¯ãb1ã¯ã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°Bã«å±ããbã¯ããã§ã¯ãããŸããã ãã¹ãŠãéåžžã«ç°¡åã§ãã å®éã«ã¯ãinstanceofã¯æ¬¡ã®æ¡ä»¶ãæºãããŠããããšãæ¢ããŠããŸã-ãªã³ã¯__proto __ïŒãã§ãŒã³ã®ä»»æã®ã¬ãã«ïŒã§æå®ããããªããžã§ã¯ãïŒçªå·1ã®åïŒã¯ãç®çã®èŠªã®ãããã¿ã€ãããããã£ã«ãã£ãŠåç §ããããªããžã§ã¯ãïŒçªå·2ã®åïŒãšåãã§ãïŒå³ã®é»ãšæ¯èŒããŠãã ããïŒè²ãšç·ïŒã é»ã§ã¯ãã®æ¡ä»¶ã¯æºããããªããªããç·ã§ã¯æºããããŸãã
ç§ãã¡ã®å Žåãã€ã³ã¹ã¿ã³ã¹ïŒbïŒã§ã¯ãç®çã®èŠªïŒBïŒã®æ°ãããããã¿ã€ãããããã£ã以åã§ã¯ãªãæ°ãããªããžã§ã¯ãããã§ã«åç §ããŠããããããã®æ¥ç¶ã¯åæãããŸãã ããããç§ãã¡ãèŠãããã«ããããæã€ã€ã³ã¹ã¿ã³ã¹ïŒb1ïŒã¯æŽç¶ãšããŠããŸãã
åŸ
ããã«é¢ããŠã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ã®æ¬äœã«ã¯è§ŠããŸãããããã«ã€ããŠã¯ã次ã®èšäºã§è©³ãã説æããŸãã ç§ãèšãå¯äžã®ããšã¯ãïŒnewãä»ããŠïŒã³ã³ã¹ãã©ã¯ã¿ãŒãšããŠé¢æ°ãåŒã³åºããšãã¯ãäœæãããã€ã³ã¹ã¿ã³ã¹ãæããé¢æ°ãšããŠåŒã³åºããšãã¯ã°ããŒãã«ãªããžã§ã¯ããæããšããããšã§ãã
äŸãèŠãŠã¿ãŸãããïŒ
function A(str){ this.val = str; } a = new A('test'); // val , this A('test'); // this â . val console.log(val) //'test'
é¢æ°ãã©ã®ããã«åŒã³åºããããã調ã¹ãæ¹æ³ã¯ïŒ æ°ãããã©ããã§ïŒ ããã¯éåžžã«ç°¡åã«è¡ãããŸãïŒ
function A(str){ if( this instanceof A) // this A( new) this.val = str; // val else retur str; //( new) }
ãã®ããã«ããŠãåå€æã®ã¡ã«ããºã ãå®è£ ãããŸãã ããšãã°ã1 + '1'ãå®è¡ãããšãã€ã³ã¿ãŒããªã¿ãŒã¯+ãã¹ããªã³ã°é£çµãšããŠè§£éããæ°å€1ãã¹ããªã³ã°ã«ãã£ã¹ãããããšããŸãã ããã¯ãStringïŒ1ïŒïŒnewãªãïŒã®æé»çãªåŒã³åºãã§çºçããŸãã ãŸããStringã®ã³ã³ã¹ãã©ã¯ã¿ãŒã«ã¯ãäžèšãšåãæ§æã«ã€ããŠèšè¿°ãããŠããŸãã ã€ãŸããåŒã³åºããnewãªãã§çºçããå ŽåãåçŽã«æååãè¿ããŸãïŒtoStringïŒïŒã¡ãœããã®æé»çãªåŒã³åºãïŒã ãããã£ãŠããªããžã§ã¯ããäœæããã«ãåå€æãçºçããŸãã
ãŸããããããã£ãé¢æ°ïŒã€ãŸãããããã¿ã€ãã§ã¯ãªãé¢æ°ïŒã«è¿œå ããã«ã¯ããªããžã§ã¯ããšããŠã¢ã¯ã»ã¹ããå¿ èŠããããŸãã äŸãã°
function A(){} A.val = 'str';
ãã®ããããã£ã¯ãããã¿ã€ãã«ååšãããåå«ã¯ããã«ã®ã¿ã¢ã¯ã»ã¹ã§ãããããåå«ã«ã¯ã¢ã¯ã»ã¹ã§ããŸããã ãããã圌ãããæ¬åœã«æããªããã§ããããšèšãããã«ã ããã¯ããããã¿ã€ããªããžã§ã¯ãã®ããããã£ã䟿å©ãªå Žæã§ãã ç§ãã¡ãæãåºãããã«ãããã¯é¢æ°èªäœãæããŸãïŒãã¡ãããããã¯ç¹ã«å€æŽãããŠããªãéãïŒã 次ã«ãå€æ°valãååŸããã«ã¯ã次ã®ããã«ã¢ã¯ã»ã¹ããå¿ èŠããããŸãã
function A(){} A.val = 'str'; a = new A(); a.constructor.val; //'str'
ãœãŒã¹ïŒ
https://developer.mozilla.org
http://dmitrysoshnikov.com/
http://javascript.ru