I don't quite understand how does the 'this' keyword in built-in functions like String, Number, etc point to the primitive value directly instead of the object like the 'this' keyword is supposed to. Is it just directly set by javascript engine?
I have this example:
String.prototype.repeatify = function(times) {
console.log(this); // Output - String {"hello"}
var str = "";
for(var i=0; i<times; i++) {
str += this; //how does 'this' gets set to 'hello' and not the object- String{ }??
}
return str;
}
var string = new String('hello');
console.log(string.repeatify(3)); //Output - prints hellohellohello
I was expecting the output in the chrome devtools to be [object Object][object Object][object Object]