第7ç« ãªãã¸ã§ã¯ãã¨é å
若手IT勉強会 - 第5回勉強会ã§è¼ªè¬ããããã®ã¾ã¨ãããããå ã«è¼ªè¬äºå®ã§ãã
- ä½è : David Flanagan,æä¸å
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã»ã¸ã£ãã³
- çºå£²æ¥: 2007/08/14
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 52人 ã¯ãªãã¯: 1,011å
- ãã®ååãå«ãããã° (270件) ãè¦ã
ã¡ãªã¿ã«ããã®ã¾ã¨ãã¯è¥æITåå¼·ä¼ ãµã¤æ¬èªæ¸ä¼#4 ã«åå ããå½æ¥ã®ï¼ã11æãããã«ã¾ã¨ããå
容ã§ãã
ããééã£ã¦ããç®æçãããã¾ããããææãã ããï¼
ãªãã¸ã§ã¯ãã«ã¤ãã¦
7.1 ãªãã¸ã§ã¯ãã®çæ
- "{}" ãªãã©ã«ã使ãæ¹æ³ã¨ new æ¼ç®åãç¨ãã¦ã³ã³ã¹ãã©ã¯ã¿ãå¼ã³åºãã¦çæããæ¹æ³ãããã
- "{}" 㨠new Object() ã¯ç価ã ã
var obj1 = {} var obj2 = new Object() // obj1 ã¨ç価 var obj3 = new Array() // Arrayãªãã¸ã§ã¯ãã®çæ
- "{}" ã®ä¸ã«ã¯ ããããã£å:å¤ ã¨ãããã¢ã§è¨è¿°ãããã¨ãã§ãããã , ã§è¤æ°è¨è¼ã§ããã
var obj1 = { test1:1, // æ°å¤ã§ã test2:"æåå", // æååã§ã test3:{name:"hoge"} // ãªãã¸ã§ã¯ãã§ãå ¥ãããã }
- ããããã£åã¯èå¥åããããã¯æååã使ããã¨ãã§ããã
// èå¥å var obj1 = { test1:1 } // æåå var obj2 = { "test1":1 } alert(obj1.test1) // 1 alert(obj2.test1) // 1 alert(obj1["test1"]) // 1 alert(obj2["test1"]) // 1
7.2 ãªãã¸ã§ã¯ãã®ããããã£
- ããããã£ã¸ã®å¤ã®è¨å®ã¯ var ã®å®£è¨çã¯ä¸è¦ã§ããã®ã¾ã¾ä»£å ¥ã§ããã
- ãªãã¸ã§ã¯ãã®å ¥ãåãåºæ¥ãã
- ã»ã¼ãã®ã¾ã¾ã®æå³ãªã®ã§ä»ã¯å²æããã
var obj = {} obj.name = "test" // 宣è¨ä¸è¦ã§ä»£å ¥ã§ããã alert(obj.name) // "test" obj.hoge = {fuga:"piyo"} // ãªãã¸ã§ã¯ãã®å ¥ãåãã§ããã alert(obj.hoge.fuga) // "piyo"
7.2.1 ãªãã¸ã§ã¯ãããããã£ã®èª¿æ»
- for/in æ§æã§ã¦ã¼ã¶å®ç¾©ã®ããããã£ã調ã¹ãäºãã§ããã
var obj = { name:"hoge", age:20 } var name = "" for(var o in obj) { name += o+"\n" } alert(name) // name\nage\n
- ã§ããçææ§ï¼ãFireBug ãªã dir é¢æ°ã§ããããã£ã¨å¤ã®åç §ãå®ç¾ã§ãã¾ããï¼
var obj = { name:"hoge", age:20 } dir(obj)// age 20\nname "hoge"
7.2.2 ããããã£ã®åå¨ç¢ºèª
- in æ¼ç®åã§ç¢ºèªãããã¨ãã§ããã
o = {x:10} if ("x" in o) o.x = 1 // x ããããã£ãããã° 1 ãä»£å ¥ alert(o.x) // 1
o = {x:10} if (o.x !== undefined) o.x = 1 // x ããããã£ãããã° 1 ãä»£å ¥ alert(o.x) // 1
- ã§ããã®æ¹æ³ã ã¨ã o.x = undefined ã¨ããå ´åã«æ£ããåããªãã®ã§ãæ±ç¨çã«ããã«ã¯ããããæ¹ãè¯ãã
o = {x:10} if (o.x) alert("hoge") // 表示ããã o = {x:undefined} if (o.x) alert("fuga") // 表示ãããªã o = {} if (o.x) alert("piyo") // 表示ãããªã
7.2.3 ããããã£ã®åé¤
- delete æ¼ç®åã使ãäºã§åé¤ãã§ããã
- undefinedãä»£å ¥ããããã¨ããæå³ã§ã¯ãªãã®ã§ãfor/in ã in æ¼ç®åã§ã調ã¹ãããªããªãã
o = {x:10} delete o.x alert(o.x) // undefined
7.3 é£æ³é åã¨ãã¦ã®ãªãã¸ã§ã¯ã
- ãªãã¸ã§ã¯ãã®ããããã£ã¯æååã§ãã¢ã¯ã»ã¹ã§ãããã
- æååã§ã¢ã¯ã»ã¹ããå ´åãå®è¡æã«åçã«çæãããã¨ãå¯è½ã ãã
var addr = "" for (i = 0; i < 4; i++) { addr += customer["address" + i] + '\n'; // address0ãaddress3 ã¾ã§ã®ããããã£ã®å¤ãåå¾ã§ãã }
- ãµã¼ããµã¤ããã渡ã£ã¦ãããã¼ã¿çãèªåèªèº«ã§ææ¡ã§ããªããã¼ã¿ã«å¯¾ãã¦ãæè»ã«ã¢ã¯ã»ã¹ã§ããã
7.4 Object ã®ããããã£ã¨ã¡ã½ãã
- ãã¹ã¦ã®ãªãã¸ã§ã¯ã(Date ã¨ã Array çãå«ãã¦)ã®è¦ªã¯ã©ã¹ã§ãã Object ã®ããããã£ãã¡ã½ããã«ã¤ãã¦ç´¹ä»ããã
7.4.1 constructor ããããã£
- ãªãã¸ã§ã¯ãã®çæå ã®ã¯ã©ã¹ã調ã¹ãäºãã§ããã
obj1 = {} obj2 = new Date() obj3 = "test" alert(obj1.constructor == Object) // true alert(obj2.constructor == Date) // true alert(obj3.constructor == String) // true
- ç¹å®ã®ã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã確èªããæ¹æ³
var o = new Date() if ((typeof o == "object") && (o.constructor == Date)) // Date åç¬èªã®å¦ç if ((typeof o == "object") && (o instanceof Date)) // ä¸è¨ã¨ç価
7.4.2 toString()ã¡ã½ãã
- ã¡ããã¨å®è£ ãããï¼ï¼è©³ãã㯠9 ç« ã§ï¼
7.4.3 toLocaleString()ã¡ã½ãã
- toString() ã®æååã«å¯¾ãã¦ããã¼ã«ã©ã¤ãºãããæååãæä¾ããã
- JavaScript1.5 ã§ã¯ toString ã¨åæ§ã« Object ã¯ã©ã¹ã«å®è£ ããã¦ããã
- Date åã¨ãã¯ç¬èªã®æååãå®è£ ããã¦ãã
7.4.4 valueOf()ã¡ã½ãã
- æåå以å¤ã®ããªããã£ãåã¸å¤æããã®ã«ä½¿ãããã
- 詳ãã㯠9 ç« ã§ï¼
7.4.5 hasOwnProperty()ã¡ã½ãã
- ç¶æ¿ãããã®ã§ã¯ãªãããããã£ãæã£ã¦ããå ´åã« true ãè¿ãã
obj = {hoge:"fuga", piyo:undefined} alert(obj.hasOwnProperty("toString")) // false(Objectããç¶æ¿ãã¦ãã) alert(obj.hasOwnProperty("hoge")) // true alert(obj.hasOwnProperty("piyo")) // true(undefined ã§ã true ã«ãªãã) alert(obj.hasOwnProperty("aaaaa")) // false(ãããªã®æã£ã¦ãªã)
7.4.6 propertyIsEnumerable()ã¡ã½ãã
- for/in ã§èª¿ã¹ãããããããã£ã®å ´å true ãè¿ãã
- ã¶ã£ã¡ãããhasOwnProperty ã¨ä¸ç·ã ã
obj = {hoge:"fuga", piyo:undefined} alert(obj.propertyIsEnumerable("toString")) // false(Objectããç¶æ¿ãã¦ãã) alert(obj.propertyIsEnumerable("hoge")) // true alert(obj.propertyIsEnumerable("piyo")) // true(undefined ã§ã true ã«ãªãã) alert(obj.propertyIsEnumerable("aaaaa")) // false(ãããªã®æã£ã¦ãªã)
7.4.7 isPrototypeOf()ã¡ã½ãã
- ãã®ã¡ã½ãããå¼ã³åºãããªãã¸ã§ã¯ããå¼æ°ã§ãããªãã¸ã§ã¯ãã®ãããã¿ã¤ããªãã¸ã§ã¯ãã§ããå ´åã« true ãè¿ãã
- ã¶ã£ã¡ãããããããããªããï¼ï¼
obj = {hoge:"fuga", piyo:undefined} Object.prototype.isPrototypeOf(obj) // true(Objectã®prototype ãç¶æ¿ãã¦ããã) Object.isPrototypeOf(obj) // false obj.isPrototypeOf(Object.prototype) // false
é åã«ã¤ãã¦
7.5 é å
- æ®éã® Object ã¨ä¼¼ã¦ããã
- å®ã®ã¨ããããªãã¸ã§ã¯ã + αãªæ©è½ã追å ããã¦ããã ãã ã
- + αã®é¨åã£ã¦ããã®ã¯ length ããããã£ã¨ãã ã
- å¤ã«ã¯ããªããã£ãå以å¤ã«ããªãã¸ã§ã¯ãããã®ã¾ã¾å ¥ããäºãã§ããã
var ary1 = [] var ary2 = [11,12,13] // [0]ã[2]ã« 11 ã 13 ã®å¤ãå ¥ã£ã¦ã var ary3 = [{hoge:"fuga"}, 1] alert(ary3[0].hoge) // fuga var ary4 = [,] // 1åã®ç©ºã®è¦ç´ ãæã¤é å
- Array ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã使ã£ã¦çæã§ããã
var ary1 = new Array() // 空ã®é å var ary2 = new Array(1, 2, "str") // åæå¤ãä»£å ¥ãã var ary3 = new Array(10) // 10 åã®ç©ºè¦ç´ ã®é åãçæãã
7.6 é åã®è¦ç´ ã®èªã¿æ¸ã
- [] æ¼ç®åã§ã¢ã¯ã»ã¹ã§ããã
- æ·»åã«ã¯å¤æ°ã使ããã
- é åã®æ·»åã¨ãã¦ã¯æ´æ°å¤ãã使ããªãããè² æ°ãè«çå¤ã使ã£ãå ´åã¯æååã«å¤æããã¦ããã®æååã®ããããã£ã¨ãã¦æ±ãããã
ary = [] ary[0] = 10 alert(ary.length) // 1 i = 1 ary[i] = 100 alert(ary.length) // 2 alert(ary[0]) // 10 alert(ary["0"]) // 10(æååã§ãæ´æ°å¤ãªãã¢ã¯ã»ã¹ã§ããã) ary["hoge"] = 199 alert(ary.length) //2 ï¼"hoge"ã¯ããããã£ãªã®ã§é åã¨ãã¦ã¯ã«ã¦ã³ãããã¦ããªãï¼
7.6.1 é åã®è¦ç´ ã®è¿½å
- æ·»åã«å¯¾ãã¦è¿½å ãã¦ããã°è¿½å ãããã
- é常ã®ãªãã¸ã§ã¯ãçã«å¯¾ãã¦ãæ·»åã使ã£ã¦è¿½å ã¨ãã§ãããã©ãArray ãªãã¸ã§ã¯ããããªãã¨è¦ç´ æ°ãåããªãã
ary = new Array() alert(ary.length) // 0 ary[0] = 11 alert(ary.length) // 1 obj = {} alert(obj.length) // undefined obj[0] = 10 alert(obj.length) // undefined
7.6.2 é åã®è¦ç´ ã®åé¤
- delete æ¼ç®åã§ã¯å¤ã¯åé¤ã§ãã¦ãé åã®è¦ç´ èªä½ãåé¤ã§ããªãã
- shift ã pop, slice é¢æ°ã使ã£ã¦åé¤ããã
7.6.3 é åã®é·ã
- Array() ã [] ã§çæããããªãã¸ã§ã¯ã㯠length ããããã£ãæã£ã¦ããã
- length ããããã£ã¯é åã®å¤§ãããä¿æãã¦ããã
- å³å¯ã«è¨ãã¨ãé åã®æ·»åã®æå¤§å¤ + 1 ãå¤ã¨ãã¦æã£ã¦ããã
ary = new Array() alert(ary.length) // 0 ary[10] = 10 alert(ary.length) // 11
7.6.4 é åã®è¦ç´ ã®å·¡å
- length ããããã£ã§ã¢ã¯ã»ã¹ã§ããã
var ary = ["hoge", "fuga", "piyo"] for (var i = 0; i < ary.length; i++) { alert(ary[i]) }
- 0ããå§ã¾ãäºãä¿è¨¼ã§ããªãå ´åã¯ä¸è¨ã®ããã«ããã¨è¯ãã
var ary = [, "fuga", "piyo"] for (var i = 0; i < ary.length; i++) { if(ary[i]) alert(ary[i]) }
7.6.5 é åã®é·ãã®å¤æ´
- length ããããã£ã¯èªã¿æ¸ãå¯è½ã ã
- length ããããã£ãçãããã¨ãç¯å²å¤ã«ãªã£ãè¦ç´ ã¯ç ´æ£ãããã
- length ããããã£ãé·ãããã¨ãå¤ã¯æªå®ç¾©ã¨ãã¦è¿½å ãããã
var ary = ["hoge", "fuga", "piyo"] alert(ary.length) // 3 alert(ary[ary.length-1]) // piyo ary.length = 2 alert(ary.length) // 2 alert(ary[ary.length-1]) // fuga alert(ary[2]) // piyo ããã£ããã© undefined
7.6.6 å¤æ¬¡å é å
- é åã®é åã¨ãã¦è¡¨ç¾ã§ããã
- ary[x][y] ã¿ããã«ããã°è¯ãã
7.7 é åã®ã¡ã½ãã
- ry
7.7.10 JavaScript 1.6ã§è¿½å ãããã¡ã½ãã
- Firefox 1.5 ã§ã¯å®è£ ããã¦ãããã©ãæ¨æºãããã¦ãã訳ã§ã¯ãªãã®ã§ãæ±ç¨çã§ã¯ãªããããªã®ã§(ry
7.8 é åã®ãããªãªãã¸ã§ã¯ã
- é å㯠length ããããã£ãç¹å¥ãªåãããããã¨ãä»ã®ãªãã¸ã§ã¯ãã¨ã¯éãã¨ããã ã
- é åã«æ°ããè¦ç´ ã追å ããã㨠length ããããã£ãæ´æ°ãããã
- length ããããã£ãå¤æ´ãããã¨ã§è¦ç´ æ°ãå¤ãå¤æ´ãããã
- length ããããã£ãæã£ã¦ãã¦ããã®å¤ãã¤ã³ããã¯ã¹ã¨ãã¦ãã使ããªããã¸ãã¯ãªãã°âé åã§ã¯ãªãã¦ãâé åã®å¦ãæ±ããã
var a = {} var size = 10 for(var i = 0; i < size; i++) { a[i] = i } a.length = size // è¦ç´ ãèµ°æ»ãããããªå¦çãªãã°é åã¨åãããã«æ±ãã var total = 0 for(var i = 0; i < a.length; i++) { total+= a[i] } alert(total)
- é åã®ãããªãªãã¸ã§ã¯ãã¨ã㦠"Arguments" ã document.getElementsByTagName()ã®æ»ãå¤çãããã
ããã¾ã