London
London

Reputation: 15274

Implementing prototype method

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?

Upvotes: 6

Views: 144

Answers (4)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

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.

Upvotes: 3

6502
6502

Reputation: 114481

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.

Upvotes: 1

gotofritz
gotofritz

Reputation: 3381

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 

Upvotes: 0

Alex
Alex

Reputation: 35409

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/

Upvotes: 2

Related Questions