第八章的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学习资料
《JavaScript: The Definitive Guide, 5th Edition》是JavaScript学习领域中一本经典的参考书,由David Flanagan撰写,全面且深入地介绍了JavaScript语言。这本书面向已经有一定编程基础的读者,旨在帮助他们掌握...
《JavaScript The Definitive Guide 5th Edition》是JavaScript编程领域的经典之作,由David Flanagan撰写,面向希望深入理解和熟练掌握JavaScript语言的开发者。这本书详细介绍了JavaScript的核心概念、语法以及...
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:
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.The.Definitive.Guide 5th Edition》是一本权威的JavaScript编程指南,由David Flanagan撰写,针对JavaScript的第五版进行了全面的讲解。这本书是JavaScript开发者的重要参考资料,尤其对于那些希望...
《JavaScript The Definitive Guide》第五版是JavaScript编程领域中一本经典的权威教程,由David Flanagan撰写。这本书全面深入地探讨了JavaScript语言的各种特性和用法,为开发者提供了详尽的指导。配合第四版一同...
[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带来的变化
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.
本资源包含两份JavaScript的API文档:《javascript the definitive guide 5th edition aug 2006.chm》和《Javascript Edition.chm》,尽管它们是英文版,但通过丰富的示例,即使中文用户也能轻松理解。 ...
内含《JavaScriptReference.chm》,内容来自《JavaScript: The Definitive Guide, 5th Edition》的“Part III: Core JavaScript Reference”(英文版)。 说明: 该文档仅是Javascript内置对象与函数的API说明文档,...
关于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的两本手册——"JavaScript The Definitive Guide 5th.chm"和"JavaScript语言参考手册.chm",是深入学习这门动态编程语言的宝贵资料。JavaScript是网页交互性的关键,用于处理用户输入、更新DOM、...