Reputation: 13487
I am trying to extend a method to the String prototype.
I want this method to manipulate the string value but I am not sure how do access it.
this
the current object context seems to refer to the string object, but not the actual string value.
String.prototype.test = function() { console.log(this)}
function () { console.log(this) }
'lol'.test()
VM192:2 String {0: "l", 1: "o", 2: "l", length: 3,
has: function, contains: function,
escapeRegExp: function, camelize: function…
}
How do I access the string value instead of the string object?
Upvotes: 0
Views: 66
Reputation: 77482
You need call toString()
String.prototype.test = function() {
console.log(this.toString())
}
Upvotes: 5