0

There are loads of functions defined in JavaScript built-in object. For example String has slice(),split() and Array has join() and they can be overridden (though it is a bad practice) by simply using

Array.prototype.join = 'test', Then all the array will lose the join() method

However, as a developer of open source project, in what way should we view this potential changes to the prototype? It will become a problem because it is very likely we will use these functions more or less to achieve some functionalities.

In jQuery, it will keep a copy of some of those methods:

var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty;

But only some of the methods are kept safe, if you read through the source code of jQuery you will notice some String methods are called directly on the String object.

Of course we can always keep a copy of all the methods of built-in JavaScript object but it is so messy. Indeed, it also becomes a problem of execution timing. If the program is executed before the prototype is changed, then it's fine. But it will fail if it is vice versa. So I am more interested to know when jQuery or other libraries they decide to keep a copy of some of the methods, what are their philosophy behind the decision?

Thank you.

Calvin Lau
  • 547
  • 1
  • 4
  • 11
  • Possible duplicate of [Why is extending native objects a bad practice?](http://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice) – CBroe May 16 '16 at 09:48
  • Why do you need to disturb the prototypes as such. there are several other ways too. isn't it better to have a check before setting like this. – Jai May 16 '16 at 09:51
  • I'd only overwrite the original prototypes if I wanted to make a joke to some programmers I was cooperating with. – Nonemoticoner May 16 '16 at 09:53
  • Let me clarify a bit, my initial idea of the question is not to discuss whether polluting the prototype is good or bad. But I am more interested in as as a developer, maybe the software I developed will be used by many other parties and I am unsure how others develop their program. So maybe (though I believe it is quite rare) they will change the prototype. So how should I handle it? As the jQuery case they will keep some of the methods but not all, why do they do so? – Calvin Lau May 16 '16 at 09:57
  • I believe jQuery keeps instances of the methods more for efficiency purposes than anything. – Jonathan Gray May 16 '16 at 09:59

1 Answers1

2

As a developer of an open source project, the safest and best thing to do is to pretend that the builtin prototypes are frozen. The practice of extending or modifying them is responsible for far more problems than solutions.

If you need a custom join function, just create it so that it takes a string as the first argument.

function join(string, separator) {
  // ... 
}

This keeps the prototypes clean and stops you from having to worry about references to overwritten methods.

What you're seeing there in the jQuery example is just aliasing. The methods are being assigned to variables to make them easier to use.

There's no way to code defensively around this, because by the time jQuery loads another script could have already modified the builtin prototypes and the original references would be lost.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • That make sense. Just to add some additional information, It may be confusing at first (at least it confused me) that calling `a.concat(b,c)` is easier than `concat.call(a, b,c)` so why bother? But I have studied the source code in jQuery, they usually use `concat` and other methods when they will have to call `concat.apply(a, arrayOfValues)`. Without the alias it will become a long statement of `Arrray.prototype.concat.apply(a, arrrayOfValues)` – Calvin Lau May 16 '16 at 10:17