toSource とかはじめて知った。
GreaseMonkey で設定値保存するときはこれ使うといいね! わざわざ JSON 云々のコピペなんてしなくてよさげ。
var foo = new Object;
foo.bar = "test";
foo.baz = "pqpq";
foo.toSource(); //=> '({bar:"test", baz:"pqpq"})'
(new Date).toSource(); //=> (new Date(1142625169156))
(function () { return ""}).toSource(); //=> '(function () { return "";})'
alert.toSource(); //=> 'function alert() {[native code]}'
ていうか、forEach とかあるのね! やべ。
var sum = 0;
[1, 2, 3].forEach(function (item, index, array) {
sum += item;
}, this); // 第二引数は callback 中の this
alert(sum);
同じように (Ruby -> JS) select -> filter, all -> every, map -> map, any -> some が使えるみたいだ。どうせ Fx 用の GM しか書かないから使いまくろう。
- トップ
-
js
-
.toSource(), forEach, etc