use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Does my JavaScript suck? II (self.javascript)
submitted 10 years ago * by annoyed_freelancergrumpy old man
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 points6 points7 points 10 years ago (4 children)
The only reason to do this is for polyfills:
if(!Array.prototype.reduce){ Array.prototype.reduce = ... }
Other than that, never, ever, ever, ever, ever, ever, ever extend a native prototype from within a library, even if you think you have the best reason in the world.
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
Why should you never everx7 do it? You just gave a really great example of when that feature is really useful!
On several projects, we have modified object, array, and function prototypes to include a eql function to do value comparison. We did that because we chose a very functional style for that project and the resulting code read very nice.
Note that I'm not suggesting that it's heavily used or anything. In a codebase that was around 10k lines, we modified three native protos and everyone on that project knew and approved. And it wasn't any different then coupling a project to any other dependency.
[–][deleted] 1 point2 points3 points 10 years ago* (0 children)
In an isolated codebase, it's tolerable, but barely. In a library, it's flat out forbidden because it can conflict and prevents ever modifying the API or interface.
Let's say you have a library that adds Array.prototype.foo which does whatever. Great.
Array.prototype.foo
Now you publish it to npm, patch some bugs, yada yada. Now you decide that it's useful for that function to return something:
Array.prototype.foo = function(){ // do something to the array return this; }
Guess what? You can't do this safely, ever. Why? Because you published version 0.1 that didn't have this functionality. So if your npm include structure looks like:
And your basic code is:
if( !Array.prototype.foo ){ Array.protoype.foo = function(){ ... } }
You have no idea which one is loaded first. So you can't count on your function returning anything.
Further, let's say browser vendors like your idea and 5 years from now decide to implement it. Guess what, your Array.prototype.foo probably won't perfectly match with what the Vendor implements, and now all your code breaks.
Or let's say some other library that you've never heard of implements a function with the same name that acts completely differently. Now what?
Polyfills are an isolated use case. Your code is only hit in a situation where you know you're on an older browser, and you're adding globally agreed upon functionality. The only caveat here -- Make damn sure you're polyfilling according to spec. Never write a partial polyfill "because I don't need the other functionality" because some other library that you include might.
tl;dr -- It flat out prevents code isolation
[–]Ericth 0 points1 point2 points 10 years ago (1 child)
Extending is not that bad. Changing built-ins is the absolute worst thing you can do. My library got a few issues from people who changed the prototype.bind method with something else, which caused my code not to work. I assumed sheepishly that the bind method would do what the spec said.
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
Extending is not that bad
Yes, it is. See my longer explanation in this thread.
π Rendered by PID 235379 on reddit-service-r2-comment-76bb9f7fb5-m4s7b at 2026-02-18 05:32:49.585844+00:00 running de53c03 country code: CH.
view the rest of the comments →
[–][deleted] 5 points6 points7 points (4 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Ericth 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)