Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  181] [ 7]  / answers: 1 / hits: 48143  / 14 Years ago, thu, february 17, 2011, 12:00:00

I was studying the jQuery source when I found this (v1.5 line 2295):



namespace = new RegExp((^|\.) +
jQuery.map( namespaces.slice(0).sort(), fcleanup ).join(\.(?:.*\.)?) + (\.|$));


My question is, why use slice(0) here?


More From » jquery

 Answers
12

sort() modifies the array it's called on - and it isn't very nice to go around mutating stuff that other code might rely on.



slice() always returns a new array - the array returned by slice(0) is identical to the input, which basically means it's a cheap way to duplicate an array.


[#93704] Tuesday, February 15, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;