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)
asked Oct 12, 2013 at 12:26
Jitender
7,99932 gold badges117 silver badges218 bronze badges
2 Answers 2
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
Sign up to request clarification or add additional context in comments.
Comments
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();
answered Oct 12, 2013 at 12:30
David Barker
14.6k3 gold badges51 silver badges77 bronze badges
Comments
lang-js