1

this is my class

function User(){
     this.nickname='nickname';
}
User.prototype.save=function(){
     dosomething();
};
User.prototype.add=function(){
     dosometing();
     call save();
};

i want to call the save() function in the add() function,but i don't know how to do.I tried

User.prototype.add=function(){
     save();
};

and User.prototype.add=function(){ User.prototype.save(); };

but both are wrong,and what should i do?

2
  • 5
    this.save(); You define your functions at prototype, so to call them at first, you should get instance of that type and then call function. this refer to the instance of User data type. Commented Dec 7, 2013 at 19:10
  • There are no classes in JavaScript, just so you know. JavaScript is a prototypal language. Commented Dec 7, 2013 at 19:21

2 Answers 2

4
function User() {
  this.nickname = 'nickname';
}
// ...
User.prototype.add = function() {
  this.save();
};

You were not defining your User constructor properly.

Also, instances of User (created like var myUser = new User(); ) can access their prototype's methods via this.methodNameHere();

Sign up to request clarification or add additional context in comments.

Comments

3

Ok.There are a few mistakes in your code.

Here we are using the classical model of inheritance.

Step 1.Create a constructor function. eg. function user(){...}

Step 2.Extend your prototype by adding methods.eg add,save etc

step 3.Create an instance to call methods.eg.MyInstance

  function User(){
         this.nickname='nickname';
    }

    User.prototype.dosomething=function(){
        //some code
    };

    User.prototype.save=function(){
         this.dosomething();
    };
    User.prototype.add=function(){
         this.dosometing();
         this.save();
    };

Now lets say I want to call a method add.This is how its done.

var MyInstance = new User();//create an instance.

MyInstance.add();//call the function.

Outside the scope of your question : The same thing could be done by Prototypal Inheritance as well.

     var UserPrototype={

     save:function(){..},
     add:function(){
         this.save();  
         }
      }

     var MyInstance = Object.Create(UserPrototype);
     MyInstance.add();

4 Comments

Only functions have prototype.
You wouldn't add prototype additions to an instance of an Object. Where would they be if the instance were to be destroyed? Edit: Retracted my downvote, thanks for fixing. This is the correct way to do it, and should be marked as the correct answer :P
I'm sorry for my error, I have corrected it, but i meet a similar problems: stackoverflow.com/questions/20445761/… this.is wrong in a callback.
so now its done...you should have posted the code with navigation..I think both the answer are coreect in context to your question...the link that you provided is also correct but this time you posted a question with all its detail..accept the answer you feel is worthy

Your Answer

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.