ã¯ããã«
JavaScript Gardenã¯ãJavaScriptèšèªã®æãå¥åŠãªæ©èœã«é¢ããããã¥ã¡ã³ãã®ã³ã¬ã¯ã·ã§ã³ã§ãã ããããééãã埮åŠãªãã°ãããã©ãŒãã³ã¹ã®åé¡ãééã£ãããã°ã©ãã³ã°ã¹ã¿ã€ã«ãåé¿ããããã®ãã³ããããã€ã玹ä»ããŸãã
ããã¯èšèªã®æç§æžã§ã¯ãããŸããã ãã§ã«èšèªãç¥ã£ãŠããããšãåæãšããŠããŸãã èšèªãã¬ãŒãã³ã°ã«ã€ããŠã¯ããã°ãããæ¬ã Expressive JavaScript ãã®ãã°ããã翻蚳ã䜿çšããããšããå§ãããŸã ã
ãªããžã§ã¯ã
ãªããžã§ã¯ãã®äœ¿çšãšããããã£
JSã§ã¯ããã¹ãŠã®ãšã³ãã£ãã£ã¯ãªããžã§ã¯ãã®ããã«åäœããŸããã2ã€ã®äŸå€ããããŸãïŒnullãšundefinedã
false.toString(); // 'false' [1, 2, 3].toString(); // '1,2,3' function Foo(){} Foo.bar = 1; Foo.bar; // 1
æ°å€ããªããžã§ã¯ããšããŠäœ¿çšã§ããªããšããã®ã¯äžè¬çãªèª€è§£ã§ãã ããŒãµãŒã®äžå®å šæ§ã«ãããæ°å€ã®åŸã®ããããæµ®åå°æ°ç¹æ°ã®ã¬ã³ãŒããšããŠè§£æããããšããŸãã
2.toString(); // SyntaxError
ããã«ã¯ããã€ãã®æ¹æ³ããããŸãã
2..toString(); // 2 .toString(); // (2).toString(); // 2
ããŒã¿åãšããŠã®ãªããžã§ã¯ã
ãªããžã§ã¯ãã¯ããã·ã¥ïŒããŒå€ãªã¹ãïŒãšããŠã䜿çšã§ããŸãã ãããã¯äž»ã«ãå€ããã€ã³ããããååä»ãããããã£ã§æ§æãããŸãã
{}ã䜿çšãããšã空ã®ãªããžã§ã¯ããäœæã§ããŸãã Object.prototypeãç¶æ¿ããç¬èªã®ããããã£ã¯ãããŸããã
var foo = {}; // // 'test' , 12 var bar = {test: 12};
ããããã£ãžã®ã¢ã¯ã»ã¹
ã¢ã¯ã»ã¹ã¯2ã€ã®æ¹æ³ã§ååŸã§ããŸã-ãããçµç±ãŸãã¯è§æ¬åŒ§çµç±ã
var foo = {name: ''} foo.name; // foo['name']; // var get = 'name'; foo[get]; // foo.1234; // SyntaxError foo['1234']; //
2ã€ã®æ¹æ³ã®éãã¯ãæ¬åŒ§ã«ãã£ãŠããããã£åã®åçãªå²ãåœãŠãšäœ¿çšãå¯èœã«ãªãããšã§ãã
ããããã£ã®åé€
ããããã£ã¯ãdeleteæŒç®åã§ã®ã¿åé€ã§ããŸãã æªå®çŸ©ãŸãã¯nullãå²ãåœãŠããšãããããã£ã®å€ã®ã¿ãåé€ãããããããã£èªäœã¯åé€ãããŸããã
var obj = { bar: 1, foo: 2, baz: 3 }; obj.bar = undefined; obj.foo = null; delete obj.baz; for(var i in obj) { if (obj.hasOwnProperty(i)) { console.log(i, '' + obj[i]); } }
ã³ãŒãã¯bar undefinedãšfoo nullã®äž¡æ¹ãåºåããŸãããbazã¯åºåããŸãã;åé€ãããŸããã
äºçŽèª
var test = { 'case': ' , ', delete: ' , !' // SyntaxError };
ãªããžã§ã¯ãã®ããããã£ã®ååã¯ãåçŽãªå€æ°ããã³æååãšããŠèšè¿°ã§ããŸãã å¥ã®ããŒãµãŒã®æ¬ é¥ã«ãããã³ãŒãã¯SyntaxECãšã©ãŒãçºçãããŸãïŒECMAScript 5ãŸã§ã®ããŒãµãŒã®å ŽåïŒã deleteã¯äºçŽèªã§ãããããæååãšããŠèšè¿°ããå¿ èŠããããŸãã
ãããã¿ã€ã
JSã«ã¯å€å žçãªç¶æ¿ã¢ãã«ããããŸãããã代ããã«ãããã¿ã€ãããããŸãã ããã¯ãã°ãã°èšèªã®æ¬ é¥ãšèŠãªãããŸãããå®éã«ã¯ãã®ã¢ãã«ã¯å€å žçãªã¢ãã«ããã匷åã§ãã ããšãã°ããããã¿ã€ãã³ã°ã«åºã¥ããŠå€å žçãªã¢ãã«ãæ§ç¯ããã®ã¯ååã«ç°¡åã§ãããéãåæ§ã§ãã
JSã¯ãããã¿ã€ããç¶æ¿ããå¯äžã®äžè¬çãªèšèªã§ãããããåŸæ¥ã®ã¢ãããŒãããåãæ¿ããéã«æ £ãããŸã§ã«æéããããå ŽåããããŸãã æåã®éãïŒJSã®ç¶æ¿ã§ã¯ããããã¿ã€ããã§ãŒã³ã䜿çšãããŸãã
function Foo() { this.value = 42; } Foo.prototype = { method: function() {} }; function Bar() {} // Bar Foo Bar.prototype = new Foo(); Bar.prototype.foo = 'Hello World'; // , Bar Bar.prototype.constructor = Bar; var test = new Bar(); // Bar // test [instance of Bar] Bar.prototype [instance of Foo] { foo: 'Hello World' } Foo.prototype { method: ... } Object.prototype { toString: ... /* etc. */ }
泚ïŒBar.prototype = Foo.prototypeã䜿çšããå Žåãäž¡æ¹ã®ãªããžã§ã¯ãã«åäžã®ãããã¿ã€ãããããŸãã 次ã«ããªããžã§ã¯ãã®ãããã¿ã€ãã®å€æŽã¯ãä»ã®ãªããžã§ã¯ãã®å€æŽã«ã€ãªãããŸãããéåžžãããã¯åœŒããéæããŠããããšã§ã¯ãããŸããã
äžèšã®ã³ãŒãã§ã¯ããã¹ããªããžã§ã¯ãã¯Bar.prototypeãšFoo.prototypeã®äž¡æ¹ããç¶æ¿ãããŠãããããFooã«å¯ŸããŠå®çŸ©ãããã¡ãœããé¢æ°ã«ã¢ã¯ã»ã¹ã§ããŸãã ãŸãããããã¿ã€ãã§ããFooã€ã³ã¹ã¿ã³ã¹ã®valueããããã£ã«ãã¢ã¯ã»ã¹ããŸãã æ°ããBarïŒïŒã¯Fooã®æ°ããã€ã³ã¹ã¿ã³ã¹ãäœæããã®ã§ã¯ãªãããããã¿ã€ãã«å²ãåœãŠããããã®ãåå©çšããããšã«æ³šæããããšãéèŠã§ãã ãããã£ãŠãBarã®ãã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ã¯åãå€ããããã£ãå ±æããŸãã
Bar.prototype = Fooã䜿çšããªãã§ãã ãããFooãããã¿ã€ãã§ã¯ãªããFooãªããžã§ã¯ããæããŸãã ãããã£ãŠããããã¿ã€ããã§ãŒã³ã¯ãFoo.prototypeã§ã¯ãªãFunction.prototypeãééããŸãã ãããŠããã®ã¡ãœããã¯ãããã¿ã€ããã§ãŒã³ã«ã¯å«ãŸããŸããã
ç©ä»¶æ€çŽ¢
ãªããžã§ã¯ãã®ããããã£ã«ã¢ã¯ã»ã¹ãããšãJSã¯æå®ãããååã®ããããã£ãèŠã€ãããŸã§ãã§ãŒã³ããã©ããŸãã
圌ããããã«å°éããå Žåãããªãã¡ Object.prototypeãŸã§ã§ãèŠã€ãããªãå Žåã¯undefinedãè¿ããŸãã
ãããã¿ã€ãããããã£
ãã§ãŒã³ã®æ§ç¯ã«äœ¿çšãããŸãããä»»æã®å€ãå²ãåœãŠãããšãã§ããŸãã ãã ããããªããã£ãã®å²ãåœãŠã¯ç¡èŠãããŸãã
function Foo() {} Foo.prototype = 1; // ,
ãããããªããžã§ã¯ãã®å²ãåœãŠã¯æ©èœãããããã¿ã€ããã§ãŒã³ã®åçãªäœæã«ã€ãªãããŸãã
æ§èœ
ãã§ãŒã³ã®æäžéšã§ããããã£ãèŠã€ããã®ã¯æéãããããŸãã ããã«ãååšããªãããããã£ã«å°éããããšãããšãåžžã«ãã§ãŒã³å šäœãééããŸãã ããã«ããªããžã§ã¯ãã®ããããã£ãæž¡ããšããã§ãŒã³å ã®åããããã£ã«çªå·ãå²ãåœãŠãããŸãã
çµã¿èŸŒã¿ãããã¿ã€ãã®æ¡åŒµ
æ¡åŒµåObject.prototypeãŸãã¯ãã®ä»ã®çµã¿èŸŒã¿ãããã¿ã€ããäžé©åã«äœ¿çšãããå ŽåããããŸãã ããã¯ãç¿ãããããšåŒã°ãããã®ææ³ã¯ã«ãã»ã«åãç ŽããŸãã Prototypeã®ãããªäžè¬çãªãã¬ãŒã ã¯ãŒã¯ã§äœ¿çšãããŠããŸãããçµã¿èŸŒã¿åã®ç®è©°ãŸããç¹å®ããçç±ã¯ãããŸããã
çµã¿èŸŒã¿ãããã¿ã€ããæ¡åŒµã§ããå¯äžã®çç±ã¯ãæ°ããJSãšã³ãžã³ã®èŠçŽ ïŒArray.forEachãªã©ïŒãèšèªã«åã蟌ãããšã§ãã
ãŸãšã
ãããã¿ã€ãç¶æ¿ã¢ãã«ã䜿çšããŠè€éãªã³ãŒããèšè¿°ããåã«ããããã¿ã€ãç¶æ¿ã¢ãã«ãç解ããããšãéèŠã§ãã ãã§ãŒã³ã®é·ããèŠããŠãå¿ èŠã«å¿ããŠããããå£ããŸãã æ°ããJSæ©èœãšã®äºææ§ã®ããã«ãããè¡ããªãéããçµã¿èŸŒã¿ãããã¿ã€ããæ¡åŒµããå¿ èŠã¯ãããŸããã
hasOwnProperty
ãããã¿ã€ãã®1ã€ã§ã¯ãªãããªããžã§ã¯ãã«çŽæ¥ããããã£ãååšãããã©ããã確èªããããã«äœ¿çšãããŸãã Object.prototypeã®ãã¹ãŠã®ãªããžã§ã¯ãã«ãã£ãŠç¶æ¿ãããŸãã
æªå®çŸ©ã®ããããã£ããã§ãã¯ããã ãã§ã¯äžååã§ãã ããããã£ã¯ååšããããšãã§ããåæã«æªå®çŸ©ã«ããããšãã§ããŸãã
hasOwnPropertyã¯ãããããã£ã§åäœãããããã¿ã€ããã§ãŒã³ãééããªãèšèªã§å¯äžã®ãã®ã§ãã
// Object.prototype Object.prototype.bar = 1; var foo = {goo: undefined}; foo.bar; // 1 'bar' in foo; // true foo.hasOwnProperty('bar'); // false foo.hasOwnProperty('goo'); // true
hasOwnPropertyãšããååã®ããããã£ã¯èšèªä¿è·ãããŠããŸããã ãªããžã§ã¯ãã¯ãã®ååã®ããããã£ãæã€ããšãã§ãããããhasOwnPropertyå€éšã¡ãœããã䜿çšããŠæ£ããçµæãååŸããå¿ èŠããããŸãã
var foo = { hasOwnProperty: function() { return false; }, bar: ' ' }; foo.hasOwnProperty('bar'); // false // hasOwnProperty , 'this' foo ({}).hasOwnProperty.call(foo, 'bar'); // true // hasOwnProperty Object prototype Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
ãŸãšã
hasOwnPropertyã®äœ¿çšã¯ããªããžã§ã¯ãã«ããããã£ããããã©ãããå€æããå¯äžã®ä¿¡é Œã§ããæ¹æ³ã§ãã å€ãã®å Žåããªããžã§ã¯ãã®ããããã£ããã©ããŒã¹ãããšãã«äœ¿çšããããšããå§ãããŸãã
forã«ãŒã
inæŒç®åãšåæ§ã«ãfor inã«ãŒãã¯ããªããžã§ã¯ãã®ããããã£ãèµ°æ»ãããšãã«ãããã¿ã€ããã§ãŒã³ãééããŸãã ã«ãŒãã¯ãåæå¯èœãªå±æ§ãfalseã§ããããããã£ïŒããšãã°ãé åã®é·ãããããã£ãªã©ïŒãééããŸããã
// Object.prototype Object.prototype.bar = 1; var foo = {moo: 2}; for(var i in foo) { console.log(i); // bar moo }
ãªããªã for inæäœãå€æŽããããšã¯äžå¯èœã§ãããã«ãŒãã®æ¬äœã®äžèŠãªããããã£ãé€å€ããå¿ èŠããããŸãã ECMAScript 3ã§ã¯ãObject.prototypeã®hasOwnPropertyã¡ãœãããããã«äœ¿çšãããŸãã
ECMAScript 5ã§ã¯ãenumerableãfalseã®å ŽåãObject.definePropertyã䜿çšããŠãããããã£ã«çªå·ãä»ããã«ããããã£ãè¿œå ã§ããŸãã ãã®å Žåãã³ãŒãå ã§äœããã®çç±ã§çªå·ä»ãããããã£ãè¿œå ãããŠãããšæ³å®ããhasOwnPropertyãçç¥ããã®ã劥åœã§ããã³ãŒãã®å¯èªæ§ãäœäžããããã§ãã ã©ã€ãã©ãªã³ãŒãã§hasOwnPropertyã䜿çšããå¿ èŠããããŸããããã¯ããããã¿ã€ããã§ãŒã³ã«ã©ã®çªå·ä»ãããããã£ãããããæšæž¬ã§ããªãããã§ãã
inã¯ãã§ãŒã³å šäœãééãããããåç¶æ¿ã¬ã€ã€ãŒã§ã®åäœãé ããªããŸãã
hasOwnPropertyã䜿çšããŠãã£ã«ã¿ãªã³ã°ãã
// foo for(var i in foo) { if (foo.hasOwnProperty(i)) { console.log(i); } }
ãã®ãããªã³ãŒãã¯ãå€ãããŒãžã§ã³ã§ã®ã¿äœ¿çšã§ããŸãã hasOwnPropertyã®äœ¿çšã«ãããmooã®ã¿ãåºåãããŸãã hasOwnPropertyãåé€ãããšããããã¿ã€ããæ¡åŒµãããå¯èœæ§ããããããã³ãŒãã«ãšã©ãŒã衚瀺ãããå ŽåããããŸãã
ECMAScriptã®æ°ããããŒãžã§ã³ã§ã¯ãObject.definePropertyã䜿çšããŠçªå·ã®ãªãããããã£ãèšå®ã§ããhasOwnPropertyã䜿çšããã«ããããã£ãæž¡ããªã¹ã¯ã軜æžã§ããŸãã ããã«ãæ°ããECMAScriptã®æ©èœã䜿çšããªãPrototypeã®ãããªå€ãã©ã€ãã©ãªã®äœ¿çšã«ã¯æ³šæããå¿ èŠããããŸãã
ãŸãšã
ã©ã€ãã©ãªã³ãŒãã ãã§ãªããECMAScript 3以äžã§ãåžžã«hasOwnPropertyã䜿çšããããšããå§ãããŸãã ECMAScript 5以éãObject.definePropertyã䜿çšãããšãçªå·ã®ãªãããããã£ãå®çŸ©ã§ãããããhasOwnPropertyãçç¥ã§ããŸãã