473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JS Array Methods, Best Practice: When to modify, when to return a copy?

FLEB
30
I'm creating a couple helper methods to beef up the Array type: Array.shuffle() and Array.rotate(steps) (rotates an array like [1,2,3,4,5], [2,3,4,5,1], [3,4,5,1,2], etc). What I'm wondering is what the consensus is on modifying the original array, versus returning a new array.

My instinct is that in these cases, since they're operations on the array itself, I should simply modify the array in-place.
Dec 22 '08 #1
3 1317
rnd me
427 Expert 256MB
array prototypes don't modify the orig (unless you hard code a variable name into the method), so i would say that returning a copy is the normal practice.
Dec 22 '08 #2
FLEB
30
You know what... I actually just found this out. Apparently assigning a value to "this" is not allowed, so I have to use copies. It's a bit odd, though, since a few of the String object builtins operate directly on the object (such as "sort" and "splice"). I'm not sure how you'd implement this in pure JS-- some sort of iteration?

Edit: Playing with it further...

Although it does seem to work if I use a simple assignment (by reference, as JS does with objects) of "this" to a different var name.

Edit again: Nope. Scratch that. Assigning "that = this" just makes a copy.
Dec 22 '08 #3
rnd me
427 Expert 256MB
neither "sort" or "splice" is a string method.
i think all native string protos return a copy.

some native array methods do affect the original; examples include pop and push.

custom ones do not, although you can return something besides "this"...

you can clobber the array by referring to it's variable name in a proto, but that makes it pretty fragile.
Dec 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
7
by: Peter | last post by:
I want to create a multidemensional arraylist. Seeing as they don't exist I was wondering if there is a way to create a class that works like one. I basically want to use it like this Dim...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
32
by: mnaydin | last post by:
Assume the main function is defined with int main(int argc, char *argv) { /*...*/ } So, is it permitted to modify the argv array? The standard says "The parameters argc and argv and the strings...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
5
by: Jon Slaughter | last post by:
I want to create function that takes either an array or a simple type but I want it not to distinguish between them, function Decrypt(&$str) { foreach($str as &$val) { $val =...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.