www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Object of objects

  1. #1
    Join Date
    Oct 2011
    Posts
    19

    resolved [RESOLVED] Object of objects

    Hi all,

    I have a html form and 've many text boxes, dropdowns, and radios. I have given ids for each. Now i wanna populate the data from that using a js function.

    Code:
    function user(id,firstname,lastname){
    this.id=id;
    this.firstname=firstname;
    this.lastname=lastname;
    }
    //hope i can include othr things when i need in future.

    This way i wanna go. And onload i creates a function,

    Code:
    function getdatas()
    now i want to get the datas entered in the form like this. After entering all the data in form when i clk a buttn i wanna populate those values using tis function. I just know that its an object of objects. Can ny1 explain n guide me How to do it? I m just a learner n please make it simple replys.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Can you translate that in English, please? As for me, I have not understood too much of your phrases.

    Anyway, you should use that function as a constructor, by using the token new whenever use it:
    Code:
    function user(id,firstname,lastname){
    this.id=id;
    this.firstname=firstname;
    this.lastname=lastname;
    }
    var John=new user(123,'John','Doe');
    var Mary=new user(124,'Mary','Smith');

  3. #3
    Join Date
    Oct 2011
    Posts
    19
    Quote Originally Posted by Kor View Post
    Can you translate that in English, please? As for me, I have not understood too much of your phrases.

    Anyway, you should use that function as a constructor, by using the token new whenever use it:
    Code:
    function user(id,firstname,lastname){
    this.id=id;
    this.firstname=firstname;
    this.lastname=lastname;
    }
    var John=new user(123,'John','Doe');
    var Mary=new user(124,'Mary','Smith');

    Sorry for my poor english usage. K.. I jus did it.

    Code:
    function User(id, center, firstName, lastName, mobileNumber)
    {
    	this.id = id;	
    	this.center = center;
    	this.firstName = firstName;
    	this.lastName = lastName;
    	this.mobileNumber = mobileNumber;
    }

    Code:
    function populatedata()
    {
    	var UserDetails = new User( document.getElementById("CenterID"), document.getElementById('something'));
    	alert(UserDetails.id ;
    }
    This is what i meant bro. This is working now. Im getting alert boxes aftr sumbitting data. So this is fine right? You told some constructor problem. Wats it? Is ther any error in this code.?

    Now my other doubt is, I want to include 2 more fields with the function user. ie I want to add "address", " somotherthing" in the function 'user()'. How to add it? ( Without editing the user(id, center, firstName, lastName, mobileNumber) part. Can i add it like this.somthing..or nysuch ways are available??

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by johnrock View Post

    Now my other doubt is, I want to include 2 more fields with the function user. ie I want to add "address", " somotherthing" in the function 'user()'. How to add it?
    In fact you don't need to add it. Once you have created the Object, you may add custom properties, at will:
    Code:
    var UserDetails = new User( document.getElementById("CenterID"), document.getElementById('something'));
    userDetails.address='5, Mark Avenue';
    You may do the same thing in a more sophisticated way: using the prototype of the object:
    Code:
    function User(id, center, firstName, lastName, mobileNumber)
    {
    	this.id = id;	
    	this.center = center;
    	this.firstName = firstName;
    	this.lastName = lastName;
    	this.mobileNumber = mobileNumber;
    }
    User.prototype.addProperty=function(property,value){this[property]=value}
    var UserDetails=new User(123,'boo','Joe','Doe',898989);
    UserDetails.addProperty('address','Mark Avenue 5');
    alert(UserDetails.address)
    To change the constructor itself could be a much too intricate job and I don't think it is worth.

  5. #5
    Join Date
    Dec 2005
    Posts
    2,984
    I find that interesting, Kor, but I don't understand it. I don't mean to hijack the thread, but the point you illustrated intrigues me.

    Why is it that this:

    Code:
    function User() {
        /* ... properties */
    }
    
    User.prototype.addProperty = function(prop,value) {
      this[prop] = value;
    }
    
    var you = new User();
    you.addProperty('isnot','me');
    is 'better' than this:

    Code:
    var you = new User();
    you.isnot = 'me';
    I guess I can see a 'best-practices' argument for it, kind of, but is it more or less efficient?

    (Sorry, I may have edited this a few times while you were responding).
    Last edited by aj_nsc; 11-15-2011 at 02:41 PM.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by aj_nsc View Post
    I find that interesting, Kor, but I don't understand it. I don't mean to hijack the thread, but the point you illustrated intrigues me.

    Why is it that this:

    Code:
    function User() {
        /* ... properties */
    }
    
    User.prototype.addProperty = function(prop,value) {
      this[prop] = value;
    }
    
    var you = new User();
    you.addProperty('isnot','me');
    is 'better' than this:

    Code:
    var you = new User();
    you.isnot = 'me';
    I guess I can see a 'best-practices' argument for it, kind of, but is it more or less efficient?

    (Sorry, I may have edited this a few times while you were responding).
    I did not say it is better. I said just that is another, more sophisticated way Maybe I wanted to make the OP curious what is about that mysterious prototype property/method
    Last edited by Kor; 11-16-2011 at 03:31 AM.

  7. #7
    Join Date
    Oct 2011
    Posts
    19
    I did not say it is better. I said just that is another, more sophisticated way Maybe I wanted to make the OP curious what is about that mysterious prototype property/method
    Thanks bro.. I got what i was looking for from your info.. thanks alot..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles