Learning JavaScript OOP ... And being all day on that field but...
I cannot understand / find how to create custom JS Method just like
.toString()
, .toUpperCase()
...and other methods, that for e.g. if you have an accessed object like
customer.name // John
and you use:
customer.name.toLowerCase();
will give you // john
That means that a function Method toLowerCase()
somehow uses the this
reference of the prefixed object as the operable argument for the return
operation.
How to do something like that? Attach a method that will do some stuff with some unknown previous object?
OK, just a stupid example, let's say we want to be able to create a .addLength()
Method that will simply allow us to do: customer.name.addLength() // John Name length = 4
function addLength(){
var that = this;
var name = that.toString();
return ( name +' Name length = '+ name.length );
}
ok, this is totally wrong I know, it was just to describe somehow.
It's not for a purpose, just to understand. (better useful examples are welcome) Thanks!