Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
25 events
when toggle format what by license comment
Jul 17, 2018 at 15:23 review Suggested edits
Jul 17, 2018 at 15:35
Aug 15, 2017 at 8:43 comment added virtualLast do this: ` var arr = [5, 15, 110, 210, 550]; var index = arr.indexOf(210); if (index > -1) { arr.splice(index, 1); }`
Mar 30, 2017 at 19:34 comment added jadkik94 This is a SUPER bad idea; when you spend 5 hours debugging an issue that is caused by this you'll realize this.
Feb 11, 2017 at 16:32 comment added Lukas Liesis @YoYoYonnY I'm not saying that you should never ever extend prototype. Just I think extending it for some random utility functions is not a good idea.
Feb 10, 2017 at 13:50 comment added yyny @LukasLeisis Lastly, to add to the previous discussion about for in loops; The real 'good practice' here is to never use them without a Object.hasOwnProperty check. In the case of Array, you would want to use for (var i=0, len=a.length; i < len; i++) { let v = a[i]; /* ... */ } instead. Or, if you really want to use for in, make sure your code is safe before you do, for example by removing all properties in Array.prototype found with a for in loop over an empty array, and re-defining them with Object.defineProperty(Array.prototype, key, {enumerable: false}); as @naXa suggested.
Feb 10, 2017 at 13:44 comment added yyny @LukasLiesis Furthermore, I have legitimately found people who refuse to use prototype, preprocessors, LinkedLists, whatever, simply because someone on the internet, usually StackOverflow, told them that they should never use them. This is just not true. There is "good practice", and there is false information, and the statement like "prototype should never be used on existing objects" is simply not good practice. MDN happily defines polyfills for several functions in Array.prototype. Some of these will break badly written existing code. But MDN is not at fault, the bad programmers are.
Feb 10, 2017 at 13:32 comment added yyny @LukasLiesis So you are clearly not understanding my comment. My whole point is that you can do whatever you want with prototypes, macros, and recursion as long as you don't have to interact with other code, or you know those interactions are safe. Your point that dependencies might change and break existing code using prototype adds nothing to this. I did, and do, in fact, know what there is to know about prototypes as well as preprocessors, recursion, LinkedLists, etc. and it annoys me a lot that there are people claiming you should never use them simply because you could use them wrongly.
Feb 9, 2017 at 14:45 comment added Lukas Liesis @YoYoYonnY Imagine JS world where everyone thinks to change prototype is OK for some random functions. if you change prototype, other libraries change it and all other dependencies change (it's normal these days to have tens of dependencies, so chance of 2 libs that changed function increases), then you finally will mess something and you, or your unlucky colleagues will have the worst days ever finding what's wrong. While your comment is 2 years old, i hope you already learned a lot more about prototype and how tricky it can become and how it's used and when it's ok to change.
May 12, 2016 at 12:59 comment added superluminary Directly hacking the prototypes of native objects is rarely a clever idea in JavaScript. You may create unpredictable problems. If another framework tries to iterate over the array by keys, your function will be one of the values output. This is not cool in the case of an array. If you wish to modify the Array prototype, follow @naXa 's advice and use Object.defineProperty. Note that this will sacrifice <=IE8 support. There is no way to polyfill Object.defineProperty because it's a low level language feature.
Aug 28, 2015 at 9:38 comment added naXa stands with Ukraine @YonnTrimoreau and what? define a non-enumerable property Object.defineProperty(Array.prototype, "remove", {enumerable : false});
Jun 29, 2015 at 12:52 comment added Yonn Trimoreau @YoYoYonnY You are missing all JS frameworks.... They ALL use for(var key in arr){ } at some point. I'm talking about jQuery, AngularJS... Some of the biggest.
Dec 6, 2014 at 0:49 comment added Blair Anderson why do people show examples adding to the array prototype? stack overflow is for learning good practices
Nov 24, 2014 at 13:36 comment added boades @user797257, for(var key in arr) { %use key in some way% } in IE8 will also give you 'indexOf' as one of elements of the array.
Apr 18, 2014 at 8:57 comment added IvanM very bad solution in while it running indexOf on each iteration, take a look at stackoverflow.com/questions/7310559/…
Oct 31, 2013 at 7:34 comment added user797257 @Doorknob this solution has quadratic asymptotic complexity in speed, while the algorithm is really linear.
Oct 17, 2013 at 9:45 comment added unicorn2 This is a good solution. Thanks. Just note that <IE9 doesn't support indexOf. I just changed: while ((ax= arr.indexOf(what)) !== -1) to while ((ax = $.inArray(what, this)) !== -1) {
Jul 23, 2013 at 22:38 comment added Majid Fouladpour @madeinstefano, one or two examples of the funny (bad) things that would happen?
May 7, 2013 at 12:46 comment added szanata Never change Array prototype. Funny things starts to happen.
S Nov 21, 2012 at 5:38 history suggested CommunityBot CC BY-SA 3.0
Made whitespace more consistent, added several semicolons, etc with the help of jshint.
Nov 21, 2012 at 5:33 review Suggested edits
S Nov 21, 2012 at 5:38
Oct 5, 2012 at 5:08 comment added Casey Rodarmor @xorinzor No, the .prototype property is cross-browser.
Jul 22, 2012 at 13:47 comment added xorinzor Just wondering, would I need to include the prototypeJS lib into my project to use your suggestion or is this cross-browser included?
Oct 17, 2010 at 20:54 vote accept MacMac
Oct 17, 2010 at 20:23 history edited kennebec CC BY-SA 2.5
indexOf
Oct 17, 2010 at 20:16 history answered kennebec CC BY-SA 2.5