chopper draw lion4
chopper draw lion4

Reputation: 13487

JavaScript: How can I access the string value of a String object?

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

Answers (2)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You need call toString()

String.prototype.test = function() { 
    console.log(this.toString())
}

Upvotes: 5

antyrat
antyrat

Reputation: 27765

Just call toString method:

console.log( this.toString() )

Upvotes: 3

Related Questions