投稿

ラベル(sample)が付いた投稿を表示しています

javascript arguments sample codes

JavaScript the definitiveguide 6th Edition (page175)にあったargumentsのサンプル。 smjsで実行できるものを書いてみた。 // javascriptらしい自由度の高いsum var isArray = Function.isArray || function(o) { return typeof o == "object" && Object.prototype.toString.call(o) == "[object Array]"; }; function flexisum(a) { var total = 0; for (var i = 0; i < arguments.length; i++) { var element = arguments[i], n; // Ignore null and undefined arguments. if (element == null) continue; if (isArray(element)) n = flexisum.apply(this, element); else if (typeof element == "function") n = Number(element()); else n = Number(element); if (isNaN(n)) throw Error("flexisum(); can't convert " + element + " to number"); total += n; } return total; }; var test = function() { return 5; }; var test_str = function() { return "9"; }; var ar_test = [1,1]; print(isArray(ar_test)); // 配列の中に他の配列や関数が含まれていて...