Is prototyping getElementById chaining appropriate?

It doesn’t make much sense for getElementById because you can only have one id in the page.

http://prototypejs.org/ famously extended the object prototypes like Element, it works but is generally considered bad practice these days.

Polyfills use them frequently though to fill in the gaps in older browsers. e.g.

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

This lets you use " asdf ".trim() in browsers that don’t have that function on strings.

2 Likes