å 容
- ã¯ããã«
- å€ãã¿ã€ããããã³æŒç®å
- ããã°ã©ã æ§æ
- æ©èœ
- ããŒã¿æ§é ïŒãªããžã§ã¯ããšé å
- é«éé¢æ°
- ãªããžã§ã¯ãã®ç§å¯ã®ç掻
- ãããžã§ã¯ãïŒé»åç掻
- æ€çŽ¢ãšãšã©ãŒåŠç
- æ£èŠè¡šçŸ
- ã¢ãžã¥ãŒã«
- ãããžã§ã¯ãïŒããã°ã©ãã³ã°èšèª
- JavaScriptãšãã©ãŠã¶ãŒ
- ããã¥ã¡ã³ããªããžã§ã¯ãã¢ãã«
- ã€ãã³ãåŠç
- ãããžã§ã¯ãïŒãã©ãããã©ãŒã ã²ãŒã
- ãã£ã³ãã¹ã«æã
- HTTP
- ãã©ãŒã ãšãã©ãŒã ãã£ãŒã«ã
- ãããžã§ã¯ãïŒãã€ã³ã
- Node.js
- ãããžã§ã¯ãïŒçµéšå ±æãŠã§ããµã€ã
- ã³ãŒãã®ãµã³ãããã¯ã¹
ãªããžã§ã¯ãæåèšèªã®åé¡ã¯ãæé»ã®ç°å¢ããã¹ãŠæã£ãŠããããšã§ãã ããªãã¯ããããå¿ èŠã§ãã-ãããŠãããªãã¯ããããšããã«ãžã£ã³ã°ã«å šäœã§ãŽãªã©ãæã«å ¥ããŸããã
ãžã§ãŒã¢ãŒã ã¹ããã³ã°ãCoders at Workãšã®ã€ã³ã¿ãã¥ãŒ
ããã°ã©ãã³ã°ã«ãããããªããžã§ã¯ãããšããçšèªã«ã¯ãæå³ãéåžžã«å€ãå«ãŸããŠããŸãã ç§ã®è·æ¥ã§ã¯ããªããžã§ã¯ãã¯ã©ã€ãã¹ã¿ã€ã«ã§ãããèæŠã®ããŒãã§ãããéæ³ã®åã倱ããªããæ°ã«å ¥ãã®åªæã§ãã
éšå€è ã«ã¯ãããã¯ãã¹ãŠæ確ã§ã¯ãããŸããã ããã°ã©ãã³ã°ã®æŠå¿µãšããŠã®ãªããžã§ã¯ãã®ç°¡åãªæŽå²ããå§ããŸãããã
ç©èª
ãã®ã¹ããŒãªãŒã¯ãã»ãšãã©ã®ããã°ã©ãã³ã°ã¹ããŒãªãŒãšåæ§ã«ãè€éãã®åé¡ããå§ãŸããŸãã ããã¢ã€ãã¢ã¯ãè€éããäºãã«åé¢ãããå°ããªéšåã«åå²ããããšã§ç®¡çããããããããšãã§ãããšèšããŸãã ãããã®éšåã¯ãªããžã§ã¯ããšåŒã°ãå§ããŸããã
ãªããžã§ã¯ãã¯ãå éšã®ã¹ãã£ãããŒãªè€éããé ãããŒãã·ã§ã«ã§ããã代ããã«ããªããžã§ã¯ãã䜿çšããå¿ èŠãããã€ã³ã¿ãŒãã§ã€ã¹ãè¡šããã¥ãŒãã³ã°ãããšã³ã³ã¿ã¯ãïŒã¡ãœãããªã©ïŒãæäŸããŸãã ãã®èãæ¹ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ãæ¯èŒçã·ã³ãã«ã§ããããããæäœãããšãã«ããªããžã§ã¯ãå ã§çºçãããã¹ãŠã®è€éãªããã»ã¹ãç¡èŠã§ããããã«ããããšã§ãã
ã·ã³ãã«ãªã€ã³ã¿ãŒãã§ãŒã¹ã¯ãå€ãã®è€éããé ãããšãã§ããŸãã
ããšãã°ãç»é¢ã®äžéšãžã®ã€ã³ã¿ãŒãã§ã€ã¹ãæäŸãããªããžã§ã¯ããæ³åããŠãã ããã ãã®ã»ã¯ã·ã§ã³ã§ã¯ããã®ã»ã¯ã·ã§ã³ã«å³åœ¢ãæç»ããããããã¹ãã衚瀺ãããã§ããŸãããåæã«ãããã¹ããŸãã¯å³åœ¢ã®ãã¯ã»ã«ãžã®å€æã«é¢ãããã¹ãŠã®è©³çŽ°ãé衚瀺ã«ãªããŸãã drawCircleãªã©ã®äžé£ã®ã¡ãœãããããããã®ãããªãªããžã§ã¯ãã䜿çšããããã«ç¥ã£ãŠããå¿ èŠãããã®ã¯ããã ãã§ãã
ãã®ãããªã¢ã€ãã¢ã¯70ã80幎代ã«éçºããã90幎代ã«ã¯åºåã®æ³¢ãã€ãŸããªããžã§ã¯ãæåããã°ã©ãã³ã°ã®é©åœã«ãã£ãŠè¡šé¢åãããŸããã çªç¶ã倧å¢ã®äººã ãããªããžã§ã¯ããããã°ã©ãã³ã°ã®æ£ããæ¹æ³ã§ãããšçºè¡šããŸããã ãããŠããªããžã§ã¯ããæããªããã®ã¯ãã¹ãŠæ代é ãã®ãã³ã»ã³ã¹ã§ãã
ãã®ãããªçä¿¡ã¯åžžã«ç¡æå³ãªç¡æå³ãªãã®ã®æã«ã€ãªããããã以æ¥äžçš®ã®åé©åœããããŸããã äžéšã®ãµãŒã¯ã«ã§ã¯ãäžè¬ã«ãªããžã§ã¯ãã®è©å€ãéåžžã«äœããªã£ãŠããŸãã
ç§ã¯ã€ããªãã®ãŒã®èŠ³ç¹ãããå®çšçãªèŠ³ç¹ããããããæ€èšããããšã奜ã¿ãŸãã ãªããžã§ã¯ãæåã®æåã«ãã£ãŠæ®åããŠãããç¹ã«ã«ãã»ã«åïŒå éšã®è€éããšå€éšã®åçŽãã®éãïŒã«åœ¹ç«ã€ã¢ã€ãã¢ãããã€ããããŸãã 圌ãã¯æ¢çŽ¢ãã䟡å€ããããŸãã
ãã®ç« ã§ã¯ãJavaScriptã®ãªããžã§ã¯ãã«å¯Ÿãããã颚å€ãããªã¢ãããŒããšãããããåŸæ¥ã®ãªããžã§ã¯ãæåæè¡ãšã©ã®ããã«é¢ä¿ãããã«ã€ããŠèª¬æããŸãã
æ¹æ³
ã¡ãœããã¯ãé¢æ°ãå«ãããããã£ã§ãã ç°¡åãªæ¹æ³ïŒ
var rabbit = {}; rabbit.speak = function(line) { console.log(" '" + line + "'"); }; rabbit.speak(" ."); // â ' .'
éåžžãã¡ãœããã¯ãåŒã³åºãå ã®ãªããžã§ã¯ãã«å¯ŸããŠäœããè¡ãå¿ èŠããããŸãã é¢æ°ãã¡ãœããã®åœ¢åŒã§åŒã³åºãããå Žå-object.methodïŒïŒãªã©ã®ãªããžã§ã¯ãã®ããããã£ãšããŠ-æ¬äœã®ç¹å¥ãªå€æ°ã¯ããããåŒã³åºãããªããžã§ã¯ãã瀺ããŸãã
function speak(line) { console.log(" " + this.type + " '" + line + "'"); } var whiteRabbit = {type: "", speak: speak}; var fatRabbit = {type: "", speak: speak}; whiteRabbit.speak(" , " + " !"); // â ' , !' fatRabbit.speak(" ."); // â ' .'
ã³ãŒãã¯thisããŒã¯ãŒãã䜿çšããŠã話ããŠãããŠãµã®ã®ã¿ã€ããåºåããŸãã
applyããã³bindã¡ãœããã¯ãã¡ãœããåŒã³åºãããšãã¥ã¬ãŒãããããã«äœ¿çšã§ããæåã®åŒæ°ãåãããšãæãåºããŠãã ããã ãã®æåã®åŒæ°ã¯ãthiså€æ°ã®å€ãäžããã ãã§ãã
åŒã°ããåŒã³åºããé©çšããã®ã«äŒŒãæ¹æ³ããããŸãã ãŸãããã®ã¡ãœããã§ããé¢æ°ãåŒã³åºããéåžžã®åŒæ°ã®ã¿ã䜿çšããé åã®åœ¢åŒã§ã¯äœ¿çšããŸããã é©çšããã³ãã€ã³ããšåæ§ã«ããã®å€ãåŒã³åºãã«æž¡ãããšãã§ããŸãã
speak.apply(fatRabbit, ["!"]); // â ' !' speak.call({type: ""}, " , ."); // â ', .'
ãããã¿ã€ã
æãèŠãŠãã ããã
var empty = {}; console.log(empty.toString); // â function toString(){âŠ} console.log(empty.toString()); // â [object Object]
空ã®ãªããžã§ã¯ãã®ããããã£ãååŸããŸããã éæ³ïŒ
ãã¡ãããéæ³ã§ã¯ãããŸããã JavaScriptã§ãªããžã§ã¯ããã©ã®ããã«æ©èœãããã«ã€ããŠãã¹ãŠã説æããããã§ã¯ãããŸããã æ©èœã»ããã«å ããŠãã»ãŒå šå¡ããããã¿ã€ããæã£ãŠããŸãã ãããã¿ã€ãã¯ãããããã£ã®ããã¯ã¢ãããœãŒã¹ãšããŠäœ¿çšãããå¥ã®ãªããžã§ã¯ãã§ãã ãªããžã§ã¯ããææããŠããªãããããã£ã®èŠæ±ãåãåããšããã®ããããã£ã¯ãããã¿ã€ãã«ãã£ãŠã次ã«ãããã¿ã€ããããã¿ã€ããªã©ã«ãã£ãŠæ€çŽ¢ãããŸãã
ããŠã空ã®ãªããžã§ã¯ãã®ãããã¿ã€ãã¯èª°ã§ããïŒ ããã¯ããã¹ãŠã®ãªããžã§ã¯ãã®å倧ãªç¥å ã§ããObject.prototypeã§ãã
console.log(Object.getPrototypeOf({}) == Object.prototype); // â true console.log(Object.getPrototypeOf(Object.prototype)); // â null
ãæ³åã®ãšãããObject.getPrototypeOfé¢æ°ã¯ãªããžã§ã¯ãã®ãããã¿ã€ããè¿ããŸãã
JavaScriptã®ãããã¿ã€ãé¢ä¿ã¯ããªãŒã®ããã«èŠããObject.prototypeã¯ãã®ã«ãŒãã«ãããŸãã ãªããžã§ã¯ããæååãã¥ãŒã«å€æããtoStringãªã©ããã¹ãŠã®ãªããžã§ã¯ãã«è¡šç€ºãããããã€ãã®ã¡ãœãããæäŸããŸãã
å€ãã®ãªããžã§ã¯ãã®ãããã¿ã€ãã¯ãçŽæ¥Object.prototypeã§ã¯ãªããããã©ã«ãããããã£ãæäŸããä»ã®ãªããžã§ã¯ãã§ãã é¢æ°ã¯Function.prototypeã«ç±æ¥ããé åã¯Array.prototypeã«ç±æ¥ããŸãã
console.log(Object.getPrototypeOf(isNaN) == Function.prototype); // â true console.log(Object.getPrototypeOf([]) == Array.prototype); // â true
ãã®ãããªãããã¿ã€ãã«ã¯ç¬èªã®ãããã¿ã€ãããããŸã-å€ãã®å ŽåObject.prototypeãªã®ã§ãçŽæ¥ã§ã¯ãããŸãããããšã«ããtoStringã®ãããªã¡ãœãããæäŸããŸãã
Object.getPrototypeOfé¢æ°ã¯ããªããžã§ã¯ãã®ãããã¿ã€ããè¿ããŸãã Object.createã䜿çšããŠãç¹å®ã®ãããã¿ã€ããæã€ãªããžã§ã¯ããäœæã§ããŸãã
var protoRabbit = { speak: function(line) { console.log(" " + this.type + " '" + line + "'"); } }; var killerRabbit = Object.create(protoRabbit); killerRabbit.type = ""; killerRabbit.speak("!"); // â ' !'
åãŠãµã®ã¯ããã¹ãŠã®ãŠãµã®ãæã£ãŠããããããã£ã®ã³ã³ãããšããŠæ©èœããŸãã ãã©ãŒãªããžã§ã¯ããªã©ã®ç¹å®ã®ãŠãµã®ãªããžã§ã¯ãã«ã¯ããã®ã¿ã€ããªã©ããã®ãªããžã§ã¯ãã«ã®ã¿é©çšãããããããã£ãå«ãŸãããããã¿ã€ãããä»ã®ãªããžã§ã¯ããšå ±æãããããããã£ãç¶æ¿ããŸãã
ã³ã³ã¹ãã©ã¯ã¿ãŒ
ç¹å®ã®ãããã¿ã€ãããç¶æ¿ããããªããžã§ã¯ããäœæãããã䟿å©ãªæ¹æ³ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒã§ãã JavaScriptã§ã¯ãåè¿°ã®æ°ããããŒã¯ãŒãã䜿çšããŠé¢æ°ãåŒã³åºããšãé¢æ°ãã³ã³ã¹ãã©ã¯ã¿ãŒãšããŠæ©èœããŸãã ã³ã³ã¹ãã©ã¯ã¿ã¯ãæ°ããäœæããããªããžã§ã¯ãã«ãã€ã³ããããthiså€æ°ãèªç±ã«äœ¿çšã§ãããªããžã§ã¯ããå«ãå¥ã®å€ãçŽæ¥è¿ããªãå Žåã代ããã«ãã®æ°ãããªããžã§ã¯ããè¿ãããŸãã
圌ãã¯ãnewã§äœæããããªããžã§ã¯ãã¯ã³ã³ã¹ãã©ã¯ã¿ãŒã®ã€ã³ã¹ã¿ã³ã¹ã§ãããšèšããŸãã
ããã¯åçŽãªãŠãµã®ã®ã³ã³ã¹ãã©ã¯ã¿ã§ãã ã³ã³ã¹ãã©ã¯ã¿ãŒåã¯éåžžãä»ã®é¢æ°ãšåºå¥ããããã«å€§æåã«ãªããŸãã
function Rabbit(type) { this.type = type; } var killerRabbit = new Rabbit(""); var blackRabbit = new Rabbit(""); console.log(blackRabbit.type); // â
ã³ã³ã¹ãã©ã¯ã¿ãŒïŒããã³äžè¬ã«ãã¹ãŠã®é¢æ°ïŒã¯ãprototypeãšããããããã£ãèªåçã«ååŸããŸãããã®ããããã£ã«ã¯ãããã©ã«ãã§Object.prototypeãã掟çããã·ã³ãã«ã§ç©ºã®ãªããžã§ã¯ããå«ãŸããŠããŸãã ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã«ãã£ãŠäœæãããåã€ã³ã¹ã¿ã³ã¹ã«ã¯ããããã¿ã€ããšããŠãã®ãªããžã§ã¯ãããããŸãã ãããã£ãŠãRabbitã³ã³ã¹ãã©ã¯ã¿ãŒã«ãã£ãŠäœæããããŠãµã®ã«speakã¡ãœãããè¿œå ããã«ã¯ã次ã®ããã«ããŸãã
Rabbit.prototype.speak = function(line) { console.log(" " + this.type + " '" + line + "'"); }; blackRabbit.speak(" ..."); // â ' ...'
ãããã¿ã€ããšã³ã³ã¹ãã©ã¯ã¿ãŒãšã®é¢é£ä»ãïŒãããã¿ã€ãããããã£çµç±ïŒãšãªããžã§ã¯ãã®ãããã¿ã€ãïŒObject.getPrototypeOfããååŸå¯èœïŒã®éãã«æ³šæããããšãéèŠã§ãã å®éãã³ã³ã¹ãã©ã¯ã¿ãŒã¯é¢æ°ã§ãããããã³ã³ã¹ãã©ã¯ã¿ãŒã®ãããã¿ã€ãã¯Function.prototypeã§ãã ãããã¿ã€ãããããã£ã¯ãããã«ãã£ãŠäœæãããã€ã³ã¹ã¿ã³ã¹ã®ãããã¿ã€ãã§ããããããã¿ã€ãã§ã¯ãããŸããã
ç¶æ¿ãããããããã£ã®åèªã¿èŸŒã¿
ãªããžã§ã¯ãã«ããããã£ãè¿œå ãããšããããã¿ã€ãã«ãããã©ããã«é¢ä¿ãªãããªããžã§ã¯ãèªäœã«çŽæ¥è¿œå ãããŸãã ä»ãããã¯åœŒã®è²¡ç£ã§ãã ãããã¿ã€ãã«åãååã®ããããã£ãããå Žåããªããžã§ã¯ãã«ã¯åœ±é¿ããŸããã ãããã¿ã€ãèªäœã¯å€æŽãããŸããã
Rabbit.prototype.teeth = ""; console.log(killerRabbit.teeth); // â killerRabbit.teeth = ", "; console.log(killerRabbit.teeth); // â , console.log(blackRabbit.teeth); // â console.log(Rabbit.prototype.teeth); // â
ãã®å³ã¯ãã³ãŒããå®è¡ããåŸã®ç¶æ³ã瀺ããŠããŸãã RabbitãšObjectã®ãããã¿ã€ãã¯ãèæ¯ã®ããã«killerRabbitã®èåŸã«ããããªããžã§ã¯ãèªäœã«ã¯ãªãããããã£ãèŠæ±ã§ããŸãã
å€ãã®å Žåããããã¿ã€ãã«ååšããããããã£ãåããŒããããšæçã§ãã ãŠãµã®ã®æ¯ã®äŸã®ããã«ãããäžè¬çãªç¹æ§ã®äŸå€çãªç¹æ§ãè¡šçŸããããã«äœ¿çšã§ããŸãããéåžžã®ãªããžã§ã¯ãã¯ãããã¿ã€ãããååŸããæšæºå€ã䜿çšããŸãã
ãŸããç°ãªãtoStringã¡ãœããã®é¢æ°ãšé åãå²ãåœãŠãããã«ã䜿çšãããŸãã
console.log(Array.prototype.toString == Object.prototype.toString); // â false console.log([1, 2].toString()); // â 1,2
é åã®toStringãåŒã³åºããšã.joinïŒ "ã"ïŒãšåæ§ã®çµæã衚瀺ãããŸã-ã³ã³ãåºåãã®ãªã¹ããååŸãããŸãã Object.prototype.toStringãé åã«çŽæ¥åŒã³åºããšãç°ãªãçµæãçæãããŸãã ãã®é¢æ°ã¯é åã«ã€ããŠäœãç¥ããŸããïŒ
console.log(Object.prototype.toString.call([1, 2])); // â [object Array]
æãŸãããªããããã¿ã€ãã®çžäºäœçš
ãããã¿ã€ãã¯ããã€ã§ããããã«åºã¥ããŠãããã¹ãŠã®ãªããžã§ã¯ãã«æ°ããããããã£ãšã¡ãœãããè¿œå ããã®ã«åœ¹ç«ã¡ãŸãã ããšãã°ããŠãµã®ã«ã¯ãã³ã¹ãå¿ èŠãªå ŽåããããŸãã
Rabbit.prototype.dance = function() { console.log(" " + this.type + " ."); }; killerRabbit.dance(); // â .
ããã¯äŸ¿å©ã§ãã ããããå Žåã«ãã£ãŠã¯ãããåé¡ã«ã€ãªãããŸãã åã®ç« ã§ã¯ãå€ãååã«é¢é£ä»ããæ¹æ³ãšããŠãªããžã§ã¯ãã䜿çšããŸããããããã®ååã®ããããã£ãäœæãã察å¿ããå€ãäžããŸããã 4ç« ã®äŸã次ã«ç€ºããŸãã
var map = {}; function storePhi(event, phi) { map[event] = phi; } storePhi("", 0.069); storePhi(" ", -0.081);
for / inã«ãŒãã䜿çšããŠãªããžã§ã¯ãå ã®ãã¹ãŠã®phiå€ãå埩åŠçããinæŒç®åã䜿çšããŠååã®ååšã確èªã§ããŸãã æ®å¿µãªããããªããžã§ã¯ãã®ãããã¿ã€ãã¯ç§ãã¡ãæ©ãŸããŠããŸãã
Object.prototype.nonsense = ""; for (var name in map) console.log(name); // â // â // â nonsense console.log("nonsense" in map); // â true console.log("toString" in map); // â true // delete Object.prototype.nonsense;
ããã¯ééã£ãŠããŸãã ããã³ã»ã³ã¹ããšåŒã°ããã€ãã³ãã¯ãããŸããã ããã«ããtoStringããšããã€ãã³ãã¯ãããŸããã
inæŒç®åã¯ã¢ã«ãŠã³ãã§trueãè¿ããŸãããtoStringãfor / inã«ãŒãã§æããªãã£ãããšã¯èå³æ·±ãã§ãã ããã¯ãJavaScriptãã«ãŠã³ãå¯èœãªããããã£ãšã«ãŠã³ãã§ããªãããããã£ãåºå¥ããããã§ãã
å€ãå²ãåœãŠãŠäœæãããã¹ãŠã®ããããã£ã¯ã«ãŠã³ãå¯èœã§ãã Object.prototypeã®ãã¹ãŠã®æšæºããããã£ã¯æ°ããããªããããfor / inã«ãŒãã§ã¯ããŒã«ã¢ãŠããããŸããã
ã«ãŠã³ãã§ããªãããããã£ã¯ãObject.definePropertyé¢æ°ã䜿çšããŠå®£èšã§ããŸãããã®é¢æ°ã䜿çšãããšãäœæããããããã£ã®ã¿ã€ããæå®ã§ããŸãã
Object.defineProperty(Object.prototype, "hiddenNonsense", {enumerable: false, value: ""}); for (var name in map) console.log(name); // â // â console.log(map.hiddenNonsense); // â
çŸåšã¯ããããã£ããããŸãããã«ãŒãå ã§ã¯è¡šç€ºãããŸããã ããã ããããObject.prototypeã®ããããã£ããªããžã§ã¯ãå ã«ååšãããšäž»åŒµããŠããinæŒç®åã®åé¡ã«ãŸã æ©ãŸãããŠããŸãã ãã®ããã«ã¯hasOwnPropertyã¡ãœãããå¿ èŠã§ãã
console.log(map.hasOwnProperty("toString")); // â false
圌ã¯ããããã¿ã€ãã«é¢ä¿ãªããããããã£ããªããžã§ã¯ãã®ããããã£ã§ãããã©ãããèšããŸãã å€ãã®å Žåãããã¯inã¹ããŒãã¡ã³ããçæãããããæçšãªæ å ±ã§ãã
ã³ãŒããããã°ã©ã ã«ããŠã³ããŒãããä»ã®èª°ãããªããžã§ã¯ãã®ã¡ã€ã³ãããã¿ã€ããå°ç¡ãã«ããŠããŸãã®ã§ã¯ãªãããšå¿é ããŠããå Žåã¯ã次ã®ãããªfor / inã«ãŒããäœæããããšããå§ãããŸãã
for (var name in map) { if (map.hasOwnProperty(name)) { // ... } }
ãããã¿ã€ãã®ãªããªããžã§ã¯ã
ãããããŠãµã®ã®ç©Žã¯ããã§çµãããŸããã ãããŠã誰ããããããªããžã§ã¯ãã«hasOwnPropertyåãç»é²ããããã«å€42ãå²ãåœãŠãå Žåã¯ã©ããªããŸããïŒ ããã§ãmap.hasOwnPropertyã®åŒã³åºãã¯ãé¢æ°ã§ã¯ãªãçªå·ãå«ãããŒã«ã«ããããã£ã«ã¢ã¯ã»ã¹ããŸãã
ãã®å Žåããããã¿ã€ãã¯éªéã«ãªãã ãã§ããããã¿ã€ãã®ãªããªããžã§ã¯ããå¿ èŠã§ãã ç¹å®ã®ãããã¿ã€ãã䜿çšããŠãªããžã§ã¯ããäœæã§ããObject.createé¢æ°ãèŠãŸããã ãããã¿ã€ãã«nullãæž¡ããŠããããã¿ã€ãã®ãªãæ°é®®ãªå°ããªãªããžã§ã¯ããäœæã§ããŸãã ããã¯ãä»»æã®ããããã£ãå¯èœãªã¿ã€ããããã®ãªããžã§ã¯ãã«å¿ èŠãªãã®ã§ãã
var map = Object.create(null); map[""] = 0.069; console.log("toString" in map); // â false console.log("" in map); // â true
ããã¯è¯ãã§ãïŒ ãªããžã§ã¯ãã®ãã¹ãŠã®ããããã£ã¯ç§ãã¡ãå人çã«èšå®ãããããhasOwnPropertyã®è¿·ãã¯ããå¿ èŠãããŸããã 人ã ãObject.prototypeã§äœãããããèŠãããšãªããéãã«for / inã«ãŒãã䜿çšããŸã
å€å
å€ããªããžã§ã¯ãã®æååã«å€æããStringé¢æ°ãåŒã³åºããšãtoStringã¡ãœãããåŒã³åºããŠæå³ã®ããæååãäœæããŸãã äžéšã®æšæºãããã¿ã€ãã¯ãtoStringã®ããŒãžã§ã³ã宣èšããŠãåãªãã[object Object]ããããæçšãªæååãäœæããããšãè¿°ã¹ãŸããã
ããã¯åŒ·åãªã¢ã€ãã¢ã®ç°¡åãªäŸã§ãã ç¹å®ã®ã€ã³ã¿ãŒãã§ã€ã¹ïŒãã®å Žåã¯toStringã¡ãœããïŒãä»ããŠãªããžã§ã¯ããæäœããã³ãŒããèšè¿°ãããŠããå Žåããã®ã€ã³ã¿ãŒãã§ã€ã¹ããµããŒããããªããžã§ã¯ãã¯ã³ãŒãã«æ¥ç¶ã§ãããã¹ãŠãæ©èœããŸãã
ãã®ãã¯ããã¯ã¯ããªã¢ãŒãã£ãºã ãšåŒã°ããŸã-誰ã圢ãå€ããŸããã ããªã¢ãŒãã£ãã¯ã³ãŒãã¯ãåãã€ã³ã¿ãŒãã§ã€ã¹ããµããŒãããŠããéããããŸããŸãªåœ¢åŒã®å€ã§æ©èœããŸãã
ããŒãã«ããã©ãŒããããã
ããªã¢ãŒãã£ãºã ãã©ã®ããã«èŠããããå®éã«ãªããžã§ã¯ãæåããã°ã©ãã³ã°ãç解ããããã«äŸãèŠãŠã¿ãŸãããã ãããžã§ã¯ãã¯æ¬¡ã®ãšããã§ããããŒãã«ã»ã«ããé åã®é åãåãåãããããã«ãã©ãŒããããããããŒãã«ãå«ãè¡ãäœæããããã°ã©ã ãäœæããŸãã ã€ãŸããåãšè¡ãæŽåãããŸãã ãã®ããã«ïŒ
name height country ------------ ------ ------------- Kilimanjaro 5895 Tanzania Everest 8848 Nepal Mount Fuji 3776 Japan Mont Blanc 4808 Italy/France Vaalserberg 323 Netherlands Denali 6168 United States Popocatepetl 5465 Mexico
ããã¯æ¬¡ã®ããã«æ©èœããŸããã¡ã€ã³é¢æ°ã¯åã»ã«ã®å¹ ãšé«ããå°ãããã®æ å ±ã䜿çšããŠåã®å¹ ãšè¡ã®é«ãã決å®ããŸãã 次ã«ãã»ã«ã«èªåèªèº«ãæç»ããããã«äŸé Œããçµæã1è¡ã§åéããŸãã
ããã°ã©ã ã¯ãæ確ã«å®çŸ©ãããã€ã³ã¿ãŒãã§ã€ã¹ãä»ããŠã»ã«ãªããžã§ã¯ããšéä¿¡ããŸãã ã»ã«ã¿ã€ãã¯åŒ·ãèšå®ãããŸããã æ°ããã»ã«ã¹ã¿ã€ã«ãè¿œå ã§ããŸããããšãã°ãèŠåºãã«äžç·ä»ãã®ã»ã«ãè¿œå ã§ããŸãã ãããŠã圌ããç§ãã¡ã®ã€ã³ã¿ãŒãã§ãŒã¹ããµããŒãããŠããã°ã圌ãã¯ããã°ã©ã ãå€æŽããããšãªãåçŽã«åäœããŸãã
ã€ã³ã¿ãŒãã§ãŒã¹ïŒ
minHeightïŒïŒã¯ãã»ã«ã«å¿ èŠãªæå°ã®é«ãã瀺ãæ°å€ãè¿ããŸãïŒè¡ã§è¡šãããŸãïŒ
minWidthïŒïŒã¯ãã»ã«ã«å¿ èŠãªæå°å¹ ã瀺ãæ°å€ãè¿ããŸãïŒæåã§è¡šãããŸãïŒ
drawïŒwidthãheightïŒã¯ãæååã®ã»ãããå«ãé·ãheightã®é åãè¿ããŸããåæååã¯æåå¹ ã§ãã ããã¯ã»ã«ã®å 容ã§ãã
ããã§ã¯é¢é£æ§ãé«ããããé«éé¢æ°ã䜿çšããŸãã
ããã°ã©ã ã®æåã®éšåã§ã¯ãã»ã«ãããªãã¯ã¹ã®æå°åå¹ ãšè¡é«ãã®é åãèšç®ããŸãã rowså€æ°ã«ã¯é åã®é åãå«ãŸããŸããåå éšé åã¯ã»ã«ã®è¡ã§ãã
function rowHeights(rows) { return rows.map(function(row) { return row.reduce(function(max, cell) { return Math.max(max, cell.minHeight()); }, 0); }); } function colWidths(rows) { return rows[0].map(function(_, i) { return rows.reduce(function(max, row) { return Math.max(max, row[i].minWidth()); }, 0); }); }
ååãã¢ã³ããŒã¹ã³ã¢ïŒ_ïŒã§å§ãŸãïŒãŸãã¯å šäœã§æ§æãããïŒå€æ°ã䜿çšããŠããã®åŒæ°ã䜿çšãããªãããšãã³ãŒããèªã¿åã人ã«ç€ºããŸãã
rowHeightsé¢æ°ã¯ç°¡åãªã¯ãã§ãã reduceã䜿çšããŠã»ã«é åã®æ倧ã®é«ããèšç®ãããããmapã«ã©ããããŠãrowsé åã®ãã¹ãŠã®è¡ãèµ°æ»ããŸãã
å€éšé åã¯åã§ã¯ãªãè¡ã®é åã§ãããããcolWidthsã®ç¶æ³ã¯ããè€éã§ãã ãããïŒforEachãfilterãããã³åæ§ã®é åã¡ãœãããªã©ïŒã2çªç®ã®åŒæ°ãæå®ãããé¢æ°ïŒçŸåšã®èŠçŽ ã®ã€ã³ããã¯ã¹ïŒã«æž¡ãããšãå¿ããŠããŸããã mapã§æåã®è¡ã®èŠçŽ ãæž¡ããé¢æ°ã®2çªç®ã®åŒæ°ã®ã¿ã䜿çšããŠãcolWidthsã¯åã€ã³ããã¯ã¹ããšã«1ã€ã®èŠçŽ ãæã€é åãäœæããŸãã reduceåŒã³åºãã¯ãåã€ã³ããã¯ã¹ã®å€åŽã®è¡ã®é åã調ã¹ããã®ã€ã³ããã¯ã¹å ã§æãå¹ ã®åºãã»ã«ã®å¹ ãéžæããŸãã
ããŒãã«ã衚瀺ããã³ãŒãïŒ
function drawTable(rows) { var heights = rowHeights(rows); var widths = colWidths(rows); function drawLine(blocks, lineNo) { return blocks.map(function(block) { return block[lineNo]; }).join(" "); } function drawRow(row, rowNum) { var blocks = row.map(function(cell, colNum) { return cell.draw(widths[colNum], heights[rowNum]); }); return blocks[0].map(function(_, lineNo) { return drawLine(blocks, lineNo); }).join("\n"); } return rows.map(drawRow).join("\n"); }
drawTableé¢æ°ã¯ãå éšã®drawRowé¢æ°ã䜿çšããŠãã¹ãŠã®ç·ãæç»ãããããã®å Žæãæ¹è¡æåã§æ¥ç¶ããŸãã
drawRowé¢æ°ã¯ãè¡ã»ã«ãªããžã§ã¯ãããããã¯ã«å€æããŸãããããã¯ã¯ãè¡ã§åºåãããã»ã«ã®å 容ãè¡šãæååã®é åã§ãã çªå·3776ãå«ãåäžã®ã»ã«ã¯1ã€ã®èŠçŽ ã®é å["3776"]ã§è¡šãããšãã§ããäžç·ä»ãã®ã»ã«ã¯2è¡ãå æããŠé å["name"ã "----"]ã®ããã«èŠããŸãã
åãé«ãã®è¡ã®ãããã¯ã¯ãé£å士ã«è¡šç€ºããå¿ èŠããããŸãã drawRowã§ã®mapã®2çªç®ã®åŒã³åºãã¯ã巊端ã®ãããã¯ã®è¡ããå§ããŠãåºåã®ãã®è¡ãè¡ããšã«æ§ç¯ãã次ã«ããããã®è¡ãããŒãã«ã®å šå¹ ã«è¿œå ããŸãã 次ã«ããããã®è¡ã¯æ¹è¡ã§çµåãããdrawRowãè¿ãäžé£ã®å šäœãäœæãããŸãã
drawLineé¢æ°ã¯ããããã¯ã®é åããé£ãåãè¡ããã£ããã£ããããããã¹ããŒã¹ã§é£çµããŠãããŒãã«ã®åéã«1æåã®ã®ã£ãããäœæããŸãã
ã»ã«ã®ã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããããã¹ããå«ãã»ã«ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãäœæããŸãããã splitã¡ãœããã䜿çšããŠæååãæååã®é åã«åå²ããŸããsplitã¡ãœããã¯ãåŒæ°ãåºçŸãããã³ã«æååãåãåãããããã®æçã®é åãè¿ããŸãã minWidthã¡ãœããã¯ãé åå ã®æ倧ç·å¹ ãèŠã€ããŸãã
function repeat(string, times) { var result = ""; for (var i = 0; i < times; i++) result += string; return result; } function TextCell(text) { this.text = text.split("\n"); } TextCell.prototype.minWidth = function() { return this.text.reduce(function(width, line) { return Math.max(width, line.length); }, 0); }; TextCell.prototype.minHeight = function() { return this.text.length; }; TextCell.prototype.draw = function(width, height) { var result = []; for (var i = 0; i < height; i++) { var line = this.text[i] || ""; result.push(line + repeat(" ", width - line.length)); } return result; };
è£å©é¢æ°repeatã䜿çšãããæå®ãããåæ°ã ãæå®ãããå€ã§è¡ãäœæãããŸãã drawã¡ãœããã¯ããã䜿çšããŠãå¿ èŠãªé·ãã«ãªãããã«è¡ãã€ã³ãã³ãããŸãã
çµéšã®ããã«5x5ã®ãã§ã¹ç€ãæããŠã¿ãŸãããã
var rows = []; for (var i = 0; i < 5; i++) { var row = []; for (var j = 0; j < 5; j++) { if ((j + i) % 2 == 0) row.push(new TextCell("##")); else row.push(new TextCell(" ")); } rows.push(row); } console.log(drawTable(rows)); // â ## ## ## // ## ## // ## ## ## // ## ## // ## ## ##
åäœããŸãïŒ ãã ãããã¹ãŠã®ã»ã«ã®ãµã€ãºã¯åãã§ãããããããŒãã«ã®æžåŒèšå®ã³ãŒãã¯äœãé¢çœããããŸããã
äœæäžã®å±±ã®ããŒãã«ã®åæããŒã¿ã¯ãMOUNTAINSå€æ°ã«å«ãŸããŠããŸãããã®å€æ°ã¯ã ããããããŠã³ããŒãã§ããŸã ã
ã¢ã³ããŒã¹ã³ã¢ã䜿çšããŠãååãå«ãäžçªäžã®è¡ã匷調衚瀺ããå¿ èŠããããŸãã åé¡ãããŸãã-ãããè¡ãã»ã«ã®ã¿ã€ããèšå®ããã ãã§ãã
function UnderlinedCell(inner) { this.inner = inner; }; UnderlinedCell.prototype.minWidth = function() { return this.inner.minWidth(); }; UnderlinedCell.prototype.minHeight = function() { return this.inner.minHeight() + 1; }; UnderlinedCell.prototype.draw = function(width, height) { return this.inner.draw(width, height - 1) .concat([repeat("-", width)]); };
äžç·ã®ã»ã«ã«ã¯å¥ã®ã»ã«ãå«ãŸããŠããŸãã ïŒminWidthã¡ãœãããšminHeightã¡ãœãããåŒã³åºããŠïŒå éšã»ã«ãšåããµã€ãºãè¿ããŸãããããã·ã¥ãå ããã¹ããŒã¹ã®ããã«é«ãã«1ãè¿œå ããŸãã
æç»ã¯ç°¡åã§ã-å åŽã®ã»ã«ã®å 容ãååŸããããã·ã¥ã§æºãããã1è¡ãè¿œå ããŸãã
ããã§ãã¡ã€ã³ãšã³ãžã³ã䜿çšããŠãããŒã¿ã»ããããã»ã«ã®ã°ãªãããäœæããé¢æ°ãäœæã§ããŸãã
function dataTable(data) { var keys = Object.keys(data[0]); var headers = keys.map(function(name) { return new UnderlinedCell(new TextCell(name)); }); var body = data.map(function(row) { return keys.map(function(name) { return new TextCell(String(row[name])); }); }); return [headers].concat(body); } console.log(drawTable(dataTable(MOUNTAINS))); // â name height country // ------------ ------ ------------- // Kilimanjaro 5895 Tanzania // âŠ
æšæºã®Object.keysé¢æ°ã¯ããªããžã§ã¯ãããããã£åã®é åãè¿ããŸãã ããŒãã«ã®äžçªäžã®è¡ã«ã¯ãååã®ä»ããäžç·ä»ãã»ã«ãå«ãŸããŠããå¿ èŠããããŸãã ããŒã¿ã»ããã®ãã¹ãŠã®ãªããžã§ã¯ãã®å€ã¯ãèŠåºãã®äžã«ããéåžžã®ã»ã«ã®ããã«èŠããŸããããŒé åã«ãããé¢æ°ãæž¡ãããšã§æœåºããåè¡ã§ã»ã«ã®é åºã1ã€ã«ããããã«ããŸãã
çµæã®ããŒãã«ã¯ãäŸã®ããŒãã«ã«äŒŒãŠãããæ°åã ããå³æããããŠããŸããã ããã«ã€ããŠã¯å°ãåŸã§èª¬æããŸãã
ã²ãã¿ãŒãšã»ãã¿ãŒ
ã€ã³ã¿ãŒãã§ã€ã¹ãäœæãããšãã«ãã¡ãœããã§ã¯ãªãããããã£ãå ¥åã§ããŸãã æ°å€ãæ ŒçŽããããã®å€æ°ãšããŠminHeightãšminWidthãç°¡åã«å®çŸ©ã§ããŸãã ããããã³ã³ã¹ãã©ã¯ã¿ãŒã§å€ãèšç®ããããã®ã³ãŒããèšè¿°ããå¿ èŠããããŸãããªããžã§ã¯ãã®æ§ç¯ã¯ãããã«çŽæ¥é¢ä¿ããªããããããã¯æªãããšã§ãã ããšãã°ãå åŽã®ã»ã«ãŸãã¯äžç·ã®ä»ããã»ã«ãå€æŽããããšãã«åé¡ãçºçããå¯èœæ§ãããããã®ãµã€ãºãå€æŽããå¿ èŠããããŸãã
ãããã®èæ ®äºé ã«ãããå€ãã®éã¡ãœããããããã£ãã€ã³ã¿ãŒãã§ã€ã¹ããçç¥ãããŸããã ããããã£å€ã«çŽæ¥ã¢ã¯ã»ã¹ãã代ããã«ãgetSomethingãsetSomethingãªã©ã®ã¡ãœããã䜿çšããŠããããã£å€ãèªã¿æžãããŸãã ãããããã€ãã¹ããããŸã-ããªãã¯å€ãã®è¿œå ã®ã¡ãœãããæžãïŒãããŠèªãïŒå¿ èŠããããŸãã
幞ããªããšã«ãJavaScriptã¯äž¡æ¹ã®ã¢ãããŒãã®é·æã䜿çšããææ³ãæäŸããŸãã å€éšã§ã¯æ®éã«èŠããããããã£ãèšå®ã§ããŸããããããã«é¢é£ä»ããããã¡ãœãããå¯ãã«æã£ãŠããŸãã
var pile = { elements: ["", "", ""], get height() { return this.elements.length; }, set height(value) { console.log(" ", value); } }; console.log(pile.height); // â 3 pile.height = 100; // â 100
ãªããžã§ã¯ãã®å®£èšã§ã¯ãã¬ã³ãŒãã®ååŸãŸãã¯èšå®ã«ãããããããã£ã®èªã¿åããŸãã¯æžã蟌ã¿æã«åŒã³åºãããé¢æ°ãæå®ã§ããŸããObject.definePropertyé¢æ°ã䜿çšããŠããããã¿ã€ããªã©ã®æ¢åã®ãªããžã§ã¯ãã«ãã®ãããªããããã£ãè¿œå ããããšãã§ããŸãïŒä»¥åã«äœ¿çšããŠãæ°ãåããªãããããã£ãäœæããŸããïŒã
Object.defineProperty(TextCell.prototype, "heightProp", { get: function() { return this.text.length; } }); var cell = new TextCell("\n"); console.log(cell.heightProp); // â 2 cell.heightProp = 100; console.log(cell.heightProp); // â 2
definePropertyã«æž¡ããããªããžã§ã¯ãã«setããããã£ãèšå®ããŠãsetterã¡ãœãããæå®ããããšãã§ããŸããã²ãã¿ãŒã¯ãããã»ãã¿ãŒããªãå Žåãããããã£ãžã®æžã蟌ã¿ã¯ç¡èŠãããŸãã
ç¶æ¿
ãã ããè¡šã®æžåŒèšå®ã®æŒç¿ã¯ãŸã å®äºããŠããŸãããæ°å€åãå³æãã®å Žåãèªã¿ããããªããŸãã TextCellã®ãããªå¥ã®ã¿ã€ãã®ã»ã«ãäœæããå¿ èŠããããŸãããããã¹ããå³åŽã«ã¹ããŒã¹ã§åãã代ããã«ãå·ŠåŽã«åããŠå³åŽã«æããŸãã
ãããã¿ã€ãã®3ã€ã®ã¡ãœãããã¹ãŠã䜿çšããŠãæ°ããã³ã³ã¹ãã©ã¯ã¿ãŒãäœæã§ããŸãããããããããã¿ã€ãèªäœã«ãããã¿ã€ããå«ããããšãã§ãããããããã¹ããŒãã«è¡ãããšãã§ããŸãã
function RTextCell(text) { TextCell.call(this, text); } RTextCell.prototype = Object.create(TextCell.prototype); RTextCell.prototype.draw = function(width, height) { var result = []; for (var i = 0; i < height; i++) { var line = this.text[i] || ""; result.push(repeat(" ", width - line.length) + line); } return result; };
éåžžã®TextCellã®ã³ã³ã¹ãã©ã¯ã¿ãŒãšminHeightããã³minWidthã¡ãœãããåå©çšããŸããããŸããRTextCellã¯ãdrawã¡ãœããã«å¥ã®é¢æ°ãããããšãé€ããŠãäžè¬ã«TextCellãšåçã§ãã
ãã®ãããªã¹ããŒã ã¯ãç¶æ¿ãšåŒã°ããŸããå€ãã®åŽåãè²»ããããšãªããæ¢åã®ãã®ã«åºã¥ããŠåªããããŒã¿åãæ§ç¯ã§ããŸããéåžžãæ°ããã³ã³ã¹ãã©ã¯ã¿ãŒã¯å€ããªããžã§ã¯ããåŒã³åºããŸãïŒcallã¡ãœãããä»ããŠãæ°ãããªããžã§ã¯ããšãã®å€ãæž¡ããŸãïŒããã®åŸãå€ããªããžã§ã¯ãã«ããã¯ãã®ãã¹ãŠã®ãã£ãŒã«ããè¿œå ããããšæ³å®ã§ããŸãããã®åã®ã€ã³ã¹ã¿ã³ã¹ãå€ããããã¿ã€ãã®ããããã£ã«ã¢ã¯ã»ã¹ã§ããããã«ããããã¿ã€ãã³ã³ã¹ãã©ã¯ã¿ãŒãå€ããã®ããç¶æ¿ããŸããæåŸã«ãããã€ãã®ããããã£ãæ°ãããããã¿ã€ãã«è¿œå ããããšã§ãªãŒããŒã©ã€ãã§ããŸãã
æ°å€ã»ã«ã«RTextCellsã䜿çšããããã«dataTableé¢æ°ãå°ãç·šéãããšãå¿ èŠãªããŒãã«ãååŸãããŸãã
function dataTable(data) { var keys = Object.keys(data[0]); var headers = keys.map(function(name) { return new UnderlinedCell(new TextCell(name)); }); var body = data.map(function(row) { return keys.map(function(name) { var value = row[name]; // : if (typeof value == "number") return new RTextCell(String(value)); else return new TextCell(String(value)); }); }); return [headers].concat(body); } console.log(drawTable(dataTable(MOUNTAINS))); // â âŠ
ç¶æ¿ã¯ãã«ãã»ã«åãšå€æ æ§ãšãšãã«ããªããžã§ã¯ãæåã®äŒçµ±ã®äž»èŠãªéšåã§ããããããåŸè ã®2ã€ã¯çŽ æŽãããã¢ã€ãã¢ãšããŠèªèãããŠããŸãããæåã®2ã€ã¯è°è«ã®äœå°ããããŸãã
äž»ã«ãéåžžãããªã¢ãŒãã£ãºã ãšæ··åãããå®éããã匷åãªããŒã«ãšããŠæ瀺ãããä»ã®ç®çã«äœ¿çšãããããã§ããã«ãã»ã«åãšããªã¢ãŒãã£ãºã ã䜿çšããŠã³ãŒãã®äžéšãåé¢ããããã°ã©ã ã®äžè²«æ§ãæžãããŸãããç¶æ¿ã¯åãçµåããããå€ãã®çµåãäœæããŸãã
ç¶æ¿ãªãã§ããªã¢ãŒãã£ãºã ã䜿çšã§ããŸããç¶æ¿ãå®å šã«é¿ããããšã¯ãå§ãããŸãã-ç§ã¯ããã°ã©ã ã§ãããå®æçã«äœ¿çšããŸãããã ããã³ãŒããæŽçããåºæ¬ååãšããŠã§ã¯ãªããæå°éã®ã³ãŒãã§æ°ããåãå®çŸ©ã§ããããã«ãããããå·§åŠãªããªãã¯ãšããŠæ±ã£ãŠãã ãããUnderlinedCellã¯å¥ã®ã»ã«ãªããžã§ã¯ãã䜿çšããŠæ§ç¯ããããããæ§æã䜿çšããŠåãæ¡åŒµããããšããå§ãããŸãã圌ã¯åã«ãããããããã£ã«ä¿åããåŒã³åºããèªåã®ã¡ãœããã«ãªãã€ã¬ã¯ãããŸãã
InstanceofæŒç®å
ãªããžã§ã¯ããç¹å®ã®ã³ã³ã¹ãã©ã¯ã¿ãŒããæ¥ããã©ãããç¥ãã®ã䟿å©ãªå ŽåããããŸãããã®ããã«ãJavaScriptã¯instanceofãã€ããªæŒç®åãæäŸããŸãã
console.log(new RTextCell("A") instanceof RTextCell); // â true console.log(new RTextCell("A") instanceof TextCell); // â true console.log(new TextCell("A") instanceof RTextCell); // â false console.log([1] instanceof Array); // â true
æŒç®åã¯ç¶æ¿ãããåãééããŸãã RTextCell.prototypeã¯TextCell.prototypeãã掟çããŠãããããRTextCellã¯TextCellã®ã€ã³ã¹ã¿ã³ã¹ã§ããæŒç®åã¯ãé åãªã©ã®æšæºã³ã³ã¹ãã©ã¯ã¿ãŒã«ãé©çšã§ããŸããã»ãšãã©ãã¹ãŠã®ãªããžã§ã¯ãã¯Objectã®ã€ã³ã¹ã¿ã³ã¹ã§ãã
ãŸãšã
ãªããžã§ã¯ãã¯ãæåã«éä¿¡ãããã®ãããå°ãè€éã§ããããšãããããŸããããããã¿ã€ãããããŸã-ãããã¯ä»ã®ãªããžã§ã¯ãã§ããããããã¿ã€ãã«ãã®ããããã£ãããå Žåãå®éã«ã¯ååšããªãããããã£ããããã®ããã«åäœããŸããåçŽãªãªããžã§ã¯ãã®ãããã¿ã€ãã¯Object.prototype /
Constructorsã§ããååãé垞倧æåã§å§ãŸãé¢æ°ã¯ãnewæŒç®åã§ãªããžã§ã¯ããäœæããããã«äœ¿çšã§ããŸããæ°ãããªããžã§ã¯ãã®ãããã¿ã€ãã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒã®prototypeããããã£ã«å«ãŸãããªããžã§ã¯ãã«ãªããŸããããã¯ãç¹å®ã®åã®ãã¹ãŠã®å€ããããã¿ã€ãå ã§å ±æããããããã£ãé 眮ããããšã§äœ¿çšã§ããŸãã instanceofæŒç®åã¯ããªããžã§ã¯ããšã³ã³ã¹ãã©ã¯ã¿ãŒãæå®ãããšããªããžã§ã¯ãããã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®ã€ã³ã¹ã¿ã³ã¹ã§ãããã©ãããå€æã§ããŸãã
ãªããžã§ã¯ãã®å Žåãã€ã³ã¿ãŒãã§ã€ã¹ãäœæãããã®ã€ã³ã¿ãŒãã§ã€ã¹ãä»ããŠã®ã¿ãªããžã§ã¯ããšéä¿¡ããããã«å šå¡ã«æ瀺ã§ããŸãããªããžã§ã¯ãã®æ®ãã®å®è£ ã®è©³çŽ°ã¯ã«ãã»ã«åãããã€ã³ã¿ãŒãã§ã€ã¹ã®èåŸã«é ãããŠããŸãã
ãã®åŸãåãã€ã³ã¿ãŒãã§ãŒã¹ã䜿çšããŠç°ãªããªããžã§ã¯ãã䜿çšããããšãçŠæ¢ãã人ã¯ããŸãããç°ãªããªããžã§ã¯ããåãã€ã³ã¿ãŒãã§ãŒã¹ãæã£ãŠããå Žåããããã§åäœããã³ãŒãã¯ç°ãªããªããžã§ã¯ãã§åãããã«åäœã§ããŸããããã¯ããªã¢ãŒãã£ãºã ãšåŒã°ããéåžžã«äŸ¿å©ã§ãã
现éšã®ã¿ãç°ãªãããã€ãã®ã¿ã€ããå®çŸ©ããå Žåãæ°ããã¿ã€ãã®ãããã¿ã€ããå€ãã¿ã€ãã®ãããã¿ã€ãããåçŽã«ç¶æ¿ãããšãæ°ããã³ã³ã¹ãã©ã¯ã¿ãŒãå€ãã¿ã€ããåŒã³åºãã®ã«äŸ¿å©ã§ããããã«ãããå€ããã®ãšåæ§ã®ãªããžã§ã¯ãã¿ã€ããåŸãããŸãããããããã£ãè¿œå ããããå€ããã®ãåå®çŸ©ãããã§ããŸãã
æŒç¿
ãã¯ã¿ãŒã¿ã€ã
2次å 空éã®ãã¯ãã«ãè¡šãVectorã³ã³ã¹ãã©ã¯ã¿ãŒãèšè¿°ããŸãããã©ã¡ãŒã¿xãšyïŒæ°å€ïŒãåãããããã¯åãååã®ããããã£ã«ä¿åãããŸãã
Vectorãããã¿ã€ãã«ãã©ã¹ãšãã€ãã¹ã®2ã€ã®ã¡ãœãããäžããŸãããããã¯å¥ã®ãã¯ã¿ãŒããã©ã¡ãŒã¿ãŒãšããŠåãåããxãšyã®2ã€ã®åãŸãã¯å·®ãæ ŒçŽããæ°ãããã¯ã¿ãŒãè¿ããŸãïŒ1ã€ã2çªç®ã®åŒæ°ïŒ
ãã¯ã¿ãŒã®é·ããã«ãŠã³ããããããã¿ã€ãã«ã²ãã¿ãŒé·ãè¿œå ããŸã-ïŒ0ã0ïŒããïŒxãyïŒãŸã§ã®è·é¢ã
// console.log(new Vector(1, 2).plus(new Vector(2, 3))); // â Vector{x: 3, y: 5} console.log(new Vector(1, 2).minus(new Vector(2, 3))); // â Vector{x: -1, y: -1} console.log(new Vector(3, 4).length); // â 5
ããäžã€ã®ã»ã«
ãã®ç« ã®ããŒãã«ã»ã«ã€ã³ã¿ãŒãã§ã€ã¹ã«äžèŽããã»ã«ã¿ã€ãStretchCellïŒå éšãå¹ ãé«ãïŒãäœæããŸãã圌ã¯ïŒUnderlinedCellã®ããã«ïŒå¥ã®ã»ã«ãã©ããããçµæã®ã»ã«ãå°ãªããšãæå®ãããå¹ ãšé«ããæã£ãŠããããšã確èªããå¿ èŠããããŸãïŒå åŽã®ã»ã«ãå°ãããŠãïŒã
// . var sc = new StretchCell(new TextCell("abc"), 1, 2); console.log(sc.minWidth()); // â 3 console.log(sc.minHeight()); // â 2 console.log(sc.draw(3, 2)); // â ["abc", " "]
ã·ãŒã±ã³ã¹ãžã®ã€ã³ã¿ãŒãã§ãŒã¹
å€ã®ã»ãããæœè±¡åããã€ã³ã¿ãŒãã§ãŒã¹ãèšèšããŸãããã®ãããªã€ã³ã¿ãŒãã§ã€ã¹ãæã€ãªããžã§ã¯ãã¯ã·ãŒã±ã³ã¹ã§ãããã€ã³ã¿ãŒãã§ã€ã¹ã¯ã³ãŒããã·ãŒã±ã³ã¹ãééãããããæ§æããå€ãåŠçããäœããã®æ¹æ³ã§ã·ãŒã±ã³ã¹ã®çµããã«å°éããããšãéç¥ã§ããããã«ããå¿ èŠããããŸãã
ã€ã³ã¿ãŒãã§ãŒã¹ãå®çŸ©ããããlogFiveé¢æ°ãäœæããŠã¿ãŠãã ãããlogFiveé¢æ°ã¯ãã·ãŒã±ã³ã¹ãªããžã§ã¯ããååŸããæåã®5ã€ã®èŠçŽ ã«å¯ŸããŠconsole.logãåŒã³åºããŸãã
次ã«ãé åãã©ããããéçºããã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠé åãèµ°æ»ã§ããArraySeqãªããžã§ã¯ãã¿ã€ããäœæããŸããå¥ã®ã¿ã€ãã®ãªããžã§ã¯ãRangeSeqãäœæããŸããããã¯ãæ°å€ã®ç¯å²ãééããŸãïŒã³ã³ã¹ãã©ã¯ã¿ãŒã¯ãfromåŒæ°ãštoåŒæ°ãåãå ¥ããå¿ èŠããããŸãïŒã
// . logFive(new ArraySeq([1, 2])); // â 1 // â 2 logFive(new RangeSeq(100, 1000)); // â 100 // â 101 // â 102 // â 103 // â 104