JavaScript: The Good Partsãèªãã§äžçŽè ãžãªãããïŒ (å匷å 容ãŸãšã)
æè¿Webã¢ããªã±ãŒã·ã§ã³ã®ããã³ããšã³ãã®ç¥èã足ããŠããªãïŒãšæãJavaScriptãæèããŠè§Šãããã«ããŠããŸããã
åºæ¬çãªææ³ãç°¡åãªAPIã¯æŒãããããAngularã§ã¢ããªäœã£ãŠã¿ããããŠãåŸã ã«èº«ã«ä»ããŠããããªãŒãšæã£ãŠããŸããã§ãããæªã ã«åå¿è ã®åãåºãªãããªã»ã»ã»ãšã
ãããªèš³ã§åªç§ã§ä¿¡é Œã§ããåŸèŒ©ã«ãäœãè¯ãæ¬ãšãç¥ããªãïŒããšèããŠã¿ããšããGood PartsãæããŠããããŸããã
[tmkm-amazon]4873113911[/tmkm-amazon]
å€ãæ¬ãªã®ã§ããã ã埮åŠãããã§ãããè¯ãæ¬ã¿ããã§ãã
ãã®æ¬ãGood Partsãšèšãããã¯Bad Partsã®æ¹ãåã£ãŠããæ°ãããŠããŸãããªãããšãããšãåºæ¬æ¹éããJavaScriptã¯è¯ãæ©èœããããã©ãæªãæ©èœãå€ãã®ã§ãæªãéšåã¯äœ¿ããªãããã«ãããïŒããšãããã®ã ããã§ãã
äœã¯ãšããããè¯ãæ¬ãªãã°åŠã¹ãäºãå€ãã ããïŒãšããèãã§èªã¿é²ããŠããŸãããé£ããéšåããããŸãããèãæ¬ãªã®ã§ããã»ã©èŸãã¯ãªãã§ããä»æ¥ã¯ãã®æ¬ããåŠãã äºããŸãšããŠã¿ãããšæããŸãã
ãããã¯ã³ã¡ã³ã/* */ã¯æ³šæ
æ£èŠè¡šçŸãªãã©ã«ã§*/ãåºãŠããã®ã§ãæ£èŠè¡šçŸéšåããããã¯ã³ã¡ã³ãã§å²ããããªã±ãŒã¹ã¯æ³šæãå¿ èŠã§ãã
ã³ã¡ã³ãã¯è¡ã³ã¡ã³ã//ã§çµ±äžãããšãããããã¯ã³ã¡ã³ãã¯é¢æ°ã®èª¬æéšåã ãã«äœ¿çšãããšããäœãããã®åºæºãèªåã®äžã«æã£ãŠãããšè¯ããããããªãã§ãã
äºçŽèª
caseã ãšãelseã ãšãã¯ä»ã®èšèªãšåæ§ã ãããŸã䜿ããªãã®ã§å€§äžå€«ããšæããŸããdefault delete inãããã¯æ°ãã€ããæ¹ãè¯ãããªãšããå°è±¡ã§ãã
æ°å€
JavaScriptã§ã¯æ°å€åã¯ïŒã€ã1ãš1.0ã¯åãå€ãçµæ§é©ãã§ããã
ãããã¯ãšå€æ°å®çŸ©å Žæ
ãããã¯ã¯äžé£ã®åœä»€æã®ããšã§ãäžæ¬åŒ§ã§å²ãŸããŠããããã ãJavaScriptã®ãããã¯ã¯ãã»ãã®äžè¬çãªèšèªãšã¯ç°ãªããæ°ããã¹ã³ãŒããçæããªãããããã£ãŠãå€æ°ã¯ãããã¯ã®äžã§ã¯ãªããé¢æ°ã®å é ã§å®çŸ©ãã¹ãã ã JavaScript: The Good Parts p.12
ããã¯ã©ããªãã§ãããããCode Complete(å¥ã®æ¬)ããªãŒããã«ã³ãŒã(å¥ã®æ¬)ã§ã¯å€æ°ã¯å©çšããäœçœ®ã«è¿ãäœçœ®ã§å®çŸ©ããçãªäºãæžãããŠããŸããããJavaããã°ã©ãã®èªåãšããŠã¯ãã®éãã«ãã¹ãã ãšæããŸããèè ãããã¯åãã£ãŠãŠp.42ã§ä»ã®èšèªã§ã¯ããã ãã©ãJavaScriptã¯éãããé¢æ°ã®å é ã§å®çŸ©ãããã ïŒãšèšã£ãŠããŸãã9ç« ã®ã¹ã¿ã€ã«ã§ããã®çç±ãšããŠãJavaScriptã ãšå€æ°ãå©çšããåŸã«å®çŸ©ããããšãã§ãããããã¯æžãééãã®ããã«èŠããããšèšã£ãŠããŸãã
å©çšããåŸã«å®çŸ©ãšããã®ã¯ä»¥äžã®ãããªããšããªããšæã£ãŠããŸãã
value = '9980';
console.log(value);
// è²ã
ãªåŠç
var value = '980'; //åå®çŸ©
console.log(value);
確ãã«valueã䜿ã£ãåŸã§var valueãç»å ŽãããšéåæãæããŸãããæžãééããšæããããããŸããã â»1è¡ç®ã®valueã¯ã°ããŒãã«ã«æé»çã«å®çŸ©ãããŸãã7è¡ç®ã®var valueãã°ããŒãã«ã¹ã³ãŒãã«æžãããŠããã®ã§ãã°ããŒãã«ã«å®çŸ©ãããã®ã§ããã
ãããé¢æ°ã®é ã§var count = personCount();ãšããã£ãŠãã®200è¡äžã®forãããã¯ã§countãå©çšãããã¿ãããªã·ãŒã³ãèãããšåŸ®åŠãªæ°ãããŸããå人çã«ã¯Code Completeã«åŸãããã§ãã
falseãšèŠãªãå€
null undefined 空æåå æ°å€0 NaNãfalseãšã¿ãªãããããã§ããã€ãŸã以äžã®ã³ãŒãã¯Validã§ãã
if(null){
console.log('null...');
}else{
console.log('not null!!!');
}
Javaã®å Žåã¯if(null)ãšãæžããšnullãbooleanã«å€æã§ããªããããšããã³ã³ãã€ã«ãšã©ãŒã«ãªããŸãã匷ãéçåä»ãèšèªã®å Žåã¯ã³ã³ãã€ã©ã«ãããã§ãã¯ãå¬ããããã§ãå®éèªåãå¬ããã§ãããã®æ¬ã®çè æ°ã匷ãéçåä»ãèšèªã®ã³ã³ãã€ã©ã«ãããšã©ãŒææã¯æ¬åœã«å¿é ããªããã°ãªããªããšã©ãŒã§ã¯ãªãããã§ãããªãã»ã©ã確ãã«ããèãããšåçåä»ããè¯ãã®ãããããŸãããå®éif(obj)ãšãæžããã®ã¯è¯ããããããŸããã
ã¡ãªã¿ã«ç§ã¯éå»ã«if(val === undefined || var === null âŠã¿ãããªã³ãŒããæžããããšããããŸãã»ã»ã»ã
&&ã§TypeErroräŸå€ãåé¿
ããããNullPointerExceptionçãªå¥Žã§ããäŸãã°myObjãšãããªããžã§ã¯ãããããã©ãããã¯nameããããã£ãæã£ãŠããªãç¶æ³ãèããŸãããã®ãšãmyObj.nameã¯å®çŸ©ãããŠããªãã®ã§undefinedã§ãããmyObj.name.firstNameã¯TypeErrorã«ãªããŸãã
以äžã®ã³ãŒãã®ããã«&&ã䜿ããšåé¿ã§ããŸããfirstNameãå®çŸ©ãããŠããªããã°undefinedã«ãå®çŸ©ãããŠããã°ãã®å 容ïŒä»¥äžã ãštsukaby.comïŒã«ãªããŸãã
var myObj = {};
console.log(myObj.name); //undefined
console.log(myObj.name.firstName); //TypeError
console.log(myObj.name && myObj.name.firstName); //undefined
myObj = {name:{firstName:'tsukaby.com'}};
console.log(myObj.name && myObj.name.firstName); //tsukaby.com
é¢å¿ã¯ããŸãããã䜿ãå Žé¢ã¯ããŸãã¡æ³åã§ããŸããã§ããã
åç §æž¡ã
ãªããžã§ã¯ãã¯åç §æž¡ãã§ããã³ããŒãããããšã¯ãªãããã§ãã
var myObj = {name:'tsukaby'};
var tmpObj = myObj;
console.log(tmpObj.name); //tsukaby
myObj.name = '.com';
console.log(tmpObj.name); //.comã tmpObjãåããªããžã§ã¯ããåç
§ããŠãããã
å人çã«ã¯ããã¯å€æž¡ãïŒã¢ãã¬ã¹ãšããå€æž¡ãïŒã ãšæã£ãŠããŸããJavaãããã§ãåç §æž¡ããšãã人ã¯å€ããã©ãJavaãå€æž¡ãã ãšæã£ãŠããŸããåç §æž¡ãã¯C#ã®refãªã©ãæãã®ã ãšèããŠããŸãã
JavaScriptã¯Javaãªã©ãšåæ§ã®ããã§ãã以äžã®ã³ãŒãã¯changeé¢æ°ã§å€ãå€æŽããäºã¯ã§ããŸããã
function change(target){
 target = {name: 'change'};
}
var myObj = {name:'tsukaby'};
console.log(myObj.name); //tsukaby
change(myObj);
console.log(myObj); //tsukaby
target.name = âchangeâãªãå€ã¯å€ããã®ã§ãããããã¯JavaãCãªãããåãã§ãããããããã¿ã€ãã¯å人çã«ã¯å€æž¡ãïŒã¢ãã¬ã¹æž¡ãïŒãšããèªèã§ãã
ãªããžã§ã¯ãã¯çµå±ãã€ã³ã¿ïŒã¢ãã¬ã¹ïŒãé¢æ°ã«æž¡ããšãã¯å€æž¡ãã¿ããã«èŠããäºã«ããŸããã
ãããã¿ã€ã
ãã¹ãŠã®ãªããžã§ã¯ãã¯ããããã¿ã€ããªããžã§ã¯ããšãªã³ã¯ããŠããŠãããããããããã£ãç¶æ¿ããŠããã
ãããã¿ã€ãã¯æ£çŽæªã ã«è¯ãåãã£ãŠããŸãããããããJavaã§èšããšããã®classã€ãŸããã²ãªåœ¢æ©èœãªã®ããªãšèããŠããŸãã
ããããã£ã®åæ
ãããªããžã§ã¯ãã®ããããã£ãé ã«ãªããŠããããå Žåãfor inæã§ã¯ãªããããããã£ãåæããé å+forã䜿ããšè¯ãããã§ãã
ããããã£ã®åé€
deleteã§ããããã£ãåé€ãããšããããã¿ã€ããã§ãŒã³äžã®åãååã®ããããã£ãèŠããããã«ãªããŸããã¡ãœããããªãŒããŒã©ã€ããããã©ããã£ã±æ¢ããŠãã¹ãŒããŒã¯ã©ã¹ã®ã¡ãœããã䜿ãçãªæãã»ã»ïŒ
å©çšã·ãŒã³ã¯ãŸã æ³åã§ããªãã§ãã
ã°ããŒãã«é åã¯ãªãã¹ã䜿ããªã
ããã¯ã©ã®èšèªã§ãåãã§ãããããJavaScriptã§ã°ããŒãã«é åã䜿ãå Žåã¯var MYAPP = {}ãšããŠããã®MYAPPã«åããããã£ãè¿œå ããŠã°ããŒãã«ã«äœ¿ãããã«ããšã®ããšã§ãã
確ãã«JavaScriptã¯åå空éãªãããäžæãªå€æ°åä»ããŠä»ã®ã©ã€ãã©ãªãšç«¶åããããŸããã§ããäžæã«ããããäœããªãæ¹ãè¯ãã®ã¯ãã®éãã ãšæããŸãã
äžèšã®ã±ãŒã¹ã ãšMYAPPãããããåå空éã«ãªã£ãŠãããã倧äžå€«ãšããèš³ã§ããã
åŒã³åºãã®ãã¿ãŒã³ã§å€ããthis
人ã®ã³ãŒããèªãã§ãããšvar that = this;ãèŠãããŸããåãã¯æå³ãåãããŸããã§ãããthatã£ãŠãªãã ïŒïŒãšãthisã¯å Žåã«ãã£ãŠglobalãæãããšãããã®ã§ããããåé¿ããçºã®æ¹æ³ã§ããã
äŸãã°ä»¥äžã®ã³ãŒãã§ãããmyObj.doSomething()ã¯ã¡ãœããåŒã³åºããã¿ãŒã³ã§ãããdoSomething2()ã¯é¢æ°åŒã³åºããã¿ãŒã³ã§ãããã®ãããthisã®æ±ããéããŸãã
doSomethingã®äžã§thisã¯myObjã§ãããã®ãããthatã«myObjãéé¿ããŠããããšã«ãªããããdoSomething2å ã®1è¡ç®ã§ã¯âmyObj local valueâãããªã³ããããŸããã§ããã2è¡ç®ã§ã¯é¢æ°åŒã³åºããã¿ãŒã³ã®ããthisã¯globalãæããŠãããâglobal valueâãããªã³ããããŸãã
èè ããã®thisã¯èšèšãã¹ãšèšã£ãŠããããã«ãèš³ãåãããŸãããããããããã®ã ãšèŠããŠããããšã«ããŸããã
var myObj = {};
myObj.value = 'myObj local value';
myObj.doSomething = function(){
   var that = this;
   var doSomething2 = function(){
       console.log(that.value); //myObj local value
       console.log(this.value); //global value
   }
   doSomething2();
}
//Global
var value = 'global value';
myObj.doSomething();
 å¯å€é·åŒæ°
é¢æ°å ã§argumentsãšããå€æ°ãèŠããšãåŒæ°ãåããŸãããã®ããfunction()ã®ããã«ä»®åŒæ°ã0åã§ããé¢æ°å ã§argumentsã䜿ãã€ã€ãé¢æ°åŒã³åºãåŽã§()å ã«å®åŒæ°ãäžããããšãããããªããšãå¯èœãããããã«å¯å€é·åŒæ°ã§ããã
é¢æ°ã®æé»ã®return(undefined)
é¢æ°ã¯åžžã«æ»ãå€ãè¿ããã®ã§ãç¹ã«returnã§æå®ããŠããªãå Žåã¯undefinedãè¿ãããã§ããnewããå Žåã¯undefinedã§ãªãthisãè¿ãããã§ãã
å¿ ãreturnãããã ïŒïŒãšæããŸããããæ»ãã®ã¯undefinedã ããå¥ã«ã©ãã§ãè¯ãæ å ±ãªæ°ãããŠããŸããã
throwãããªããžã§ã¯ã
æ £äŸçã«throw {name:âhogeâ, message:âhogeâ};ãšããŠãnameãšmessageãæãããããã§ãã
ã¯ããŒãžã£
é¢æ°å ã«é¢æ°ãæžããããããã«ãã£ãŠå€æ°ãé èœã§ããã®ã¯å°ãé©ãã§ãã
ã«ã¹ã±ãŒã
setterã¡ãœãããªã©ã¯æ®éã¯æ»ãå€ãªãã§ãããthisãè¿ãããã«ããã°ãã§ãŒã³ã§ããŸãããã®ãã§ãŒã³ã®ããšãã«ã¹ã±ãŒããšèšãããã§ããJavaã§ã¯Builderãã¿ãŒã³ã§ããç¥ãããŠãŸãã®ã§ãããã¯ãããªãé ã«å ¥ããŸãããJavaScriptã¯Java以äžã«ãã§ãŒã³ã«ãªã£ãŠãããšãå€ãã§ããã
ãªããžã§ã¯ãæå®å
func(a, b, c, d, e)ã®ããã«åŒæ°ãå€ããªãå Žåã¯ã以äžã®ããã«ããæ¹ãå®å šã ãå¯èªæ§ãäžãããŸããé çªãæ°ã«ããªããŠè¯ãã®ã¯å€§ããã§ããã
func({
age: a,
name: b,
birthday: c,
gender: d,
address: e
});
é åã®èŠçŽ ã®åé€
å€æ°ã¯deleteã§å®çŸ©ãåé€ããŠundefinedã«ããããšãã§ããŸãããé åã®åèŠçŽ ã«äœ¿ãå Žåã¯æ³šæãå¿ èŠã§ãããšããããã¯äœ¿çšçŠæ¢ã§ããã
var arr = ['zero', 'one', 'two', 'three', 'four', 'five'];
console.log(arr); //["zero", "one", "two", "three", "four", "five"]
console.log(arr.length); //6
delete arr[1];
console.log(arr); //["zero", undefined, "two", "three", "four", "five"]
console.log(arr.length); //6
deleteã«ãã£ãŠâoneâã®éšåãundefinedã«ãªã£ãŠããŸããã èŠçŽ ãæžãäºã¯ç¡ãlengthã¯6ã®ãŸãŸã§ãã
äœãšãããããšãã¯array.spliceã䜿ãã°OKã§ãã
var arr = ['zero', 'one', 'two', 'three', 'four', 'five'];
console.log(arr); //["zero", "one", "two", "three", "four", "five"]
console.log(arr.length); //6
arr.splice(1, 1);
console.log(arr); //["zero", "two", "three", "four", "five"]
console.log(arr.length); //5
spliceã«ãã£ãŠindex1ã®å Žæãã1åã®èŠçŽ ãåãé€ããŠããŸããã¡ãããšâoneâãæ¶ããŠlengthã5ã«ãªããŸããã
spliceã¯é ãã®ã§èŠæ³šæ
äžèšã®spliceã¯äŸ¿å©ã§ãããè©°ãæ¿ãåŠçã®ããã§é ãã§ããäŸãã°ä»¥äžã®ã³ãŒããå®è¡ããŠã¿ãŸãã
var arr = [];
var arr2 = [];
for(var i=0; i<100000; i++){
   arr.push(i);
   arr2.push(i);
}
// å
é ãã1ã€ãã€èŠçŽ ãåãé€ã
console.time('timer');
for(var i=0; i<100; i++){
   arr.splice(0, 1);
}
console.timeEnd('timer'); //ç§ããã£ããšãã¯å€§äœ6msã§ãã
// é
åã®åŸãã®æ¹ã«å¯ŸããŠïŒã€ãã€èŠçŽ ãåãé€ã
console.time('timer2');
for(var i=0; i<100; i++){
   arr2.splice(90000, 1);
}
console.timeEnd('timer2'); //ç§ããã£ããšãã¯å€§äœ1msã§ãã
èŠçŽ æ°10äžã®ïŒã€ã®é åã«å¯ŸããŠspliceã100åãã€å®è¡ããå®è¡æéãèšæž¬ããŸããäœåããããŸããããsplice(0, 1)ã®æ¹ã¯6msã»ã©ã§ãsplice(90000, 1)ã®æ¹ã¯1msã»ã©ã§ããã
åè ã®æ¹ãåé€ããèŠçŽ ã®åŸã®éšåã倧ããã®ã§è©°ãæ¿ãã倧éã«çºçããŠãã®åæéãããããŸãã
ã©ããã£ãŠåé¿ãããã§ãããäŸãã°é£çµãªã¹ã(Linked List)ãšãã§ãããããèŠçŽ ãåé€ããŠããªã³ã¯(ãã€ã³ã¿)ãå°ãç¹ãå€ããã ããªã®ã§è©°ãæ¿ãåŠçã¯çºçããæ©ããšæããŸããå°ãæ¢ããŸããããé£çµãªã¹ãã¯èŠã€ããããŸããã§ããããããããŠæšæºã§ã¯çšæãããŠããªãã®ã§ããããã
array.sortã¯stringãšããŠåŠçããã
var arr = [3, 297, 45, 99, 5, 1000, 27];
console.log(arr); //[3, 297, 45, 99, 5, 1000, 27]
arr.sort();
console.log(arr); //[1000, 27, 297, 3, 45, 5, 99]
sortã¯é åã®äžèº«ã®åãŸã§ã¯æèããŠãããªãããããæååãšããŠåŠçããããã§ãããªã®ã§ãäžèšã®ãããªããšã«ã
èè ã¯æ¯èŒé¢æ°ãèªåã§çšæããã°å¯Ÿå¿ã§ãããšèšã£ãŠããŠãå®é以äžã¯æ£ããåããŸãã
var arr = [3, 297, 45, 99, 5, 1000, 27];
console.log(arr); //[3, 297, 45, 99, 5, 1000, 27]
arr.sort(function(a, b){
   return a - b;
});
console.log(arr); //[3, 5, 27, 45, 99, 297, 1000]
æ°å€ã®ã³ã³ãã¬ãŒã¿ãèªåã§çšæããªããŠã¯ãªããªããšããããšã«çŽåŸãè¡ããŸãããã»ã»ã»ããšããããæ°å€ã®ãœãŒãã¯æ®éã«ã¯ã§ããªããšèŠããŠããäºã«ããŸããã
ã»ãã³ãã³æ¿å ¥æ©èœã®æ³šæ(returnæ)
return
{
hoge: 'hoge'
};
ãšããªã£ãŠãããšãreturnã®åŸã«èªåã§;ãæ¿å ¥ãããŠundefinedãæ»ãå€ã«ãªã£ãŠããŸããããªãK&Rã¹ã¿ã€ã«ã«åŸã£ãŠ{ã¯è¡æ«ã«çœ®ããŠreturn {ãšããã®ãè¯ãããã§ãã
æµ®åå°æ°ç¹æ°ã®å ç®
JavaScriptã§ã¯0.1+0.2=0.30000000000000004ã§ããããã¯ã©ã®èšèªã§ã倧æµããã§ã泚æããªããã°ãªããªãåé¡ã§ããä»ã®èšèªã ãšDecimalã¿ãããªã¯ã©ã¹ã䜿ã£ãŠèª€å·®ãé²ããŸãããJavaScriptã§ã¯ã¹ã±ãŒãªã³ã°ããŠå¯Ÿå¿ããããã§ãã
var num = 0.1*10 + 0.2*10;
console.log(num/10);
10åããŠãã£ãåŸã«10ã§å²ã£ãŠãããšãçµå±ä»ã®èšèªã®Decimalãšããšåãä»çµã¿ã§ããã
function hoge(){}ãšvar hoge = function(){}ã¯åã
ãã£ãšçåã«æã£ãŠããã®ã§ããã®æ¬èªãã§ããã£ãã§ãã
varã®æ¹ã ããå©çšããæ¹ãè¯ããããããŸãããããããã¯ããããããšæããŸããçç±ã¯æ¬ãåç §ãã ããã
ä»é²
åç« ã®ãŸãšããèŒã£ãŠããã®ã§ãããã£ãšåŸ©ç¿ããããšãã«ã¯äŸ¿å©ããšæããŸãã
JSLintã®è§£èª¬ãèŒã£ãŠããŠãè¯ãã³ãŒãã£ã³ã°ã«åœ¹ç«ã€ãšæããŸããã§ãããæè¿ã ãšJSHintã®æ¹ã話é¡ã«ãªã£ãŠããŸãããèªåãJSHintã䜿ã£ãŠããã®ã§ããã®ç¹å€ãæ¬ã ãªãŒãšæããŸããã
æå³ãåãããªãç¹ããããŸãããçŽåŸã§ããªãç¹ããããŸããããããªãå匷ã«ãªããŸããã
JavaScriptåããã°ããã®äººã«ã¯è¯ãæ¬ããšæããŸããã¿ãªããããããã§ããïŒ