Javascript using defineProperty

Better way to attach methods to objects (than using prototype) - to avoid issue with for ... in loops

by Prathap Beschi AA

JavaScript

Array.prototype.itsMe = function() {
	console.log('itsMe: ', this);
}
Object.defineProperty(Array.prototype, 'sameMe', {
	value: function() {
  	console.log('sameMe: ', this);
  }
});

var newArr = [1, 2];

// result will be same for these 2
newArr.itsMe();
newArr.sameMe();

// but, see an extra item if we loop, third one is itsMe
for (var i in newArr) {
	console.log(newArr[i]);
}