JavaScript 面向对象编程技巧深度解析
1. 为 Set 原型添加方法
在 JavaScript 里,我们能够借助代码为 Set.prototype 增添方法。下面的代码展示了如何运用 extend() 函数来实现这一目的:
// Add these methods to the Set prototype object.
extend(Set.prototype, {
// Convert a set to a string
toString: function() {
var s = "{", i = 0;
this.foreach(function(v) { s += ((i++ > 0)?", ":"") + v; });
return s + "}";
},
// Like toString, but call toLocaleString on all values
toLocaleString : function() {
var s = "{", i = 0;
this.foreach(function(v) {
if (i++ > 0) s += ", ";
if (v == null) s += v; // null & undefined
else s += v.toLocaleString(); // all others
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



