Add a generic clone() method to all classes through Base which creates a new item using the constructor and passing 'this'.

This commit is contained in:
Jürg Lehni 2011-05-19 18:34:22 +01:00
parent 6854f7d026
commit b37ba3d858

View file

@ -16,8 +16,12 @@
// Extend Base with utility functions used across the library. // Extend Base with utility functions used across the library.
Base.inject({ Base.inject({
statics: true,
clone: function() {
return new this.constructor(this);
},
statics: {
read: function(list, start, length) { read: function(list, start, length) {
var start = start || 0, var start = start || 0,
length = length || list.length - start; length = length || list.length - start;
@ -97,8 +101,8 @@ Base.inject({
}, },
/** /**
* Utility function for rendering numbers to strings at a precision of up * Utility function for rendering numbers to strings at a precision of
* to 5 fractional digits. * up to 5 fractional digits.
*/ */
formatNumber: function(num) { formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString(); return (Math.round(num * 100000) / 100000).toString();
@ -113,4 +117,5 @@ Base.inject({
this.push(key + ': ' + value); this.push(key + ': ' + value);
}, []).join(', ') + ' }'; }, []).join(', ') + ' }';
} }
}
}); });