第八章的Flexsum的例子,书上的代码如下:
function flexisum(a) {
var total = 0;
for(var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (!element) continue; // Ignore null and undefined arguments
// Try to convert the argument to a number n,
// based on its type
var n;
switch(typeof element) {
case "number":
n = element; // No conversion needed here
break;
case "object":
if (element instanceof Array) // Recurse for arrays
n = flexisum.apply(this, element);
else n = element.valueOf(); // valueOf method for other objects
break;
case "function":
n = element(); // Try to invoke functions
break;
case "string":
n = parseFloat(element); // Try to parse strings
break;
case "boolean":
n = NaN; // Can't convert boolean values
break;
}
// If we got a valid number, add it to the total.
if (typeof n == "number" && !isNaN(n)) total += n;
// Otherwise report an error
else throw new Error("sum(): can't convert " + element + " to number");
}
return total;
}
我调试后发现这个apply应用的不太对,应该是call。我将这个例子改进了一下(当然也改动了一下),代码如下:
<html>
<head>
<title>
</title>
</head>
<body>
<script language="javascript">
function flexsum(arr) {
var rst = 0;
if (arr instanceof Array || typeof arr == "object" && "length" in arr) {
for (var i = 0 ; i < arr.length; ++i) {
if (!arr[i]) {
continue;
}
else {
var n;
if (typeof arr[i] == "number") {
n = arr[i];
}
else if (typeof arr[i] == "function") {
n = arr[i]();
}
else if (typeof arr[i] == "string") {
n = parseFloat(arr[i]);
}
else if (typeof arr[i] == "boolean") {
if (arr[i]) {
n = 1.0;
}
else {
n = 0.0;
}
}
else if (typeof arr[i] instanceof Array || typeof arr[i] == "object" && "length" in arr[i]) {
n = flexsum.call(this, arr[i]);
}
else { // typeof arr[i] == "object"
n = arr[i].valueOf();
}
if (typeof n == "number" && !isNaN(n)) {
rst += n;
}
}
}
return rst;
}
else {
throw new Error("The argument " + arr.toString() + "is not a Array or Arraylike object!");
}
}
var arr1 = [1,2,3];
document.write(flexsum(arr1) + "<br>");
var arr2 = [1,2,3,"4", [5,6,7]];
document.write(flexsum(arr2) + "<br>");
</script>
</body>
</html>
分享到:
相关推荐
JavaScript The Definitive Guide 5th 权威指南 第五版 英文 教材 非常经典的javascript学习资料
e文版,chm格式,JavaScript学习经典书籍
JavaScript The Definitive Guide 5th Edition
This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including:
JavaScript.The.Definitive.Guide 5th Edition(CHM)
Part IV is a reference for client-side JavaScript, covering legacy web browser APIs, the standard Level 2 DOM API, and emerging standards such as the XMLHttpRequest object and the <canvas> tag. ...
[JavaScript权威指南(第5版)中文版(下)].(JavaScript.The.Definitive.Guide.5th.Edition).David.Flanagan.影印版
本书还包括详细的参考手册,涵盖了JavaScript的核心 API、遗留的客户端API和W3C标准DOM API,记述了这些API中的每一个JavaScript对象、方法、性质、构造函数、常量和事件处理程序。 这本最畅销的JavaScript参考书...
《JavaScript权威指南(第5版)》还包括详细的参考手册,涵盖了JavaScript的核心API、遗留的客户端API和W3C标准DOM API,记述了这些API中的每一个JavaScript对象、方法、性质、构造函数、常量和事件处理程序。
JavaScript The Definitive Guide 5th Edition.chm
完整版,完美版! 全球超过30万的JavaScript程序员的必备参考 JavaScript之父高度评价和强力推荐的权威之作 第五版与你一起迎接Web 2.0的浪潮,拥抱Ajax带来的变化
是一个权威的js教材,只是英文的,配合js权威指南第4版可以很容易理解
JavaScript: The Definitive Guide, 5th Edition <br>By David Flanagan <br>............................................... <br>Publisher: O'Reilly <br>Pub Date: August 2006 <br>Print ISBN-10: 0-596-10199...
JavaScript The Definitive Guide, 5th Edition.chm
资料包括了最权威的JavaScript The Definitive Guide 5th.chm 高效javascript和javascript设计模式这三本都是英文版,还有一本中文版的悟透javascript.
内含《JavaScriptReference.chm》,内容来自《JavaScript: The Definitive Guide, 5th Edition》的“Part III: Core JavaScript Reference”(英文版)。 说明: 该文档仅是Javascript内置对象与函数的API说明文档,...
这本书最大的特点就是简明易懂,循序渐进,适合初学者,非常容易上手。...2. JavaScript: The Definitive Guide, 5th Edition 3. Professional JavaScript for Web Developers 三部书都在我的资源中了,请大家下载。
关于Java Web 开发的三个学习资料:「HTML & XHTML The Definitive Guide, 5th Edition.chm」、「JavaScript The Definitive Guide, 5th Edition.chm」、「Java.How.to.Program,7th.Edition.pdf」
英文版,包含源码 英文名:JavaScript: The Definitive Guide, 5th Edition By David Flanagan 出版社:O'Reilly