On the one hand I can assign a new function to an object with
Object.defineProperty(...)
.
Then I tried to assign a function directly to the NodeList Prototype. But somehow it doesn't work properly. What am I doing wrong?
Object.defineProperty(
NodeList.prototype,
'lastElement', {
get: function() {
return this[this.length - 1].textContent;
}
}
);
NodeList.prototype.firstElement = () => {
return this[0].textContent;
}
const h = document.querySelectorAll('span');
console.log('#last:', h.lastElement);
console.log('#first:', h.firstElement);
<div>
<span>1</span>
<span>2</span>
</div>