Skip to content

Conversation

nagaozen
Copy link
Contributor

Just a little of feature detection to leverage the power of native clone!

Just a little of feature detection to leverage the power of native clone!
Copy link
Member

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 using return this.slice(0); would keep the objects reference and add a new behaviour. http://jsfiddle.net/syv2he8o/

},

clone: function(object){
if(Object.create) return Object.create(object);
Copy link

@AmraniCh AmraniCh Oct 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Object.create function creates a new object that inherits directly from the one passed as the first argument, in opposite the Object.clone creates a new object with always the base Object prototype, this is actually changing the Object.clone behavior. https://jsfiddle.net/AmraniCh/u86c0qoL/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmraniCh: In your fiddle, you should use Array.clone with arrays and not Object.clone
https://jsfiddle.net/vu5xt0ed/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergioCrisostomo Actually it Object.clone, I'm just making a test with the array type.
The Mootools Object.clone always clone an object and return a new one with the Object prototype, if we add the return Object.create(object) then the Object.clone will use the object passed as an argument (can be an array, function ..) as a prototype of the newly created object, which is not the native behavior of the Object.clone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants