Created
April 7, 2015 10:31
-
-
Save weiland/0056b44c75dd6158db75 to your computer and use it in GitHub Desktop.
Clone an Array in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// extending the Array prototype | |
Array.prototype.clone = function clone() { | |
return this.slice(0); | |
}; | |
// example | |
var arr = [4,5]; | |
var copy = arr.slice(0); | |
// better (since we do not extend the prototype): | |
function cloneArray(arr) { | |
return arr.slice(0); | |
} | |
var arr = [4,5]; | |
var copy = cloneArray(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment