JavaScriptã¨ä»ã®ããã°ã©ãã³ã°è¨èªã¨ã®éã
ãã®ã¡ã¢ã¯ãJavaScriptã¨ããã以å¤ã®ããã°ã©ãã³ã°è¨èªã¨ã®éãã解説ãããã®ã§ãã
ä»ã®æ¹ã®è§£èª¬ãèªãã¨ãå
±éç¹ã強調ããã¦ããã®ã§ããã®ã¡ã¢ã§ã¯ãéã«ãéãç¹ã調ã¹ã¦ãããã¨ããããã§ãã
å人çãªã¡ã¢ãªã®ã§ãééããããã¾ããè¦ã¤ããããããã«è¨æ£ãã¾ãã
Cè¨èªãC++ãPerlãJavaãPHPãRubyãªã©ãJavaScript以å¤ã®è¨èªãç¿å¾ãã¦ãã人ã念é ã«ããã¦ãã¾ããã³ã¡ã³ãã¯/*ã¨*/ã¨ã®éã«æã¿ã¾ãã
ãªãã©ã«
JavaScriptã«ã¯ã¯ã©ã¹ã¯ããã¾ããã6種é¡ã®ãªãã©ã«ã使ã£ã¦ãªãã¸ã§ã¯ããä½ãã¾ãã
ãªãã©ã«ã¯ä¾¿å©ãªã®ã§ãã使ããã¾ãã
var a = {}, /*ãªãã¸ã§ã¯ããªãã©ã«*/ b = 12, /*æ°å¤ãªãã©ã«*/ c = "Hello, World!", /*æååãªãã©ã«*/ d = /a+b*c?/g, /*æ£è¦è¡¨ç¾ãªãã©ã«*/ e = [], /*é åãªãã©ã«*/ f = function(){}; /*é¢æ°ãªãã©ã«*/
ã¡ã½ãã
ãªãã¸ã§ã¯ãã¯ãã¡ã½ãããå¼ã³åºããã¨ãå¯è½ã¨ãªãã¾ãããã®ã¡ã½ããã使ã£ã¦ãJavaScriptã§ã¯å¦çãé²ãã¦ããã¾ãã
ã¡ã½ããã®å®ç¾©
ãªãã¸ã§ã¯ããªãã©ã«ãé¢æ°ãªãã©ã«ã¨ã³ãã³ãçµã¿åããã¦å®ç¾©ãã¾ãã
var a = { hoge: function() {}, hogehoge: function() {} };
ã¡ã½ããã®å¼ã³åºã
ã¡ã½ããã®å¼ã³åºãã¯ããããè¨ãã¦ãã¡ã½ããåã«()ãä»ããã°OKã§ãã
a.hoge();
å¤æ°ã使ããã«ããªãã©ã«ã¨ãããã使ã£ã¦ãç´æ¥ã¡ã½ãããå¼ã³åºããã¨ãå¯è½ã§ãã
"abcdefghijk".indexOf("c"); /*2ãè¿ã*/
ã¡ã½ããã®æ¸ãæã
ã¡ã½ããã¯åçã«æ¸ãæãããã¨ãã§ãã¾ããã¾ããå¾ããæ°ãã追å ãããã¨ãå¯è½ã§ãã
a.hoge = function(){}; a.ne = function(){};
é¢æ°å¼ã³åºã
é¢æ°ãªãã©ã«ãéãã¦ãé¢æ°ãå¼ã³åºããã¿ã¼ã³ã¯5éãããã¾ã (注1)ãããããé¢æ°ãå®è¡ãããã¨ãã§ãã¾ãã
var f = function(){}, obj = { hoge : function(){} }; /*é¢æ°å¼ã³åºããã¿ã¼ã³*/ f(); /*apply (call) å¼ã³åºããã¿ã¼ã³*/ f.apply(); f.call(); /*ã¡ã½ããå¼ã³åºããã¿ã¼ã³*/ obj.hoge(); /*å³æé¢æ°*/ (function(){})(); /*ã³ã³ã¹ãã©ã¯ã¿å¼ã³åºããã¿ã¼ã³*/ new f();
var
é¢æ°ã®å
é¨ããå¤é¨ã®ã©ããã§varãæå®ããªãéããå¤æ°ã¯ã°ãã¼ãã«å¤æ°ã¨ãã¦æ±ããã¾ãã
é¢æ°ãå¼ã³åºãç´åã«ãé¢æ°ãããã¯å
ã§ãvarã§æå®ãããå¤æ°ã¯undefinedã¨ãªãã¾ãããã®ããã以ä¸ã®ãããªå¥å¦ãªãã¨ãèµ·ãã¾ãã
hoge = 12; /*hogeã¯ã°ãã¼ãã«å¤æ°*/ var f = function() { hoge; /*ä¸ã«varãã¤ãã¦ããããã12ã§ã¯ãªãã¦ãundefinedã¨ãªã*/ var hoge; }; ãf(); ãvar df = function() { var hoge = hoge; /*varã®åæåãå ã«å®è¡ãããã®ã§ãããã12ã§ã¯ãªãã¦ãundefined*/ }; df();
ã¾ãããããã¯ã¹ã³ã¼ãã¯ããã¾ããã®ã§ããããã¯ã®å¤ããå¤æ°ã«ã¢ã¯ã»ã¹å¯è½ã§ãã
var f = function() { for (var i=0;i < 12; i++) {} i; /*ãã®ã¨ããiã¯11*/ }; ãf();
ã¯ãã¼ã¸ã£
ãã©ã¤ãã¼ãå¤æ°ã¯ããã¾ãããã¯ãã¼ã¸ã£ã使ã£ã¦ããã©ã¤ãã¼ãå¤æ°ã®ä»£ããã¨ãã¾ãã
ã¯ãã¼ã¸ã£ã使ãã°ãå
é¨ã®é¢æ°ã¯ãå¤å´ã®é¢æ°ã§å®ç¾©ãããå¤æ°ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
以ä¸ã®ã³ã¼ãã§ã¯ãã¡ã½ããã§ãããã©ã¤ãã¼ãå¤æ°ã«ã¢ã¯ã»ã¹ã§ãã¾ãããã¾ããthat = thisã¨ããã®ã¯ãã³ã³ã¹ãã©ã¯ã¿å¼ã³åºããã¿ã¼ã³ã®å ´åãã¡ã½ããã®åç
§å¼æ¸¡ãã§thisã«åé¡ãèµ·ãããããåé¿ãã対çãæã£ã¦ãã¾ãã
var F = function(){ var that = this, a; /*ãã©ã¤ãã¼ãå¤æ°ã¨ãã¦ä½¿ã*/ that.getA = function() { return a; }; that.setA = function(n) { a = n; }; that.hoge = function() { return that.getA(); }; }; var obj = new F(); obj.setA(1); obj.getA(); /*1ãè¿ã*/ obj.setA(2); obj.getA(); /*2ãè¿ã*/ obj.hoge(); /*2ãè¿ã*/
ç¶æ¿
ããã«applyå¼ã³åºããã¿ã¼ã³ã使ã£ã¦ãã·ã³ãã«ãªç¶æ¿ãå®ç¾ã§ãã¾ãã
ç¶æ¿ããããã³ã³ã¹ãã©ã¯ã¿é¢æ°ããSuper1ãSuper2ã¨Super3ã¨ååãä»ãã¦ããã¾ãããããããã®ã¡ã½ããããã¹ã¦ãChildé¢æ°ã«ç¶æ¿ããã¾ãã
var Super1 = function(){ var that = this, a; /*ãã©ã¤ãã¼ãå¤æ°ã¨ãã¦ä½¿ã*/ that.getA = function() { return a; }; that.setA = function(n) { a = n; }; that.hoge = function() { return that.getA(); }; }, Super2 = function(){ var that = this; that.ho = function(){}; }, Super3 = function(){ var that = this; that.h = function(){}; }, Child = function() { var that = this; /*å¤éç¶æ¿ãå§ãã*/ ããããããSuper1.apply(that, arguments); ããããããSuper2.apply(that, arguments); ããããããSuper3.apply(that, arguments); that.n = function() { return that.hoge(); }; }; var obj = new Child(); obj.setA(2); obj.hoge(); /*2ãè¿ã*/ obj.ho(); obj.h(); obj.n(); /*2ãè¿ã*/
çµè«
- JavaScriptã¯ã¯ã©ã¹ããªã
ãããªãã¸ã§ã¯ãæåè¨èªã§ã¯ãªããã©ãç¶æ¿ãªã©åããã¨ã¯ã§ãã¾ã - éçºãããã¨ãã¯ãããããããé¿ããããã«ãã©ã¤ãã©ãªã使ãã®ããã¹ã
ç¶ã
JavaScriptã®å¤éç¶æ¿ã¨ãªã¼ãã©ã¤ã - JavaScriptã®å人çãªã¡ã¢ (ãã®2)
http://d.hatena.ne.jp/dhrname/20121004
ãããã¿ã¤ããã§ã¼ã³ã«ã¤ãã¦ã¯æ©ä¼ãããã°ã¡ã¢ã¨ãã¦æ´çãã¦ããããã§ãã
åç §
注1
Douglas Crockfordãè æ°´éè²´æã訳ãJavaScript: The Good Parts ãè¯ããã¼ããã«ãããã¹ããã©ã¯ãã£ã¹ããP32ãã
è¿½è¨ (2012å¹´10æ13æ¥)
JavaScriptã¯ãããã¿ã¤ããã¼ã¹ã®ãªãã¸ã§ã¯ãæåè¨èªã¨ããèãããããã§å¤ãè¦ããã¾ããã®ã§ããªãã¸ã§ã¯ãæåã«é¢ããè¨è¿°ãåé¤ãã¦ããã¾ããã