0

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

2 Answers 2

1

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

answered Oct 12, 2013 at 12:29
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.