For executing array methods on NodeList objects(Array like) we need to make indirect call on Array.prototype every time.
Can we do something so that node_list.slice(0,1) is possible?
As NodeList inherits methods from Object.prototype, i implemented like below and works
perfectly node_list.slice(0,1)
Object.prototype.slice = function(a,b){ return Array.prototype.slice.call(this,a,b);}
Is there any disadvantage on implementing slice on Object.prototype?
Legacy way to call on Array.prototype:
node_list
[<li> test_list1 </li>, <li> test_list2 </li>]
Object.prototype.toString.call(node_list)
"[object NodeList]"
var content = Array.prototype.slice.call(node_list,0,1)
content
[<li> test_list1 </li>]
Object.prototype.toString.call(content)
"[object Array]"
It’s a bad idea to extend the Object’s prototype for this reason.
Even if you want to extend a built-in prototype, reduce the extension to the necessary parts:
Prototype methods which don’t show up in a
for(.. in ..)loop can be defined as follows:See also:
Object.definePropertyShortest way (without extending prototypes)