6

If I implemented a method x on a String like :

String.prototype.x = function (a) {...}

And then the new version of javascript actually implements the x method, but on the another way, either returning something different than my implementation or function with more/less arguments than my implementation. Will this break my implementation and override it?

asked Nov 13, 2013 at 22:20

4 Answers 4

3

You'll overwrite the default implementation.

Any code that uses it will use yours instead.

There was a proposal for scoped extension methods and it was rejected because it was too expensive computationally to implement in JS engines. There is talk about a new proposal (protocols) to address the issue. ES6 symbols will also give you a way around that (but with ugly syntax).

However, that's not the punch - here's a fun fact no one is going to tell you.

No one is ever going to implement a method called x on String.prototype

You can implement it and get away with it. Seriously, prollyfilling and polyfilling is a viable, expressive and interesting solution to many use cases. If you're not writing a library I think it's acceptable.

answered Nov 13, 2013 at 22:37
Sign up to request clarification or add additional context in comments.

Comments

2

No, you'll be overriding the default implementation of said function, from the point at which you've declared/defined it. The "new" implementation will function in its native behavior, until your implementation's defined.

var foo = 'some arbitrary string';
console.log(foo.indexOf('s')); // logs [0]
String.prototype.indexOf = function(foo, bar) { return 'foo'; };
console.log(foo.indexOf()); // logs [foo]

Illustration: http://jsfiddle.net/Z4Fq9/

answered Nov 13, 2013 at 22:23

3 Comments

thank you for your answer but can you elaborate on this a bit more ` from the point at which you've declared/defined it`
@London as soon as you write String.prototype.x = [something]
"The new version of JavaScript" will have the method already defined before your code, that redefines it, is executed ...
1

Your code will be overriding the default implementation.

However if the interface of your method is not compatible with the standard one the libraries you may use could depend on the standard behavior so the program as a whole could break anyway with newer versions of the libraries.

In general is a bad idea doing something that could break if others do the same: what if another library thinks it's a good idea to add a method x to the standard string object prototype? Trying to avoid conflicts is a must for libraries but it's also good for applications (and if an application is written nicely then a lot of its code is probably quite similar to a library, and may evolve in a library later).

This kind of "patching" makes sense only to provide the a standard method for broken or old javascript implementations where that method is absent. Patching standard prototypes just because you can is a bad idea and will make your code a bad neighbor with which is difficult to share a page.

answered Nov 13, 2013 at 22:40

Comments

0

If the implementation of x is from a new version of Javascript, it's part of the core, therefore when you write String.prototype.x... it will be already there, and you will overwrite it.

Best practice in this kind of things is to write

if( !String.prototype.x ){
 String.prototype.x = function ...
 //your 
answered Nov 13, 2013 at 22:34

1 Comment

That's risky, it's useful for pollyfilling in retrospect to get native implementations when available. If you do that to a new method and you get different behavior in different browsers that might be really risky.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.