javascript definition guide(javascript权威指南)第六章 Example 6-2里一个小错误

本文针对JavaScript权威指南中的一处小错误进行了修正,并给出了正确的对象合并方法。详细对比了extend与union函数的不同之处,并展示了如何避免在合并过程中不必要的属性覆盖。

javascript definition guide(javascript权威指南)第六章 Example 6-2里的例子有一个小错误

/**
* Copy the enumerable properties of p to o, and return o.
* If o and p have a property by the same name, o's property is overwritten.
* This funcdtion does not handle getters and setters or copy attributes.
*/
function extend(o, p){
	for (prop in p)
		o[prop] = p[prop];
	return o
}

/**
* Return a new object that holds the properties of both o and p.
* If o and p have properties by the same name, the values from o are used
*/
function union(o, p) {
	return extend(extend({}, o), p);
}


这里的union函数实现,如果o和p有相同的属性,o的属性还是会被p中的属性覆盖。

正确的函数应该是:

function union(o, p) {
	return extend(p, extend({}, o));
}


中文名: JavaScript权威指南 (6) 原名: JavaScript: The Definitive Guide: Activate Your Web Pages, 6th edition 作者: David Flanagan 版本: 英文文字版-pdf/EPUB + 完整书中源代码 出版社: O'Reilly 书号: 978-0596805524 发行时间: 2011年05月10日 语言: 英文 内容简介: Since 1996, JavaScript: The Definitive Guide has been the bible for JavaScript programmers—a programmer's guide and comprehensive reference to the core language and to the client-side JavaScript APIs defined by web browsers. The 6th edition covers HTML5 and ECMAScript 5, with new chapters on jQuery and server side JavaScript. It's recommended for experienced programmers who want to learn the programming language of the Web, and for current JavaScript programmers who want to master it. 目录: Chapter 1 Introduction to JavaScript Core JavaScript Chapter 2 Lexical Structure Chapter 3 Types, Values, and Variables Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects Chapter 7 Arrays Chapter 8 Functions Chapter 9 Classes and Modules Chapter 10 Pattern Matching with Regular Expressions Chapter 11 JavaScript Subsets and Extensions Chapter 12 Server-Side JavaScript Client-Side JavaScript Chapter 13 JavaScript in Web Browsers Chapter 14 The Window Object Chapter 15 Scripting Documents Chapter 16 Scripting CSS Chapter 17 Handling Events Chapter 18 Scripted HTTP Chapter 19 The jQuery Library Chapter 20 Client-Side Storage Chapter 21 Scripted Media and Graphics Chapter 22 HTML5 APIs Core JavaScript Reference Client-Side JavaScript Reference Colophon
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值