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?

Arnold
  • 210
  • 1
  • 6
  • 13
  • 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. – Givi Dec 07 '13 at 19:10
  • There are no classes in JavaScript, just so you know. JavaScript is a prototypal language. – Jackson Dec 07 '13 at 19:21

2 Answers2

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();

Jackson
  • 9,188
  • 6
  • 52
  • 77
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();
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • 1
    Only functions have prototype. – Givi Dec 07 '13 at 19:15
  • 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 – Chris Cirefice Dec 07 '13 at 19:19
  • I'm sorry for my error, I have corrected it, but i meet a similar problems: http://stackoverflow.com/questions/20445761/how-can-i-call-a-javascript-function-in-a-callback-of-another-function this.is wrong in a callback. – Arnold Dec 07 '13 at 19:48
  • 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 – HIRA THAKUR Dec 07 '13 at 19:56