-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use native clone methods when available #2719
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,6 +368,7 @@ var cloneOf = function(item){ | |
}; | ||
|
||
Array.implement('clone', function(){ | ||
if(this.slice) return this.slice(0); | ||
var i = this.length, clone = new Array(i); | ||
while (i--) clone[i] = cloneOf(this[i]); | ||
return clone; | ||
|
@@ -397,6 +398,7 @@ Object.extend({ | |
}, | ||
|
||
clone: function(object){ | ||
if(Object.create) return Object.create(object); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AmraniCh: In your fiddle, you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SergioCrisostomo Actually it |
||
var clone = {}; | ||
for (var key in object) clone[key] = cloneOf(object[key]); | ||
return clone; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.clone
clones also eventual objects inside the array. Doing a early return here usingreturn this.slice(0);
would keep the objects reference and add a new behaviour. http://jsfiddle.net/syv2he8o/