I've been learning Js recently from "JavaScript the Good Parts", and according to my understanding
Object.propertyName
yields same result as Object["propertyName"]
(Please correct me if I'm not right and describe the difference of the two).
I'm trying to augment the Function.prototype to make a method available to all functions as below:
Function.prototype.method = function (name, func) {
this.prototype[name]= func;
};
And it's working fine.However, when I'm replacing the this.prototype[name]
with this.prototype.name
, it'll fail functioning as expected!
This is how I'm testing it:
Number.method("myRoundFunction", function () {
return Math[this < 0 ? "ceil" : "floor"](this);
});
console.log((-10 / 3).myRoundFunction());
This shows the expected value (-3) when using this.prototype[name]
, but
(-3.3333333333333335).myRoundFunction is not a function
on changing it to this.prototype.name
Could someone possibly clarify why this is happening?
Thanks in advance for any help.