0

I have a javascript class person, I have made Obj of it, works fine. But I need to access the function from person class.

When I tried to access I get ..not a function error.

Please help!

Important... I need to initialize the 'person' class, and call the method exactly same way of my code. How?? Help!

Here is my code:

 var person = {
   setBasic: function(data) {
     alert(data.name + ' ' + data.age);
   },
   this:getPerson = function() {
     alert('get me');
   },
   success: null
 };

 //lets instantiate.enter code here

 var perObj = new person.setBasic({ 'name': 'ikair', 'age': 33, });

 // works fine..

 //now I need to call a function of person class:

 perObj.getPerson();

 //not working :(... how to call function with prev code?
1
  • I think var person should be a function, not just an Object set. Then you add those functions to its prototype. Commented Jan 23, 2014 at 16:41

4 Answers 4

1

You need a class and to give its prototype functions:

/**
 * Person class
 * @param {String} name Name of person
 * @param {Number} age Age of person
 */
function person(name, age) {
   this.name = name || "";
   this.age = age || 0;
   this.success = null;
}
person.prototype = {
   /**
    * Set the parameters of this object
    * @param {type} data
    * @returns {undefined}
    */
   setBasic: function(data) {
      this.name = data.name;
      this.age = data.age;

      alert(data.name + ' ' + data.age);
      // Do you mean to use this?
      alert(this.name + ' ' + this.age);
   },
   /**
    * Alert out fields
    * @returns {undefined}
    */
   getPerson: function() {
      alert(this.name + ' ' + this.age);
      alert('get me');
   }
};

/**
 * Make a person and print him out
 * @returns {undefined}
 */
function main() {
   var perObj = new person("bob", 33);
   perObj.setBasic({
      name: "akira",
      age: 100
   });
   perObj.getPerson();
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks but i must call like: var perObj = new personsetBasic({ name: "akira", age: 100 }); then perObj.getPerson(); how? :(
What do you mean? The first line in main() gives you an object with the fields you pass in the constructor. You can stop after the first line. I just put in the second line to show you that you can still use a function to set an initialized person's values. The perObj.getPerson() shows an alert that prints the persons name and age.
Sorry ..but i may not able describe my problem. 1. I need to initialize a js class like this: var perObj = new person.setBasic({ 'name': 'ikair', 'age': 33, }); //person is class, 2. then i call method like this: perObj.getPerson(); // getPerson is a function. how?
person is not a class, it is an Object instance
@Ikhtiar I really am not sure how else to answer your question. You instantiate Objects from faux "Class"es that are declared through function and then have more functions tacked onto their prototype. To do the instantiating, you call new [whatever function describes the class]() Maybe read this: developer.mozilla.org/en-US/docs/Web/JavaScript/… It will help.
1
function person() {
   this.setBasic = function(data) {
     alert(data.name + ' ' + data.age);
   };
   this.getPerson = function() {
     alert('get me');
   };
}

var perObj = new person();
perObj.setBasic({ name: 'ikair', age: 33 });
perObj.getPerson();

Update:

var person = {
   setBasic: function(data) {
     alert(data.name + ' ' + data.age);

     this.getPerson = function() {
       alert('get me');
     };
   }
};

var perObj = new person.setBasic({ name: 'ikair', age: 33 });
perObj.getPersion();

4 Comments

thanks Igor...but as i said my requirement is to initiate Object exactly my code.. var perObj = new person.setBasic({ 'name': 'ikair', 'age': 33, }); then call perObj.getPerson(); ///here i m getting error
With your code you are assigning to perObj an object that is an instance of persion.setBasic function. It does not have a method called getPerson.
then how to change my code (person function/class) ..so it will have a method getPerson, and i can call by var perObj = new person.setBasic({ 'name': 'ikair', 'age': 33, }); perObj.getPerson();
You did not use the code that I provided. I updated the fiddle: jsfiddle.net/k3L3J/17
0

Try this

 var person = {
   setBasic: function(data) {
   alert(data.name + ' ' + data.age);
   },
   getPerson: function() {
    alert('get me');
   },
   success: null
   };
   person.setBasic({ 'name': 'ikair', 'age': 33, });
   person.getPerson();

1 Comment

Note: the value of this starts to get muddled in this setup
0

The other answers are all valid, but you could also restructure your code to be much simpler like this:

function Person(data){
    this.age = data.age;
    this.name = data.name;
};

Person.prototype.getData = function(){
    alert(this.name + " " + this.age);
};

var john = new Person({name: "John", age: 43});
john.getData();

And a JSFiddle to prove that it works here

2 Comments

thanks ..but must call like var perObj = new personsetBasic({ name: "akira", age: 100 }); then perObj.getPerson(); how? :(
The way you want for format your code doesn't make any sense. And even if I were to try, I don't think I could get that code to run at all.

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.