ã¯ããã«
ãDojoãã¼ã«ãããã§ã¯JavaScriptã®ãªãã¸ã§ã¯ãæåéçºãå©ããããã«ãJavaScriptã®è¨èªä»æ§ãæ¡å¼µãã¦ãã¾ããæ¬è¨äºã§ã¯ããã®åºæ¬ã¨ãªãDojoãã¼ã«ãããã§ã®ã¯ã©ã¹å®£è¨ãç¶æ¿ã®æ¹æ³ã解説ãã¾ãã
ããã®è¨äºã§ã¯ãDojoãã¼ã«ãããã®å©ç¨æ¹æ³ãæ¢ã«ç¥ã£ã¦ãããã¨ãåæã«ãã¦ãã¾ãã
JavaScriptã®ãªãã¸ã§ã¯ãæåã¨ã¯ï¼
ãJavaScriptã¨è¨ãã°ãã¹ã¯ãªããè¨èªããé¢æ°ããHTMLã®ä»å±åãã¨ãããããªã¤ã¡ã¼ã¸ãå¼·ãã¨æãã¾ããããããJavaScriptã§ããªãã¸ã§ã¯ãæåéçºãå¯è½ãªãã¨ãçããã¯ãåãã§ããããã
ããªãã¸ã§ã¯ãæåã¨ããè¨èãèãã¦ããªãã¸ã§ã¯ãæåãç¥ã£ã¦ãã人ãªãä½ãæãæµ®ãã¹ãã§ãããããå¤ãã®å ´åãã¯ã©ã¹ããç¶æ¿ããã¤ã³ã¹ã¿ã³ã¹ããã¯ã©ã¹å¤æ°ããã¯ã©ã¹ã¡ã½ããããæ½è±¡ã¯ã©ã¹ãã¨ãããããªãã¨ãæãæµ®ãã¹ãã«éãããã¾ãããããããJavaScriptã®ãªãã¸ã§ã¯ãæåã¯ããã¨ã¯éãæ¦å¿µã§åä½ããããã«ãªã£ã¦ãã¾ãã
ãJavaScriptã§ã¯ãã»ã¨ãã©ã®ãã®ããªãã¸ã§ã¯ãã¨ãã¦æ±ã£ã¦ãã¾ãããªãã©ã«ãå¤æ°ãã¡ã½ããããããªãã¸ã§ã¯ãã§ãããªãã¸ã§ã¯ãã«ã¯åç
§åã¨å¤ããããè¤åç©ãé
åãä½ããã¨ãã§ãã¾ããè¤åç©ã¯ { 㨠} ã§å²ãã ãåå:å¤
ã並ã¹ããã®ã¨ãªãã¾ããéãã«ã³ãã,ãã§åãã¾ããé
å㯠[ 㨠] ã§å²ã¿ãéãã«ã³ãã,ãã§åãã¾ããé
ç®ã«ååã¯ä»ãããã¾ããã
people1 = { name : "ç±³æ幸寿", getName : function() { return this.name; } }
methods = [ function() { /* ãªãããã®å¦ç */ }, function() { /* ãªãããã®å¦ç */ }, function() { /* ãªãããã®å¦ç */ } ]
ãã¯ã©ã¹ã®ãããªãã®ãä½ãã¤ã³ã¹ã¿ã³ã¹åãããã¨ãã§ãã¾ãããã®å ´åãã³ã³ã¹ãã©ã¯ã¿ã¼ã¨ãªãé¢æ°ãå®ç¾©ãããã®ä¸ã«ã¯ã©ã¹å¤æ°ãã¯ã©ã¹ã¡ã½ãããå®ç¾©ãã¦ããã¾ããã¾ããããããã¿ã¤ããã¨ããæ©è½ãããããã®æ©è½ãå©ç¨ããã¨ç¶æ¿ãå¯è½ã§ãããã¡ã½ãããå·®ãæ¿ãããã¨ãã§ãã¾ãã®ã§ã¡ã½ããã®ãªã¼ãã¼ã©ã¤ããå¯è½ãã¨ãããã¨ã«ãªãã¾ãã
ããããããããã¯ã©ã¡ããã¨è¨ãã¨ãæä½ããæãã£ã·ãã®ãªãã¸ã§ã¯ãæåã§ããä¸è¬çã«ãªãã¸ã§ã¯ãæåè¨èªã¨ãã¦ç¥ããã¦ããC++ãJavaãC#ã®ãããªå®£è¨åã®ãªãã¸ã§ã¯ãæåè¨èªã¨ã¯éãæè¦ã§ããã°ã©ãã³ã°ããã¾ããããããããããããã¨ãèæ ®ããªãã¦ã使ããç¹ããJavaScriptããªãã¸ã§ã¯ãæåè¨èªã¨ãã¦ãã¾ãèªç¥ãããªãçç±ããããã¾ããã
ããã®è¨äºã¯prototypeãåºæ¬ã¨ããJavaScriptã®ãªãã¸ã§ã¯ãæåãå¦ãã§ããããã¨ãããã®ã§ã¯ãªãã®ã§ããããããã§ããã¦ããã¾ãããã¤ã³ãã¯ãJavaScriptã®ãªãã¸ã§ã¯ãæåã¯ãããç¥ããã¦ãããã®ã¨ã¯ä½ãéããã¨ãããã¨ã§ãã
ã¯ã©ã¹å®£è¨ãdojo.declareã
ãDojoãã¼ã«ãããã§ã¯ãã³ã¢ã©ã¤ãã©ãªã«ããã¦JavaScriptã®è¨èªä»æ§ãæ¡å¼µããä¸è¬çãªãªãã¸ã§ã¯ãæåè¨èªã«ä¼¼ãå½¢ã§ãªãã¸ã§ã¯ãæåéçºï¼ã¯ã©ã¹å®£è¨ãã¤ã³ã¹ã¿ã³ã¹åï¼ãå®ç¾ã§ããããã«ãã¦ãã¾ãã
ãDojoãã¼ã«ãããï¼ã³ã¢ï¼ããã¼ãããã¦ããã¨ãJavaScriptã«ããã¦ãdojo.declare
ãé¢æ°ã§ã¯ã©ã¹å®£è¨ããããã¨ãã§ãã¾ãã
ãããã§ãããããä¸è¬çãªãªãã¸ã§ã¯ãæåã«è¿ãå½¢ã«ãªãã¾ãããããè足ã«ãªãã¾ãããJavaã§æ¸ãã¨ããããããªãã§ãããã
package myModule; public class myClass extends myBaseClass { public myProp= ""; public myClass(??? myProp){ this.myProp = myProp; … } public ??? myFunc(){ … } }
ãdojo.declareã使ãå ´åãååã«æ¥é èªï¼ããªãã£ã¯ã¹ãåå空éãã¿ãããªãã®ï¼ãä»ãããã¨ãã§ããããã«ãªãã¾ããä¾ãã°ãyone.MyClassã®ããã«ã§ãã
ããã¦ãã§ã¯å®éã«åãã³ã¼ãã§åä½ã確èªãã¦ããã¾ããããsample01.htmlãè¦ã¦ãã ãããdojo.require ã®ç´å¾ã«dojo.declareãããã¾ããããã§ã¯ãyone.MyClassã宣è¨ããã¾ããä½ãç¶æ¿ãã¦ããªãã®ã§äºçªç®ã®å¼æ°ã«nullãæå®ããã¦ãã¾ããã³ã³ã¹ãã©ã¯ã¿ã¼ãã¯ã©ã¹å¤æ°ãã¯ã©ã¹ã¡ã½ãããããã¾ããã
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ã¯ã©ã¹ã®å®ç¾©ï¼1ãã¡ã¤ã«ãã¼ã¸ã§ã³</title> <script type="text/javascript" src="../dojo-release-1.1.1/dojo/dojo.js" djConfig="parseOnLoad: true"></script> <script type="text/javascript"> dojo.require("dojo.parser"); dojo.require("dijit.form.Button"); dojo.declare(
"yone.MyClass",
null,
{
}
); dojo.addOnLoad(function(){ dojo.connect(dojo.byId("button1"), "onclick", function(){ var myClass = new yone.MyClass(); alert(myClass); }); }); </script> </head> <body> <div dojoType="dijit.form.Button" id="button1">ã¯ã©ã¹çæ</div> </body> </html>
ããã®ã³ã¼ãã§ã¯ããã¼ããããã¨ãã«dojo.declare()
ãå¼ã°ããyone.MyClassãå®è¡ããã¾ãããã¿ã³ãã¯ãªãã¯ããã¨newããããã®ãªãã¸ã§ã¯ããçæããalertã§è¡¨ç¤ºããã¾ããå®è¡ããã¨æ¬¡ã®ããã«ãªãã¾ãã