FAQå½¢å¼ã«ããJavaScriptã®æ¬è³ªããããè¶ å ¥é
ã¯ããã«
JavaScriptã¯ç°¡åãªè¨èªã®ããã§ãã¦ãå®ã¯å¥¥ãæ·±ããåå¿è ã«ã¨ã£ã¦ãªããªããã®æ¬è³ªããããã«ããè¨èªã§ããããã§ã¯ãJavaScriptã®è¨èªçã¨ãã»ã³ã¹ãç解ã§ãããããªãã®ãFAQå½¢å¼ã§æ¸ãã¦ã¿ã¾ããããæè¦ã誤ãçãææãã¦ãããããå¬ããã§ãã
ãªããJavaScriptã®æ¨æºåã§ããECMAScriptã¯ãä»å¹´æ«ã«Edition 5 ããªãªã¼ã¹ãããäºå®ã§ãããã®FAQã¯ãç¾å¨ã®ãã¼ã¸ã§ã³ã§ããEdition 3ããã¼ã¹ã«ãã¦ãã¾ãã
ECMAScriptã£ã¦ä½ã§ããï¼
ECMAã¯European Computer Manufacturer Association(欧å·é»åè¨ç®æ©å·¥æ¥ä¼)ã®ç¥ã§ãæ¨æºåå£ä½ã§ããNetscapeã®JavaScriptã¨Microsoftã®JScriptããã¼ã¹ã«ãç´ç²ãªããã°ã©ã è¨èªé¨åãæ½åºãããã®ãECMAã§æ¨æºåããã¹ã¯ãªããè¨èªã§ãã1998å¹´ã«å¶å®ãããECMA262 3rd Edtionãç¾å¨ã®ä»æ§ã¨ãªã£ã¦ãã¾ãã
ECMAScriptã¯JScriptãJavaScriptã¨ä½ãã¡ããã®ï¼
Microsoftã®JScriptãJavaScrpt(ããã§ã¯FirefoxãMozillaã®JavaScript)ã¯ãã©ã¦ã¶ã«çµã¿è¾¼ã¾ããã¹ã¯ãªããè¨èªã¨ãã¦DOMãCSSé¢é£ã®APIãæä¾ããã¦ãã¾ãããECMAScriptã§ã¯ãããã®APIã¯è¦å®ãã¦ããªããããã¤ãã®æ±ç¨ã¯ã©ã¹ã®ã¿ãå®ç¾©ããã¦ããç´ç²ãªããã°ã©ãã³ã°è¨èªã§ããJScript ãJavaScriptãECMAScriptã®ã¹ã¼ãã¼ã»ããã¨ããä½ç½®ã¥ãã§ãããECMAScriptã§è¦å®ããã¦ããAPIé¨åã«å¯¾ãã¦ãè¥å¹²ã®åä½ã®éããããã¾ããã¾ããFlashã®ã¹ã¯ãªããè¨èªã§ããActionScriptãECMAScriptããã¼ã¹ã«ç¬èªã«æ¡å¼µããè¨èªã«ãªã£ã¦ãã¾ãã
ECMAScriptã§å®ç¾©ããã¦ããäºç´èªãæãã¦ãã ããã
ECMAScript Edtion 3 ã§ã¯ä»¥ä¸ã®ãã®ããã¼ã¯ã¼ãã¨ãã¦å®ç¾©ããã¦ãã¾ãã
break | else | new | var | case | finally | return | void |
catch | for | switch | while | continue | function | this | with |
default | if | throw | delete | in | try do | instanceof | typeof |
ããã«ã以ä¸ã®ãã®ãå°æ¥ã®ããã®äºç´èªã¨ãã¦å®ç¾©ããã¦ãã¾ãã
abstract | enum | int | short | boolean | export | interface | static |
byte | extends | long | super | char | final | native | synchronized |
class | float | package | throws | const | goto | private | transient |
debugger | implements | protected | volatile | double | import | public |
ããã§ãtrue, false, null, undefined, NaN, Infinityãªã©ã¯ãã¼ã¯ã¼ãã§ãäºç´èªã§ããªããã¨ã«æ³¨æãã¦ãã ãããtrue, false, nullã¯ãªãã©ã«ã§äºç´èªã§ãããundefined, NaN, Infinityã¯åãªãã°ãã¼ãã«å¤æ°ã§ãããªãã©ã«ã«ã¯å¤ã代å
¥ãããã¨ã¯ã§ãã¾ããããå¤æ°ã«ã¯ä»£å
¥ã§ãã¾ãã
true = 0; // syntax error; NaN = 1; // OK undefined = "hello" // OK
NaNã£ã¦ä½ã§ããï¼
NaNã¯Not a Numberã®ç¥ã§ãIEEE754ã§å®ç¾©ããã¦ããæ°å¤è¡¨ç¾ã®ä¸ç¨®ã§ããJavaScriptã§ã¯æ°å¤ã¯64bitå精度ã®æµ®åå°æ°è¡¨ç¾ãç¨ãã¦ãã¾ããå ¨ã¦ã®NaNã¯ãäºãã«åºå¥ã§ããªããããããå¤æ°(x)ã®å¤ãNaNãã©ãããæ¯è¼ããã®ã« x == NaN ã§ã¯ãã¡ã§ããisNaN(x) ã¨ããã°ãã¼ãã«é¢æ°ãããã¾ãã®ã§ãisNaN(x)ãå¿ ã使ãããã«ãã¾ããããNaN == NaNãfalseã«ãªãã¾ãã
ãã®ä»ã«ãIEEE754ã§å®ç¾©ããã¦ããé常ã®æ°å¤ä»¥å¤ã®ãã®ã¯Infinity/-Infinity ãããã¾ããé常ã®æ°å¤ãã©ããã®å¤å®ã«ã¯isFinite(x)ã¨ããã°ãã¼ãã«é¢æ°ãããã¾ãã®ã§ããããå©ç¨ãã¦ãã ããããã®é¢æ°ã¯å¼æ°xã NaN, Infinity, -Infinity以å¤ã®ã¨ãtrueãè¿ãã¾ãã
ä¸æ¹ã-0ã¨ãããã®ãããã¾ããã¼ãã«ã符å·ãããã¾ãã-0ãæå³ã®ãã使ãæ¹ãããå ´åã¯ã»ã¨ãã©ããã¾ããããä¸è¨ã®ã³ã¼ãã§ã¯xã«ã¯Infinityãä»£å ¥ããã¾ãããy ã«ã¯-Infinityãä»£å ¥ããã¾ãã
x = 1 / 0; y = 1 / -0;
undefined ãNaNã®å¤ãå¤ãããããªãã¦é常ã«åé¡ããããã¾ãããï¼
ã¯ããåé¡ã¨ãªãå ´åãããã®ã¯ç¢ºãã§ããå®ã¯ããããã®å¤æ°ã®ä»ã«ãè¦æ ¼æ¸ã§å®ç¾©ããã¦ããçµè¾¼é¢æ°ãªã©ãã¦ã¼ã¶ããã°ã©ã ã«ãã£ã¦å¤æ´ãããã¨ãã§ãã¦ãã¾ãã¾ãããã®ãããªè¨èªä»æ§ã¨ãããã¨ã§ç解ãã¦ãã ãããéã«ãããã«ããé常ã«åçã§æè»ãªããã°ã©ãã³ã°ãå¯è½ã¨ãªãã¾ãã
ã¡ãªã¿ã«ãundefiend, NaN, Infinityã¯ãã®å¤æ°ã®å¤ãå¤æ´ããã¦ã以ä¸ã®å¼ã§å¿ ãå ã®å®ç¾©ãããå¤ãå¾ããã¨ãã§ãã¾ãã
my_undefined = void 0; my_NaN = 0 / 0; my_Infinity = 1 / 0;
ã¾ããNumberãªãã¸ã§ã¯ãã«ã¯ãReadOnlyã®ããããã£ã¨ãã¦Number.NaN,Number.POSITIVE_INIFINITY, Number.NEGATIVE_INFINITYãªã©ãå®ç¾©ããã¦ãã¾ãã
ããããã£ã®attributeã«ã¯ReadOnly以å¤ã«ä½ãããã¾ããï¼
ReadOnly, DontDelete, DontEnumã®ï¼ç¨®é¡ããã¾ããReadOnlyã¯ä»£å ¥çã§ããããã£ã®å¤ãå¤æ´ã§ããªããã¨ãDontDeleteã¯ããããã£èªèº«ãåé¤ã§ããªããã¨ãDontEnumã¯for-inæ§æã§åæã§ããªããã¨ãæå³ãã¾ãããããã¯ãã«ãã¤ã³ãªãã¸ã§ã¯ãã«å¯¾ãã¦å®ç¾©ããã¦ãããã¦ã¼ã¶ãå®ç¾©ãããªãã¸ã§ã¯ãã®ããããã£ã«ã¯ãã¤ãããã¨ã¯ã§ãã¾ããã
thisã®æå³ãããããããªããã§ãããC++ãJavaã®thisã¨ä½ãéãã®ï¼
C++ãJavaã®thisã¨ã ãããåãã§ãããé常ã®é¢æ°ã®ä¸ãã©ã®é¢æ°ã«ãå±ããªãé¢æ°ã®å¤ã®ã³ã¼ãã§ãthisã使ããã¨ãã§ãã¾ãããã®å ´åãthisã«ã¯Globalãªãã¸ã§ã¯ãããã¤ã³ãããã¦ãã¾ããJavaScriptã§ã¯thisã«ã¯å¸¸ã«ä½ããã®ãªãã¸ã§ã¯ãããã¤ã³ãããã¦ãã¾ããthisãnullã«ãªããã¨ã¯ããã¾ããã
ã°ãã¼ãã«ã³ã¼ãã£ã¦ã©ãããæå³ã§ããï¼
ãããã¬ãã«ã®é¢æ°ã®å¤ã§å®è¡ãããã³ã¼ãã®ãã¨ã§ãããããã¬ãã«ã«ã¯é¢æ°ã¨ãã以å¤ã®ä»£å ¥æãifæãªã©ä¸è¬ã®æãæ¸ããã¨ãã§ãã¾ãããã®é¢æ°ä»¥å¤ã®ã³ã¼ããã°ãã¼ãã«ã³ã¼ãã¨å¼ã³ã¾ããããã°ã©ã ãæåã«å®è¡ããã¨ãã«ã¯ãé¢æ°å®ç¾©ãèªã¿è¾¼ãã å¾ã«ããã®ã°ãã¼ãã«ã³ã¼ããæåã«å®è¡ããã¾ãã
ã°ãã¼ãã«ãªãã¸ã§ã¯ãã£ã¦ä½ã§ããï¼
JavaScriptã§ã¯å ¨ã¦ã®ãã¼ã¿ã¯ä½ãã®ãªãã¸ã§ã¯ãã®ããããã£ã®å¤ã«ãªã£ã¦ãã¾ãããã°ãã¼ãã«é¢æ°ãã°ãã¼ãã«å¤æ°ãªã©ãå±ãããªãã¸ã§ã¯ããã°ãã¼ãã«ãªãã¸ã§ã¯ãã§ããECMA262ã§ã¯ã°ãã¼ãã«ãªãã¸ã§ã¯ãã«ã¯ç¹å®ã®ã·ã³ãã«ãå²ãå½ã¦ããã¦ãã¾ãããã°ãã¼ãã«ã¹ã³ã¼ãã§ã¯thisãã°ãã¼ãã«ãªãã¸ã§ã¯ãã«ãªãã®ã§ãã°ãã¼ãã«ã³ã¼ãã®å é ã§ã
var Global = this;
ãªã©ã¨ããã¨ããã¼ã«ã«å¤æ°çã§ä¸æ¸ããããªãéããä»»æã®ã¹ã³ã¼ãã§Globalã§ã°ãã¼ãã«ãªãã¸ã§ã¯ããåç §ã§ããããã«ãªãã¾ãã
ã¡ãªã¿ã«ãWebãã©ã¦ã¶ã§ã¯windowå¤æ°ãã°ãã¼ãã«ãªãã¸ã§ã¯ããåç §ããããã«ãªã£ã¦ãã¾ãã
ã¨ããã§ããªãã¸ã§ã¯ãã£ã¦ä½ã§ããï¼
JavaScriptã®å ´åãããããã£ã¨å¤ã®çµã®éåããªãã¸ã§ã¯ãã¨è¨ããã¨ãã§ãã¾ããããããã£ã®å¤ã«ã¯ãæ°å¤ãæååããªãã¸ã§ã¯ããé¢æ°ãªã©ä½ã§ãå¤ã«ãããã¨ãã§ãã¾ããæ¦å¿µçã«ã¯ãã°ãã¼ãã«å¤æ°ããã¼ã«ã«å¤æ°ãé¢æ°ã®å®å¼æ°ãé¢æ°èªèº«ãå é¨é¢æ°ãªã©å ¨ã¦ã®å¯¾è±¡ã¯ä½ããã®ãªãã¸ã§ã¯ãã®ããããã£ã®å¤ã¨ãã¦å®ç¾©ããã¾ãã
é¢æ°ãå®ç¾©ããåã«ãã®é¢æ°ãã³ã¼ã«ã§ããã®ï¼
ã¯ããã§ãã¾ããä¸è¨ã®ã³ã¼ãã¯åé¡ãªãå®è¡ãããvã«ã¯246ãä»£å ¥ããã¾ãã
v = myfunc(123); function myfunc (x) { return x * 2; }
åãååã®é¢æ°ãè¤æ°åå®ç¾©ãããã©ããªãã®ï¼
æå¾ã«å®ç¾©ãããé¢æ°ãæå¹ã«ãªãã¾ããä¸ã®ããã°ã©ã ãå®è¡ããã¨
myfunc(); function myfunc() { print("Hello"); } myfunc(); function myfunc() { print("World"); } myfunc();
以ä¸ã®ããã«Worldãï¼å表示ããã¾ãã
World World World
å¤æ°ã¯å®£è¨ããªãã¦ã使ããã®ï¼
ã¯ãã使ãã¾ãããã ãã宣è¨ããå ´åã¨ããªãå ´åã¨ã§ã¯åä½ãç°ãªãã¾ããé¢æ°ã®ä¸ã§å®£è¨ããå¤æ°ã¯ãä¸è¬ã«ã¯ãã®é¢æ°ã®ä¸ã ãã§æå¹ã§ããã宣è¨ããªãå ´åã«ã¯ãã°ãã¼ãã«å¤æ°ã¨åæ§ã®æå³ã«ãªãã¾ããå é¨é¢æ°ã®å ´åã¯ãããã¨ã¯ç°ãªãã¾ãã®ã§ãå¥ã®é ç®ã§èª¬æãã¾ãã
ã°ãã¼ãã«å¤æ°ã¯å®£è¨ãã¦ãããªãã¦ãåããªã®ï¼
ã¡ãã£ã¨éãã¾ããvarã§å®£è¨ããã°ãã¼ãã«å¤æ°ã¯åé¤ã§ãã¾ãããã宣è¨ããªãã°ãã¼ãã«å¤æ°ã¯ deleteã§åé¤ã§ãã¾ãã
ããæ£ç¢ºã«ã¯ã°ãã¼ãã«ãªãã¸ã§ã¯ããããã®å¤æ°åãæååã¨ãã¦ãã¤ããããã£ãåé¤ãã¾ãã
ã¾ããvar宣è¨ããå¤æ°ã¯åæå¤ããªããã°ããã®å¤ã¯undefinedã§åæåããã¾ãããvar宣è¨ããã¦ããªãå¤æ°ã¯ä»£å ¥ãããã¾ã§å¤æ°èªä½ãåå¨ãã¾ããããããã£ã¦ã ãã®å¤æ°ã®å¤ãåç §ãããã¨ããã¨ReferenceErrorä¾å¤ãçºçãã¾ãã
å¤æ°å®£è¨ã¯ã©ãã§ããã°ããã®ï¼
ã©ãã§å®£è¨ãã¦ãæ§ãã¾ãããå¤æ°ã宣è¨ããåã«ãã®å¤æ°ã使ããã¨ãã§ãã¾ãã
x = v;
var v = 123;
å¤æ°xã«vã®å¤ãä»£å ¥ãã¦ãã¾ãããv ã¯å¾ã§å®£è¨ããã¦ãã¾ã(ããã¦123ã§åæåãã¦ãã¾ãï¼ãxã«ã¯ä½ãä»£å ¥ãããã®ã§ããããï¼ãã®å ´åãundefinedãä»£å ¥ããã¾ããããã¯ã
var v;
x = v;
v = 123;
ã¨åãæå³ã«ãªãããã§ããä¸æ¹ãvãå¤æ°å®£è¨ããªãã§
x = v; v = 123
ã¨ããã¨ãx = vã®ã¨ããã§vãåç §ããéã«ãReferenceErrorä¾å¤ãçºçãã¾ãã
ä¸è¬ã«ã¯ãC++ã®ããã«ä½¿ãåã«ï¼åæåããã¨ãï¼å®£è¨ããã®ãããã¨æãã¾ãããå¤æ°ã®ã¹ã³ã¼ãã¯é¢æ°å ¨ä½ã«äºããããé¢æ°ã®å é ã§ã¾ã¨ãã¦å®£è¨ããã®ãããã¨ã®æè¦ãããã¾ãã
è¤æ°ã®å¤ãè¿ãé¢æ°ãã¤ããã«ã¯ã©ãããã®ï¼ãã¤ã³ã¿ããªãã®ã§å¼æ°ã®OUTãã©ã¡ã¼ã¿ã§è¿ãæ¹æ³ããªãã®ã§ããã
é åããªãã¸ã§ã¯ããè¿ãããã«ããã°å®¹æã«å®ç¾ã§ãã¾ãã
é åã®å ´åã«ã¯ãä¸è¨ã®ããã«Arrayãªãã©ã«ã使ãã°ãã¹ãã¼ãã«æ¸ãã¾ãã
function myfunc(x, y) { return [x + y, x - y]; }
å¤ã®æ°ãå¤ãå ´åãªã©ã¯ããªãã¸ã§ã¯ããè¿ãæ¹ãåããããã§ãããã
function myfunc () { return { x:100, y:100, width: 300, height: 400 }; }
ã©ã¡ãã®æ¹æ³ãããªãã¸ã§ã¯ããçæãããã®ã§ãå¹çãåé¡ã®ã¨ãã¯ãå¼æ°ã«ãªãã¸ã§ã¯ãã渡ãã¦ãå¼ã°ããå´ã§ããããã£ã«ã»ãããããªã©ã®æ¹æ³ãå¿ è¦ã§ãããã
switchæã¯C/C++ãJavaã®switchã¨åãã§ããï¼
ã¡ãã£ã¨éãã¾ããC/C++ãJavaã§ã¯caseã®ã¨ããã«ã¯ã³ã³ãã¤ã«æã«å¤ã決å®ã§ããæ°å¤å®æ°ããè¨è¿°ã§ãã¾ããããJavaScriptã§ã¯ä»»æã®å¼ãè¨è¿°ã§ãã¾ãããã®å¼ãå®è¡ããçµæã®å¤ã¨switchã§æå®ããå¤ãStrictEquals(===)ã®ã¨ãcaseæ¬ä½ãå®è¡ããã¾ãããã以å¤ã¯C/C++ãJavaã¨åãã§ãã
==ã¨===ã®éãã¯ä½ã§ããï¼
JavaScriptã§ã¯ï¼é æ¯è¼ãªãã¬ã¼ã¿ã®ä¸¡è ã®åãç°ãªãã¨ããå¼·å¶çã«åãåãããæ©è½ãããã¾ãã==ã¯åãåããã===ã§ã¯åãåããããã¨ã¯ãã¾ããã
true == 1 // true true === 1 // false 123 == new String("123") // true 123 === new String("123") // false null == undefined // true null === undefined // false
ãªãã¸ã§ã¯ãå士ã®ç価æ§å¤å®ã¯ã©ãããã°ããã®ã§ããï¼
ãªãã¸ã§ã¯ãå士ã¯==ã¨===ã¯å ¨ãåãçµæã«ãªãã¾ããããããtrueã«ãªãã®ã¯ãåä¸ã®ãªãã¸ã§ã¯ããã¾ãã¯joinããããªãã¸ã§ã¯ãã«éãã¾ããåä¸ã®ãªãã¸ã§ã¯ãã¨ã¯ãå®ä½ãåããªãã¸ã§ã¯ãã§ããã¢ãã¬ã¹ãçãããªãã¸ã§ã¯ãã¨è§£éãã¦ãããã§ããããjoinããããªãã¸ã§ã¯ãã¨ã¯ãå®ä½ãçãããã©ããã¯åãããä¸æ¹ã®ãªãã¸ã§ã¯ãã®ããããã£ãå¤æ´ããããããä¸æ¹ã®ãªãã¸ã§ã¯ãã®å¯¾å¿ããããããã£ãå¤æ´ããããããªé¢ä¿ããã¤ãªãã¸ã§ã¯ãã§ããã©ã¤ãã³ãã¼ã®ãããªã¤ã¡ã¼ã¸ã§ãããjoinedã¨ããã®ã¯å®è£ ã®æé©åãå¯è½ã«ãªãããã«==ã®æ¡ä»¶ãç·©ããããã«å°å ¥ããã¾ããã
å é¨é¢æ°ãé¢æ°å¼ã¯ãããå®è¡ããã度ã«ãæ°ããªé¢æ°ãªãã¸ã§ã¯ããçæããã¾ãããã¯ãã¼ã¸ã£ãçæãããªãå ´åã«ã¯ãé¢æ°ãªãã¸ã§ã¯ãã®æ°ããªçæã¯ç¡é§ã«ãªãã¾ããã¤ã¾ããåä¸ã®é¢æ°ãªãã¸ã§ã¯ããå ±æããæ¹ãå¹ççã§ãããã®å ´åãæ°ããªé¢æ°ãªãã¸ã§ã¯ããçæããã¨ããä»æ§ã¨çç¾ãã¦ãã¾ãã¾ãããjoin ãããé¢æ°ãªãã¸ã§ã¯ãã¨è§£éããã°ãçç¾ã¯è§£æ¶ããã¾ããã¡ãã£ã¨ãããã¤ãããããªæãã§ãããè¦æ ¼ã®ä¸ã§æé©åã®å®è£ ãèæ ®ããé¨åã§ãã
æååãBooleanã«å¤æãããããããããªåå¤æãèªåçã«è¡ãããããã ãã©ãã©ããã£ãè¦åãããã®ï¼
主ãªåå¤æã«ã¯ä»¥ä¸ã®ãã®ãããã¾ãããã以å¤ã«ããToInteger, ToInt32,ToUint32, ToUint16ãããã¾ããC/C++ã®ãã£ã¹ãã¨ã¯ç°ãªãã¾ãã®ã§ã注æãå¿ è¦ã§ãã
- ToBoolean
Undefined | false |
Null | false |
Boolean | ãã®ã¾ã¾ |
Number | 0,-0, NaNã®ã¨ãfalse, ãã以å¤ã¯ true |
String | 空æå("")ã®ã¨ãfalse, ãã以å¤ã¯ true |
Object | true |
- ToNumber
Undefined | NaN |
Null | +0 |
Boolean | trueã®ã¨ã1, falseã®ã¨ã+0 |
Number | ãã®ã¾ã¾ |
String | parseFloat()ã®çµæã®æ°å¤ |
Object | åºæ¬åã«å¤æããçµæã«å¯¾ãã¦ãToNumberãé©ç¨ãã |
- ToString
Undefined | "undefined" |
Null | "null" |
Boolean | trueã®ã¨ã"true", falseã®ã¨ã"false" |
Number | ï¼è¤éãªã®ã§çç¥ï¼ |
String | ãã®ã¾ã¾ |
Object | åºæ¬åã«å¤æããçµæã«å¯¾ãã¦ãToStringãé©ç¨ãã |
- ToObject
Undefined | TypeError |
Null | TypeError |
Boolean | new Boolean(value) |
Number | new Number(value) |
String | new String(value) |
Object | ãã®ã¾ã¾ |
&, |, >> ãªã©ã®ãããæ¼ç®ã¯å¼æ°ãToUint32ã«å¤æãã¦ãããããæä½ãè¡ãã®ã§ãä»»æã®æ°å¤ãå¼·å¶çã«32ãããæ´æ°ã«å¤æããã¨ããªã©ã«å©ç¨ã§ãã¾ãã
var x = 1234.5678 | 0 // 1234ãxã«ä»£å ¥ããã
åæ§ã«ãåé æ¼ç®å+ã¯ãªãã©ã³ããToNumberãã¾ãã®ã§ãæ°å¤ãæååãä¸æãªã¨ãæ°å¤ã«å¤æããææ³ã¨ãã¦ãã°ãã°ç¨ãããã¾ãã
var x = ....; var y = +x; // y ã¯å¼·å¶çã«Numberåã«ãªã
ããã¯ã
var y = Number(x);
ã¨ç価ã§ãã+x ã¯è¨è¿°éãå°ãªããã好ãã§ç¨ãã人ããã¾ãããNumber()é¢æ°ã使ã£ãã»ãããæ示çã§ããããããã§ãããã
gotoæã¯ãªãã®ï¼
ããã¾ããããã ããgotoã¯å°æ¥ã®äºç´èªã¨ãã¦å®ç¾©ããã¦ãã¾ãã®ã§ãå¤æ°åãªã©ã«ã¯ä½¿ãã¾ããã
FirefoxãIEãªã©ã®æ®ã©ã®ãã©ã¦ã¶ã§ã¯å°æ¥ã®ããã®äºç´èªã¨ãã¦å®ç¾©ããã¦ãã· ã³ãã«ã®ãªãã§ãå®éã«ã¯ä½¿ããã¦ããªããã®ã¯ãå¤æ°ã¨ãã¦ä½¿ãã¾ããã使ããªã æ¹ãããã§ãããã
å¤éã«ã¼ãã®å å´ããå¤ã«æããã«ã¯ã©ãããã®ï¼
try-catch throwã使ãæ¹æ³ãããã¾ãããä¸è¬ã«ã¯ã©ãã«ã使ãã¾ããforãwhileãªã©ã®åã«ã©ãã«ãã¤ããã¨ãbreakãcontinueã®éã«ãã®ã©ãã«ãæå®ãããã¨ãã§ãã¾ããJavaã¨åãã§ãã
loop: for (var i = 0; i < 100; i++) { for (var j= 0; j < 200; j++) { for (var k = 0; k < 300; k++) { if (k == 100) { break loop;ã// loopã¨ããã©ãã«ã®ã¤ããä¸çªå¤ã®foræãæããã } } } }
åç´ã«gotoã«ç¸å½ãããã¨ã¯ã§ããªãã®ï¼ã¨ã©ã¼å¦çãªã©ã§gotoã便å©ãªã¨ããããã®ã§ããã
ãã®å ´åãã©ãã«ä»ããããã¯æãå©ç¨ããã°å®ç¾ã§ãã¾ãã JavaScriptã«ã©ãã«ã¤ããããã¯æããããã¨ãç¥ããªãããã°ã©ããå¤ãããã§ãããç¥ã£ã¦ããã¨å½¹ã«ç«ã¤ã§ãããã
aaa; if (bbb) goto Exit; ccc; if (ddd) goto Exit; return OK; Exit: .... return FAIL;
ã¨ããã³ã¼ãã«ç¸å½ããã«ã¯ã以ä¸ã®ã³ã¼ãã§å®ç¾ã§ãã¾ãã
Exit: { aaa; if (bbb) break Exit; ccc; if (ddd) break Exit; return OK; } ... return FAIL;
break Exitã§ãããã¯æããæãããã¨ã§ãreturn FAILãå®è¡ããã¾ããä½äºããªããã°ãããã¯ã®æå¾ã® return OKãå®è¡ããã¾ããã©ãã«ã¤ããããã¯æã¯å ¥ãåã«ãããã¨ãã§ãã¾ãã
Exceptionã¨ãã¦æãããã¨ãã§ããå¤ã®æ¡ä»¶ã¯ããã¾ããï¼
ããããå ¨ã¦ã®å¯¾è±¡ãExceptionã¨ãã¦æãããã¨ãã§ãã¾ãã
throw new Object(); throw 123; throw "hello"; throw function () { return 321; }; throw undefined; throw NaN; throw null;
finallyã®ä½¿ãæ¹ãæãã¦ã
tryãããã¯ããæããã¨ãå¿ ãå®è¡ãããæãfinallã«è¨è¿°ãããã¨ãã§ãã¾ããopenãããå¿ ãcloseãããããªãã¿ã¼ã³ã®ã¨ããä¸è¨ã®ããã«æ¸ãã¾ãã
for (....) { try { open(); if (aaa) return; ... if (bbb) throw Error(); if (ccc) continue; if (ddd) break; } finally { close(); } }
tryä¸ã§returnããããä¾å¤ãthrowãããããªå ´åã§ããå¿ ãfinallyãããã¯ãå®è¡ããã¾ããä¸è¨ã®ããã«foræãªã©ã®ä¸ã«ããå ´åã«ã¯ãbreakãcontinueãå®è¡ããå¾ã«ãfinallyãå®è¡ããã¾ããtry-catch-finallyã¨catchãããã¯ãåæã«ä½¿ããã¨ãã§ãã¾ãã
finallyä¸ã§æ示çã«returnããã¨tryä¸ã§çºçããä¾å¤ãç¡å¹ã«ãªãã¾ãã
function func1() { var x = 0; try { throw Error(); } finally { x = 321; } return x; } function func2() { var x = 0; try { throw Error(); } finally { x = 321; return x; } }
func1()ãå®è¡ããã¨Errorä¾å¤ãæãããã¾ãããfunc2()ã¯321ãè¿å¤ã¨ãã¦è¿ããã¾ããåºæ¬çã«Javaã¨åãã§ãã
tryã«catchã¨finallyã両æ¹è¨è¿°ãããã©ããªãã®ï¼
tryä¸ã§ä¾å¤ãçºçããå ´åãcatché¨ãå®è¡ãããå¾ã«ãfinallyé¨ãå®è¡ããã¾ããcatché¨ã§ããã«å¥ã®ä¾å¤ãçºçãã¦ããfinallyã¯å®è¡ããã¾ããè¦ããã«ãfinallyãããã¨ãã©ããªãã¨ãèµ·ãã£ã¦ãfinllayãå®è¡ããããã¨ãä¿éããã¾ãã
ä¸è¨ã®ããã°ã©ã ãå®è¡ããã¨
function test() { try { throw "hello"; } catch (ex) { print("ex=" + ex); throw "world"; } finally { print("finally"); } } try { test(); } catch (ex) { print(">>> ex=" + ex); }
ã¨ä»¥ä¸ã®ããã«è¡¨ç¤ºããã¾ãã
ex=hello
finally
>>> ex=world
C++ãJavaã§ã¯è¤æ°ã®catchã§å ´ååãã§ãããã©ãJavaScriptã¯ã§ããªãã®ï¼
JavaScriptã¯å¤æ°ã«åããªãã®ã§ãè¤æ°ã®catchãè¨è¿°ãããã¨ã¯ã§ãã¾ãããå ´åããããã¨ããããcatché¨ã®ä¸ã§instanceofãªãã¬ã¼ã¿ãªã©ãå©ç¨ããã¨ããã§ãããã
try { ... } catch (ex) { if (ex instanceof TypeError) { ... } else if (ex instanceof Number) { ... } else if (ex instanceof Object) { ... } .... }
ã¡ãªã¿ã«ãFirefoxã®JavaScriptã§ã¯ä¸è¨ã®ããã«catchãè¤æ°è¨è¿°ãããã¨ãã§ãã¾ãã
try { ... } catch (ex if ex instanceof TypeError) { ... } catch (ex if ex instanceof Number) { ... } catch (ex if ex instanceof Object) { ... } catch (ex) { ... }
åå空éã¯ãªãã®ï¼
ããã¾ãããããããåå空éã«ç¸å½ãããã®ã¯ç°¡åã«è¡¨ç¾ã§ãã¾ãã
var myns = { Foo: function () {...} }; myns.Foo.prototype = { func: function () {...}, func2: function () { ...} }; var v = new myns.Foo(); v.func(); v.func2();
Ajaxãªã©ã®ã©ã¤ãã©ãªã¨ãã¦æµéãã¦ããã½ããã§ã¯ããã®ããã¯æ¹æ³ã§ååè¡çªãé¿ãã¦ãã¾ãã
ã¦ã¼ã¶å®ç¾©ã®ãªãã¸ã§ã¯ãã¯ã©ããã£ã¦çæããã®ï¼
JavaScriptã«ã¯ã¯ã©ã¹ã¨ãããã®ã¯ããã¾ããããé¢æ°ãnewããã¨ãªãã¸ã§ã¯ããçæããã¾ãã
function myfunc() { } v = new myfunc();
vã¯ãªãã¸ã§ã¯ãã§ããããããã£ãè¨å®ãå¤ãã¤ãããã¨ãã§ãã¾ãã
v.abc = 123;
é¢æ°ãé¢æ°ã¨ãã¦ã³ã¼ã«ãã以å¤ã« newãã§ããã£ã¦ã¡ãã£ã¨ãããé£ãã§ãã
確ãã«æåã¯æ··ä¹±ãã¾ããJavaScriptã§ã¯é¢æ°ããªãã¸ã§ã¯ãã§ãããªãã¸ã§ã¯ãã«ã¯ããããã£ã¨å¤ãããã¾ãããé¢æ°ã®å®è¡ã³ã¼ãã¯ãå®ã¯ããããããã£ã®å¤ã«ãªã£ã¦ãã¾ããnewãããã¨ãã«å®è¡ããããã®ã¯ã[[Consturctor]]ã¨ããå é¨ã®(ä»®æ³çãª)ããããã£ã®å¤ã«è¨å®ããã¦ããã³ã¼ãã§ããé常ã®é¢æ°ã³ã¼ã«ã®éã«ã¯ã[[Call]]ã¨ããããããã£ã«è¨å®ããã¦ããã³ã¼ããå®è¡ããã¾ãããã®ä¸¡è ã®ã³ã¼ãã¯ã¦ã¼ã¶ãå®ç¾©ããé¢æ°æ¬ä½ã®ã³ã¼ãã«ä¸è´ãã¾ãããã³ã³ããã¹ããç°ãªãã¾ãã
function myfunc(x, y) { this.v = x; this.w = y; return x + y; } a = myfunc(3, 4);
ãå®è¡ããã¨ãaã«ã¯7ãä»£å ¥ããã¾ããthis.v, this.w ã«ä»£å ¥ãããã®ã¯ã©ããªãã®ã§ãããï¼ãã®å ´åã¯ãthisã«ã¯Globalãªãã¸ã§ã¯ããä»£å ¥ããã¦ãããããvã¨wã¨ããã°ãã¼ãã«å¤æ°ã«x,yã®å¤ãä»£å ¥ããã¾ãã
print(v); // 3 print(w); // 4
ä¸æ¹ã
b = new myfunc(3, 4);
ãå®è¡ããã¨ãã¾ãããªãã¸ã§ã¯ããçæãã¦ããã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦é¢æ°æ¬ä½ãå®è¡ããã¾ãããã®ã¨ããthisã«ã¯ãã®ãªãã¸ã§ã¯ããä»£å ¥ããã¦ãã¾ãã
print(b.v); // 3 print(b.w); // 4
ãã®å ´åãmyfunc()ã®returnå¤ã¯ç¡è¦ããã¾ãã
nullã¯ãªãã¸ã§ã¯ãã§ããï¼
ããããããããã£ãæããªãã¨ããç¹ã§ãªãã¸ã§ã¯ãã§ã¯ããã¾ãããã¾ãã
var v = null instnaceof Object;
ã®çµæãfalseã§ããããããtypeofãªãã¬ã¼ã¿ã§ã¯objectãè¿ããã¾ããä»æ§ããããªã£ã¦ããã®ã§ãããããã¯ä»æ§ã®ãã°ã¨ãã£ã¦ãããããç¥ãã¾ããã
æ°å¤ãæååã«ãéã«æåãæ°å¤ã«å¤æããã«ã¯ã©ãããã°ããã®ï¼
æ°å¤ãæååã«å¤æããã«ã¯ãããã¤ãã®æ¹æ³ãããã¾ãã
var x = 123; var s1 = String(x); var s2 = x.toString(); var s3 = "" + x; var s4 = x + "";
s1ã®æ¹æ³ãããã§ããããs3,s4ã¯ããè¦ããã¾ãããè¦ãç®ãä¸èªç¶ã§ãããå¹çãå¤å°è½ãå ´åãããã¾ãã
æååãæ°å¤ã«å¤æããã«ã¯ã
var x = "321"; var n1 = Number(x); var n2 = parseInt(x); var n3 = parseFloat(x); var n4 = x - 0; var n5 = +x;
ãªã©ã®æ¹æ³ãããã¾ãããããããæå³ãç°ãªãã¾ãï¼n1ã¨n4,n5ã¯ã»ã¼åãï¼ã
é¢é£ã¨ã³ããªã¼ãJavaScriptで ""+x を文字列変換に使うのは気持ち悪いããã覧ãã ããã
Arrayãnewããªãã§é¢æ°ã®ããã«ä½¿ã£ã¦ããããã°ã©ã ãè¦ã¾ããããåé¡ãªãã®ã§ããï¼
var x = new Array(1,2,3);
ã¨
var x = Array(1,2,3);
ã§ãããArrayã¯ã³ã³ã¹ãã©ã¯ã¿ã¨é¢æ°ã³ã¼ã«ãåãã§ãããä¸ã®æã¯å ¨ãåçã§ããã³ã³ã¹ãã©ã¯ã¿ã¨é¢æ°ãåããã®ã¯ãArray以å¤ã«ãFunction, RegExp, Error(ã¨ãã®ãµãã®TypeError, EvalError, ReferenceError, URIError)ãããã¾ãã
ãã£ã¨ããArrayã®å ´åã¯ãArrayãªãã©ã«ãç¨ããæ¹ãããããããã§ããArrayãªãã©ã«ã¯ä¸è¨ã®ããã«[]ä¸ã«ã³ã³ãã§è¦ç´ ãåæãã¾ãã
var v = [1,2,3];
ç¹ã«ãè¦ç´ ãï¼ã¤ã§ããããããæ°å¤ã®å ´åã«ã¯ãArray()é¢æ°ãã³ã³ã¹ãã©ã¯ã¿ã§ã¯çæããæ¹æ³ãããã¾ããã
var v = new Array(5);
ã¯v[0]ã5ã®é
åã§ã¯ãªããé·ãã5ã®é
åãçæãããã¨ãæå³ãã¾ããv[0]ã5ã®é·ã1ã®é
åãArrayãªãã©ã«ãªãã°
var v = [5];
ã¨è¨è¿°ãããã¨ãã§ãã¾ãã
ã¾ããArrayãªãã©ã«ãç¨ããã¨ãéä¸ã®è¦ç´ ãçç¥ããã(ãã®å ´åã¯undefinedã§åæå)æå¾ã®ã³ã³ããç¡è¦ãããªã©ã®æ©è½ãããã¾ãã
v = ["a","b",,,"f","g",]
ã¯
v = ["a","b",undefined,undefined,"f","g"]
ã¨ç価ã§ãã
Arrayã®ããã«ã¦ã¼ã¶å®ç¾©é¢æ°ã§new ãã¦ãé¢æ°ã³ã¼ã«ãã¦ãåãåä½ããããã®ã¯ä½ãã¾ããï¼
ã¦ã¼ã¶å®ç¾©é¢æ°ã«å¯¾ã㦠new ããã¨ããã®é¢æ°ã®æ»ãå¤ããªãã¸ã§ã¯ãã®å ´åãnewã®çµæã¯ãã®ãªãã¸ã§ã¯ãã«ãªãã¾ãã
function MyTest () { return { name: "hello", value: 123 }; } var v = new MyTest(); var w = MyTest();
vã¨wã¯ã©ã¡ããMyTest()ã®æ»ãå¤ã§ãããªãã¸ã§ã¯ããä»£å ¥ããã¾ããç¹ã«ãã³ã¼ãä¸ã¯ v 㯠MyTestãnewããã¤ã³ã¹ã¿ã³ã¹ã®ããã«è¦ãã¾ãããv instanceof MyTest㯠falseã§ãããã¨ã«æ³¨æãã¦ãã ããã
é åã«ããã¢ã¯ã»ã¹([])ã¯Arrayã§ãªãã¦ãã§ãã¾ãããArrayã¯ä½ãéãã®ï¼
確ãã«ãé常ã®ãªãã¸ã§ã¯ããçæãã¦é åã®ããã«ä½¿ããã¨ãã§ãã¾ãã
var v1 = new Object(); v1[0] = "hello"; v1[1] = "world";
Arrayã使ã£ã¦ãè¦ãç®ã¯åãã§ãã
var v2 = new Array(); v2[0] = "hello"; v2[1] = "world";
ããããArrayã«ã¯è¦ç´ æ°ã示ãlengthããããã£ãããã¾ããlengthã«ã¯å¤ãã»ãããããã¨ãã§ããã»ããããã¨Arrayã®è¦ç´ æ°ãã»ããããå¤ã«å¤æ´ããã¾ãããããã£ã¦ãv2.length = 1; ãå®è¡ããã¨è¦ç´ æ°ãã¯ã¿åºããv2[1]ã¯undefinedã«ãªãã¾ããä¸æ¹ã v1.length = 1;ãå®è¡ãã¦ãv1[1]ã¯"world"ã®ã¾ã¾ã§ãã
é åã§ãªãæ®éã®ãªãã¸ã§ã¯ããã©ããã¦[]ã§é åã¢ã¯ã»ã¹ã§ããã®ã§ããï¼
ãªãã¸ã§ã¯ãã¯ããããã£ã¨å¤ã®éåã§ãããé åã®ã¤ã³ããã¯ã¹ãããããã£ã ããã§ããä¸è¬ã«ãã¤ã³ããã¯ã¹ãæ°å¤ã§ãªãé åã¯ç¹ã«é£æ³é åã¨å¼ã°ãã¦ãã¾ããããªãã¸ã§ã¯ãã¯ã¾ãã«é£æ³é åãªã®ã§ãã
v.myprop = "hello";
ã¨
v["myprop"] = "hello";
ã¯å ¨ãåãã§ããæ°å¤ã¯èå¥åã§ã¯ãªãã®ã§ããããã£åã«ã¯ãªããªãããã§ãé åã¢ã¯ã»ã¹è¡¨ç¾ã®ã¿ã§è¨è¿°ãããã¨ãã§ãã¾ãã
prototypeããããã£ã®æå³ããããããã¾ããã
ä¸è¬ã®ãªãã¸ã§ã¯ãæåè¨èªã§ã¯ãã¯ã©ã¹ã®é層ã¯super-subã®é¢ä¿ãããã¾ãããJavaScrptã§ã¯ããã«å¯¾å¿ãããã®ã¨ãã¦prototypeãã§ã¼ã³ãããã¾ãããããªãã¸ã§ã¯ãã®ããããã£ãæ¢ç´¢ããéã«ã¯ãæåã«ãã®ãªãã¸ã§ã¯ãèªèº«ã«ç®çã®ããããã£ããããã©ããã調ã¹ãããã°ãã®å¤ãè¿ãã¾ãããããç¡ããã°ããã®ãªãã¸ã§ã¯ããçæãããªãã¸ã§ã¯ãï¼ããããã°ï¼ã®prototypeããããã£ã®å¤ã®ãªãã¸ã§ã¯ãã«ç®çã®ããããã£ããããã©ããã調ã¹ã¾ããããã°ãããè¿ãããªããã°ããã«ãã®ãªãã¸ã§ã¯ããçæãããªãã¸ã§ã¯ãã®prototypeããããã£ãåæ§ã«å帰çã«ç¹°ãè¿ãæ¢ç´¢ãã¾ãã
function Foo() { this.p1 = 123; } function Foo2() { this.p2 = "hello"; } Foo2.prototype = new Foo(); function Foo3() { this.p3 = 987; } Foo3.prototype = new Foo2(); var v = new Foo3(); var v1 = v.p1; var v2 = v.p2; var v3 = v.p3;
v1,v2,v3ã¯ããããã123, "hello", 987ã«ãªãã¾ããprototypeãã§ã¼ã³ã辿ã£ã¦ç®çã®ããããã£ãæ¢ç´¢ãã¦ãã¾ãã
ããã§ã
v.p2 = "world";
ãå®è¡ããã¨ãå½ç¶ãv.p2ã®å¤ã¯"hello"ã§ã¯ãªãã"world"ã«ãªãã¾ãã ãã®å ´åãp2ããããã£ã¯ãªãã¸ã§ã¯ãvã«ç´æ¥ä»å ããã¦ãã¾ãããã®å¾ã
delete v.p2;
ãå®è¡ããã¨ãv.p2ã®å¤ã¯ãåã³"hello"ã«ãªãã¾ãã
ã¾ããfor-inæ§æã¯vã«ç´æ¥ä»å ããã¦ããªãããããã£ã対象ã«ãªããå¤ã¯prototypeãã§ã¼ã³ã辿ã£ã¦æ¤ç´¢ããã¾ãã
for (var i in v) { print("name=" + i + ", value=" + v[i]); }
ã®çµæã¯ã
name=p3, value=987 name=p2, value=hello name=p1, value=123
ã«ãªãã¾ãã
æ°å¤ãtrue,falseãªã©ã®åºæ¬åã¯ãªãã¸ã§ã¯ãã§ã¯ãªãã®ï¼ãªãã¸ã§ã¯ãæåè¨èªãªããããã®åºæ¬åããªãã¸ã§ã¯ãã§ããã¹ãã¨æãã®ã§ããã
ããããã£ã¨å¤ã®éåããªãã¸ã§ã¯ãã¨å®ç¾©ãããªããåºæ¬åã¯ãªãã¸ã§ã¯ãã§ã¯ããã¾ãããããããåºæ¬åããããªãã¸ã§ã¯ãããçæãããã¤ã³ã¹ã¿ã³ã¹ã¨è§£éãããã¨ãã§ãã¾ããï¼ãã®å ´åã¤ã³ã¹ã¿ã³ã¹ã¨ããç¨èªã¯é©åã§ã¯ããã¾ãããï¼ããããã£ã¦ã次ã®ãããªã³ã¼ãã¯åé¡ãªãåä½ãã¾ãã
var v1 = (123).toString(); var v2 = true.toString();
123ã«æ¬å¼§ãã¤ããã®ã¯ã123.toString()ã¨ãã¦ãã¾ãã¨ãææ³çã«ã¯123ã®æ¬¡ã®ããããå°æ°ç¹ã¨ãã¦è§£éãã¦ãã¾ããSyntaxErrorã«ãªãããã§ãã
ããã«ãæ°ããªã¡ã½ããã追å ãããã¨ãã§ãã¾ããä¾ã¨ãã¦ãæ£æ°ãè² æ°ã«ãtrueãfalse ãªã©ã«å転ããã¡ã½ããneg()ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
Number.prototype.neg = function () { return -this; } Boolean.prototype.neg = function () { return !this.valueOf(); } var v1 = (123).neg(); var v2 = ture.neg();
v1, v2ã¯ãããã -123, falseã«ãªãã¾ãã
æååã«å¯¾ãã¦ãåæ§ãªãã¨ãã§ãã¾ãã
123ã¯ãªãã¸ã§ã¯ãã§ãªããã¾ãã123 instanceof Numberãfalseãªã®ã«ãã©ãã㦠(123).toString()ãªã©ãå®è¡ã§ããã®ãããããã¾ããã
確ãã«æ··ä¹±ããããã§ãããv.fun() ã¨ããå½¢å¼ã¯ãvã«å¯¾ãã¦ToObjectã¨ããåå¤æãå®è¡ããå¾ã«ãfunããããã£ãæ¢ç´¢ãã¦ããã®å¤ãé¢æ°ã¨ãã¦å®è¡ããã¨ããæå³ãæã¡ã¾ãããã®ã¨ããthisã«ã¯ToObjectããå¤ããã¤ã³ãããã¾ãããããã£ã¦ãæ°å¤ãæååãªã©ã®åºæ¬åã«å¯¾ãã¦ããªãã¸ã§ã¯ãã«å¯¾ããã¡ã½ããã³ã¼ã«ã¨åçã®è¨è¿°ãã§ããããã§ãã
ã¤ã¾ãã
(123).toString();
ã¯
(new Object(123)).toString();
ã¨åçã§ããããã«new Object(123)㯠new Number(123)ã¨åçã§ãã
åã®FAQé ç®ã§ -this ã¨ãã¦ãã¾ãããthis㯠new Object(123)ãããªãã¸ã§ã¯ãã§ãã'-'ãªãã¬ã¼ã¿ã¯ å¼æ°ã ToNumberããçµæã«é©ç¨ããã®ã§ãã¾ãæå³ããçµæãå¾ããã¾ãã'!'ãªãã¬ã¼ã¿ãåæ§ã«ToBooleanããçµæã«å¯¾ãã¦é©ç¨ããã¾ãããã ããnew Boolean(false)ãToBooleanããå¤ã¯trueãªã®ã§ãvalueOf()ã¡ã½ããã§åºæ¬åã«å¤æãã¦ãã¾ãã
æååã«å¯¾ãã¦Stringã¯ã©ã¹ã®ã¡ã½ããcharAt(), indexOf()ãªã©ãå©ç¨ã§ããã®ãåãçç±ã«ãããã®ã§ãã
var x = "abcdefg".charAt(3) // xã«ã¯"d"ãä»£å ¥ããã
åºæ¬åã«ã¡ã½ãããé©ç¨ããæ§æãæ¸ãã¨ããªãã¸ã§ã¯ããçæããããã¨ã«ãªããããå¹çãæªãã®ï¼
å¿ ãããããã¨ã¯éãã¾ããããªãã¸ã§ã¯ããçæãããã¨ããã®ã¯ãè¨èªã®æå³çãªãã¨ã§ããã®ãªãã¸ã§ã¯ãã¯å¤é¨ããåç §ãããã¨ãã§ãã¾ãããããå®éã«ã¯ãªãã¸ã§ã¯ããçæããªãã¦ããããããªå®è£ ãå¯è½ã§ããå¦çç³»ä¾åã§ãã
å®éã«ç¢ºããã¦ã¿ãã®ã§ããã¡ããã覧ãã ããã
newã¨deleteã¨ãããªãã¬ã¼ã¿ãããã¾ãããC++ã¨ã©ãéãã®ï¼
newã¯Constructorãªãã¬ã¼ã¿ã§C++ã¨ã»ã¼åãæå³ã§ãããdeleteã¯Destructorãªãã¬ã¼ã¿ã§ã¯ãªããC++ã®deleteã¨ã¯å ¨ãæå³ãéãã¾ããJavaScriptã§ã¯deleteã¯ãªãã¸ã§ã¯ãããå±æ§ãåé¤ããã¨ããæå³ã®ãªãã¬ã¼ã¿ã§ãã
var v = new Object(); v.foo = "hello"; print(v.foo); //-- (1) delete v.foo; print(v.foo); //-- (2) delete v;
(1)ã§ã¯helloã表示ããã¾ããã(2)ã§ã¯ undefinedã表示ããã¾ããããã¯ããªãã¸ã§ã¯ãvã®å±æ§fooãdeleteã«ããåé¤ãããããã§ããæå¾ã® delete v㯠delete this.v ã¨åãæå³ã«ãªãã¾ããããã¯ç¾å¨ã®thisãªãã¸ã§ã¯ãã®å±æ§vãåé¤ãããã¨ã«ãªãã¾ããnew ã§çæãããªãã¸ã§ã¯ãèªä½ãåé¤ãããã¨ã§ã¯ããã¾ãããï¼ãã®å ´åãvãvar宣è¨ããã¦ãã¾ãã®ã§ãå®éã«ã¯å±æ§vã¯åé¤ãããã¨ã¯ã§ãã¾ããï¼
C++ãJavaã§ã¯åçãªnewã¯ã§ãã¾ããããJavaScriptã§ã¯ã©ãã§ããï¼
C++ãJavaã§ã¯ new Foo()ã®ããã«Fooã®é¨åã¯å¿ ãã¯ã©ã¹åãååã§ãªããã°ããã¾ããããJavaScriptã§ã¯ Fooã®é¨åã¯å®è¡æã«è©ä¾¡ããã¾ãã®ã§ãåçãªnewãã§ãã¾ãã
function mynew(obj, arg) { return new ((typeof obj == "function") ? obj : Object)(arg); } v = mynew(Number, 123);
new ããããªãã¸ã§ã¯ãã¯èª°ãéæ¾ããã®ï¼
ã¦ã¼ã¶ããã°ã©ã ãæ示çã«ãªãã¸ã§ã¯ããéæ¾ãããã¨ã¯ã§ãã¾ãããå¿ è¦ãªããªãã¸ã§ã¯ãã¯ã¬ã¼ãã¸ã³ã¬ã¯ã·ã§ã³æ©è½ã«ããå¦çç³»ãèªåçã«éæ¾ãã¾ãã
voidã¨ããã®ããããã©ãC/C++ã®voidã¨ã©ãéãã®ï¼
JavaScriptã®voidã¯C/C++ãJavaã®voidã¨ã¯å ¨ãå¥ã®æå³ã«ãªãã¾ããJavaScriptã®voidã¯çµæãundefinedã«ããã¨ãããªãã¬ã¼ã¿ã§ãã
var x = void (3 + 5);
㯠3+5ã®çµæãç¡å¹ã«ãã¦undefinedãxã«ä»£å ¥ããã¾ããundefinedã¯ãã¼ã¯ã¼ãã§ãªããåãªãã°ãã¼ãã«å¤æ°ã§ãã®ã§ãå¤ãå¤æ´ãããã¨ãã§ãã¦ãã¾ãã¾ããvoidã使ãã¨ãå¿ ãundefinedãå¾ãããã¨ããç¹ã§å¿ è¦ãªã¨ããããã¾ãããå®éã«ã¯ã»ã¨ãã©ä½¿ããããã¨ãããã¾ããã
undefined = 123; var x = undefined; var y = void 0;
xã«ã¯123ãä»£å ¥ããã¾ãããyã«ã¯undefinedãä»£å ¥ããã¾ãã
ã°ãã¼ãã«å¤æ°ãé¢æ°ããªãã¸ã§ã¯ãã®ããããã£ã®å¤ã¨ãããã¨ããã¾ãã¡ãã³ã¨ãã¾ãããã
ã°ãã¼ãã«å¤æ°ãé¢æ°ã¯ãå®ã¯ããã°ã©ã ããåç §ããããã®ç¹å®ã®ååãä»ãããã¦ããªãGlobalãªãã¸ã§ã¯ãã¨ãããªãã¸ã§ã¯ãã®ããããã£ã§ããã°ãã¼ãã«ã¹ã³ã¼ãã§ã¯ãGlobalãªãã¸ã§ã¯ãã¸ã¯thisã§åç §ãããã¨ãã§ãã¾ãã
var x = 123; var y = this.x;
yã«ã¯xã®å¤ã§ãã123ãä»£å ¥ããã¾ãã
ã°ãã¼ãã«ã³ã¼ãã§å®ç¾©ããé¢æ°å®ç¾©
function myfunc () { ... } //-- (1)
ã¯ã
var myfunc = function myfunc () {...} //-- (2)
ã¨ã»ã¼åãæå³ã«ãªãã¾ããå¤æ°ãGlobalãªãã¸ã§ã¯ãã®ããããã£ã§ããã®ã¨åæ§ã«ã°ãã¼ãã«é¢æ°ãGlobalãªãã¸ã§ã¯ãã®ããããã£ã§ãã
é¢æ°ã«ãªãã·ã§ã³å¼æ°ã表ç¾ããã«ã¯ã©ãããã°ããã§ããï¼
ã¦ã¼ã¶å®ç¾©é¢æ°ã§ã¯ãå®ç¾©ãããå¼æ°(ä»®å¼æ°)ã®æ°ããå®éã®å¼æ°(å®å¼æ°)ãå°ãªãå ´åã«ã¯ãæ®ãã®å¼æ°ã«undefinedãä»£å ¥ããã¾ããã¾ããargumentsãªãã¸ã§ã¯ãã®lengthããããã£ã«ã¯ãå®éã®å¼æ°ã®æ°ãã»ããããã¾ãããããã®æ©æ§ãç¨ããã°ãªãã·ã§ã³å¼æ°ãå®ç¾ã§ãã¾ãã
ä¸æ¹ããªãã·ã§ã³å¼æ°ã®æ°ãå¤ãå ´åã«ã¯ããªãã¸ã§ã¯ããªãã©ã«ãå©ç¨ãã¦ãã¼ã¯ã¼ããã©ã¡ã¼ã¿é¢¨ã®è¡¨ç¾ãããç¨ãããã¾ãã
gfx.setProperty({ x: 10, y: 20, width: 80, height: 40 });
è¦ã¦ãããããããé çªã«ä¾åãããã¾ããæ°ããªãªãã·ã§ã³ã®è¿½å ãªã©ãç°¡åã«ã§ãã¾ãã
å é¨é¢æ°ã£ã¦ä½ã§ããï¼
é¢æ°ã®ä¸ã§å®ç¾©ãããé¢æ°ã§ããé¢æ°å®ç¾©ã¯å ¥ãåã«ãããã¨ãã§ãã¾ããå é¨é¢æ°ã¯è¦ªã®é¢æ°ã®ãããã¬ãã«ã«è¨è¿°ãããã¨ãã§ãã¾ããifãwhileæãªã©ã®ä¸ã«ã¯å é¨é¢æ°ãå®ç¾©ãããã¨ã¯ã§ãã¾ãããJScriptãFirefoxã®JavaScriptãªã©ã¯æã¨ãã¦è¨è¿°å¯è½ãªã¨ããã«ã¯ã©ãã§ãé¢æ°å®ç¾©ãå¯è½ã§ãããåä½ãå¾®å¦ã«ç°ãªãã¾ãã
å é¨é¢æ°ã®ä¸ããã¯ã親ã®é¢æ°ã®ãã¼ã«ã«å¤æ°ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ããã¾ããå é¨é¢æ°ã®ååã¯ãã¼ã«ã«ãªååã«ãªãã¾ãã®ã§ãå¤é¨ããã¯ãã®ååã§åç §ãããã¨ã¯ã§ãã¾ãããããã«ãå é¨é¢æ°ã¯åçã«çæããããã®ã§ãã
function func1(x) { var y = 2 * x; function func2(z) { return x + y * z; } return func2(x + y); } v = func1(5); // vã«ã¯155ãä»£å ¥ããã
ä¸æ¹ãé¢æ°ããªãã¸ã§ã¯ãã§ããããé¢æ°ã®æ»ãå¤ã«é¢æ°ãè¿ããã¨ãã§ãã¾ãã
function func1(x) { var y = 2 * x; function func2(z) { return x + y * z; } return func2; } v = func1(5); // vã«ã¯é¢æ°func2ãä»£å ¥ãããã
vã¯é¢æ°ãªã®ã§ãé¢æ°ã³ã¼ã«ãå¯è½ã§ãã
w = v(8);
wã«ã¯ä½ã代å
¥ãããã§ããããï¼vã¯é¢æ°ãªãã¸ã§ã¯ãã§ããããã®é¢æ°ãªãã¸ã§ã¯ããçæãããã¨ãã®ãã¼ã«ã«å¤æ°xã¨yã®å¤ãé¢æ°ãªãã¸ã§ã¯ãã¨ä¸ç·ã«ä¿æããã¦ãã¾ãããã®ãããªç¶æ
ãé¢æ°ã¯ãã¼ã¸ã£ããããã¯åã«ã¯ãã¼ã¸ã£ã¨å¼ã³ã¾ãããããã£ã¦ãv(8)ã®å¤ã¯ãã®æã®ç°å¢ãx=5,y=10,z=8ãªã®ã§ã85ã«ãªãã¾ãã
ãã¼ã«ã«å¤æ°ãé¢æ°ãã©ã¡ã¼ã¿ãä½ãã®ãªãã¸ã§ã¯ãã®ããããã£ã ã¨ããæå³ããããã¾ããã
é¢æ°ã«å¶å¾¡ã移ã£ãã¨ããããã°ã©ã ããã¯åç §ãããã¨ãã§ããªãActivationObjectã¨å¼ã°ããä»®æ³çãªãªãã¸ã§ã¯ããçæããã¾ããæå³çã«ã¯é¢æ°ã®ãã¼ã«ã«å¤æ°ã¯ããã®ActivationObjectã®ããããã£ãªã®ã§ããActivationObjectã¯é¢æ°ã«å¯¾ãã¦ï¼ã¤ã§ãã®ã§ããã¼ã«ã«å¤æ°ã®ã¹ã³ã¼ããé¢æ°ã®ä¸å ¨ä½ã®ã¹ã³ã¼ãã«ãªãã¾ããã¾ããåãååã®ãã¼ã«ã«å¤æ°ã¯ï¼ã¤ããå®ç¾©ã§ããªããã¨ã«ãªãã¾ããããã¯ãC/C++ãJavaãªã©ã¨å¤§ããç°ãªãç¹ã§ãã®ã§æ³¨æãå¿ è¦ã§ãã
function myfunc() { var i = 3; for (var j = 0; j < 10; j++) { var i = j; } print(i); // iã®å¤ã¯9 }
ä¸æ¹ãé¢æ°ãã©ã¡ã¼ã¿ã¯ãargumentsã¨ãããªãã¸ã§ã¯ãã®ããããã£ã§ãããã¾ãã
function plus(x, y) { return x + y; }
ããã¯ã次ã®é¢æ°ã¨åçã§ãã
function plus() { return arguments[0] + arguments[1]; }
é¢æ°ãã©ã¡ã¼ã¿ã¨ãã¼ã«ã«å¤æ°ã®ååãåãã ã£ããã©ããªãã®ï¼
ãã¼ã«ã«å¤æ°ãåæåããã¦ããªãã°ãé¢æ°ãã©ã¡ã¼ã¿ã®å¤ãåªå ããã¾ããåæå¤ã®ãªãvar宣è¨ã¯ããã®æç¹ã§ãã®å¤æ°ãå®ç¾©ããã¦ããªããã°å®ç¾©ããæ¢ã«å®ç¾©ããã¦ããã°ä½ãããªãï¼undefinedã§åæåã¯ããªãï¼ã¨ããåä½ã«ãªãã¾ãã
function myfunc(x, y) { var x; var y = 987; print("x=" + x); print("y=" + y); } myfunc(135, 246); x=135 y=987
ãã¹ããªãã¸ã§ã¯ãã£ã¦ä½ã§ããï¼
ãã¹ãç°å¢ã«ãã£ã¦æä¾ããããªãã¸ã§ã¯ãã®ãã¨ã§ãããã¹ããªãã¸ã§ã¯ãã®è©³ç´°ãªåä½ã¯ECMA262ã§ã¯å¤ããå®è£ ä¾åã¨ãªã£ã¦ãã¾ãã
ã¯ãã¼ã¸ã£ã£ã¦ä½ã§ããï¼
å®ã¯ãã¯ãã¼ã¸ã£ã¨ããç¨èªã¯ECMAScript Edition3ã®è¦æ ¼æ¸ã«ã¯ç»å ´ãã¾ãããé¢æ°ããã¡ã¼ã¹ãã¯ã©ã¹ãªãªãã¸ã§ã¯ãã§ãããé¢æ°ã®ä¸ã§é¢æ°ãå®ç¾©ã§ãããããªè¨èªã§ã¯ã¯ãã¼ã¸ã£ãç»å ´ãã¾ãã
ä¸è¬ã«ãé¢æ°ã®ä¸ã§å®ç¾©ãããã¼ã«ã«å¤æ°ã¯ãã®é¢æ°ãçµäºããæç¹ã§æ¶æ» ãã¾ãããå å´ã§å®ç¾©ããé¢æ°ã®ä¸ã§ãã®å¤æ°ãåç §ãã¦ããç¶æ ã§ããã®é¢æ°ã親ã®é¢æ°ã®æ»ãå¤ã¨ãã¦è¿ããå ´åã親ã®é¢æ°ãçµäºããå¾ã§ããã®ãã¼ã«ã«å¤æ°ãæå¹ãªç¶æ ãä¿æãã¦ãã¾ããé¢æ°ã¨å¤æ°ãï¼ã¤ã®ãªãã¸ã§ã¯ãã¨ãã¦ã«ãã»ã«åãããç¶æ ã§ããããããé¢æ°ã¯ãã¼ã¸ã£ã¨å¼ã°ãã¾ã
å ã®FAQé ç®ã§ãé¢æ°ã®å¶å¾¡ã移ã£ãã¨ãä»®æ³çãªãªãã¸ã§ã¯ãã§ããActivationObjectãçæãããã¨æ¸ãã¾ããããé¢æ°ãçµäºãã¦ãããã®ActivationObjectãåå¨ãç¶ãããããªç¶æ³ãçºçããå ´åã«ã¯ããã®é¢æ°ã®ä¸ããåç §ã§ãããã¼ã«ã«å¤æ°çãé¢æ°ãçµäºããå¾ãåå¨ãç¶ãã¾ãã
function Counter(n) { var cnt = 0; function StepUp() { return cnt += n; } return StepUp; } var fun = Counter(5); // -- (a) print(fun()); print(fun());
Counteré¢æ°ã®ä¸ã§StepUpé¢æ°ãå®ç¾©ããCounteré¢æ°ã®æ»ãå¤ã¨ãã¦StepUpé¢æ°ãè¿ãã¦ãã¾ãã(a)ã§funã«ä»£å ¥ãããã®ã¯ãã®æ»ãå¤ã®é¢æ°ãªãã¸ã§ã¯ãã§ããCounteré¢æ°ã¯çµäºãã¦ããã®ã§ããã©ã¡ã¼ã¿å¼æ°nã¨ãã¼ã«ã«å¤æ°cntã¯æ¶æ» ãã¦ããã¯ãã§ãããfunãé¢æ°ã¨ãã¦å®è¡ããã¨ã5, 10, 15, ...ã¨ããå¤ãå®è¡ãããã³ã«è¿ããã¾ããä¸è¦ãä¸æè°ãªç¾è±¡ã§ãããå¤æ°nã¨cntã¯Counteré¢æ°ã®ã¹ã¿ãã¯ãã¬ã¼ã ä¸ã«ããã®ã§ã¯ãªããStepUpé¢æ°ã«é¢é£ä»ããããã¯ãã¼ã¸ã£ã®ä¸ã«åå¨ãç¶ãã¦ãã¾ããJavaScriptã®å ´åãããããã£ã«ã¢ã¯ã»ã¹å¶éãè¨å®ãããã¨ã¯ã§ãã¾ããããã¯ãã¼ã¸ã£ãç¨ããã¨å®å ¨ã«å¤é¨ããé è½ããå¤æ°ãå®ç¾ãããã¨ãã§ãã¾ãã
ã¯ãã¼ã¸ã£ãæå¹ãªç°¡åãªä¾ã¯ããã¾ãããï¼
ä¾ãã°ããããªã®ã¯ã©ãã§ãããï¼
æå®ããé¢æ°ãå®æçã«æå®ããåæ°ã ãç¹°ãè¿ãã¦å®è¡ããé¢æ°repeatãå®è£ ãã¦ãã ãããç¹°ãè¿ããéä¸ã§ä¸æã§ãããã¨ãããã³ãç¾å¨ã®ç¹°ãè¿ãåæ°ãåå¾ã§ãããã¨ã¨ãã¾ããã¤ã¾ãã以ä¸ã®ãããªä»æ§ã®é¢æ°ã«ãªãã¾ãã
repeat(num, dur, func, args) num: ç¹°ãè¿ãåæ° dur: ç¹°ãè¿ãã®æééé func: ç¹°ãè¿ãå®è¡ãããé¢æ°ãªãã¸ã§ã¯ãã é¢æ°ä¸ã§ã®thisã®å¤ã¯æ»ãå¤ã¨åä¸ã®ãªãã¸ã§ã¯ãã args: funcã«ä¸ããããå¼æ°ã®é å æ»ãå¤ï¼ count, stopã¨ããã¡ã½ãããæã¤ãªãã¸ã§ã¯ã count: ç¾å¨ã®ç¹°ãè¿ãåæ° stop: ç¹°ãè¿ããå¼·å¶çµäºãã
ããªãè¤éã§å®è£ ãé£ããããã«è¦ãã¾ãããã¯ãã¼ã¸ã£ã使ãã°é常ã«ç°¡åã«å®ç¾ã§ãã¾ãã以ä¸ããã®ã³ã¼ãã§ããï¼ã¤ã®é¢æ°ã§å®ç¾ãã¦ãã¾ãã
function repeat(num, dur, func, args) { var cnt = 0; var timer = setInterval(function () { if (cnt++ < num) { func.apply(rv, args); } else { clearInterval(timer); } }, dur); var rv = { stop: function () { clearInterval(timer); }, count: function () { return cnt; } }; return rv; }
ã¯ãã¼ã¸ã£ã使ããªãã§å®è£ ãããã¨ãã§ãã¾ãããè¤éã§è¦éãã®æªãã³ã¼ãã«ãªãã§ãããã
ãããã«
HTML5ã注ç®ãéãã¦ãã¾ããããã¦ã次ã®è¦æ ¼ã§ããECMAScript 5th Editionãã¾ããªããªãªã¼ã¹ããã¾ããééããªãJavaScriptã¯ä»ä»¥ä¸ã«éè¦ãªè¨èªã«ãªãã§ãããããã®åºç¤ããã£ããç解ãããã¨ã¯ã¨ã¦ã大åã§ããä¸çªæ£ç¢ºã«ç解ããã«ã¯è¦æ ¼æ¸ãèªããã¨ã§ããããªããªãé£è§£ãªå 容ã§ãããã«ç解ã§ãããã®ã§ã¯ããã¾ãããæ¬ã¨ã³ããªã¼ãJavaScriptã®åå¦è ã«ã¨ã£ã¦å°ãã§ãåèã«ãªã£ã¦ããã ããã幸ãã§ããã