Jitender
Jitender

Reputation: 7971

Define object method in javascript

I want to make my custom object in javascript. I have made a method in my object to make value uppercase but it is not working. fiddle

function mystring (name,uppercase){
this.name= name;
this.uppercase= function (){
return this.toUpperCase();
};

}
var jj= new mystring('mycompany');
 jj=jj.uppercase();
console.log(jj)

Upvotes: 0

Views: 181

Answers (2)

David Barker
David Barker

Reputation: 14620

You are trying to convert to the entire object to upper case, if you check the console it tells you that the element has no method toUpperCase. Instead convert the string, not the object.

return this.name.toUpperCase();

Upvotes: 1

hamon
hamon

Reputation: 1016

You need to do

function mystring (name,uppercase){
    this.name= name;
    this.uppercase= function (){
        return this.name.toUpperCase();
    };

}
var jj= new mystring('mycompany');
jj=jj.uppercase();
console.log(jj);

You forgot the this.name in the this.uppercase function

Upvotes: 1

Related Questions