[dellajs] DeLLa.JS JavaScript第5版読書会#3のlog

メモったことを記す。

constructorプロパティ

等価

if (typeof o == "object") && (o.constructor == Date))
if (typeof o == "object") && (o instanceof Date))

7.5 配列

2個の要素を持つ配列になる。3個じゃない。

var undefs = [,,]

まぁ、こうすれば紛らわしくない

var a = new Array(3);

7.6.1 配列の要素の追加

a[0] = 1;
a[10000] = "hoge";

0と10000のふたつの要素にメモリを割り当てるだけで、その間の9999個のインデックスに対応する要素にはメモリを割り当てない。

7.6.5 配列の長さの変更

きもい。

js> var hoge = new Array(1,2,3);
js> hoge.length
3
js> hoge.length = 10;
10
js> hoge
1,2,3,,,,,,,
js
js> a.length = 3.5
29: RangeError: invalid array length

7.7.3 sortメソッド

破壊的

sortつかって配列をランダムに並べ替える。
js> var ary = ["b", "A", "e", "d", "D"]
js> ary.sort(function(){return 0.5 - Math.random()})
d,e,b,A,D

http://www.javascriptkit.com/javatutors/arraysort.shtml参照

8.2.2 可変長の引数リスト

  • arguments識別子

キモい。

js> function f(x) {
print(x);
arguments[0] = null;
print(x);
}
js> f(3);
3
null
  • arguments.callee
js> function(x) {
if (x <= 1) return 1;
return x * arguments.callee(x-1);
}

8.3 データとしての関数

このへんから集中力がなくなってきたので写経しながら理解していく。

var op = {
    add		: function(x, y){ return x + y; },
    substruct	: function(x, y){ return x - y; },
    multiply	: function(x, y){ return x * y; },
    divide	: function(x, y){ return x / y; },
    pow		: Math.pow
};

function operate(op_name, operand1, operand2) {
    if (typeof op[op_name] == "function") {
	return op[op_name](operand1, operand2);
    } else {
	throw "unknown operator";
    }
}

print(operate("add", 1,2));
print(operate("hige", 1,2));

8.4 メソッドとしての関数

var calculator = {
    operand1: 1,
    operand2: 1,
    compute: function() {
	this.result = this.operand1 + this.operand2;
    }
};

calculator.compute();
print(calculator.result);

今回の範囲は読んでて楽しかった。apply,call,thisのあたりはもう少し読み込む必要ある。
次回は7/5(土)懇親会はfirefox3.0のリリースパーティと一緒にやるよ!楽しみ。