Object inheritence

  • rexxy
  • Beginner
  • Beginner
  • rexxy
  • Posts: 41

Post 3+ Months Ago

Hello,
I'm a beginner with Javascript and I would like to accomplish the following:
Two have two or more objects inheriting one from another. I do not need classes to instantiate, just objects, and i can't figgure out how this works:

Example:

Code: [ Select ]
// User object
User = {
 id: '2',
 firstname: 'John'
}

// User information object
User.Information = {
 getFirstname : function() {
  return this.firstname;
 }
}
  1. // User object
  2. User = {
  3.  id: '2',
  4.  firstname: 'John'
  5. }
  6. // User information object
  7. User.Information = {
  8.  getFirstname : function() {
  9.   return this.firstname;
  10.  }
  11. }


Now, from what I can tell, the user object does indeed receive the Information object as a property, but if I call:
Code: [ Select ]
alert(User.Information.getFirstname());


Instead of returning the 'John' firstname, it returns undefined. Anyone know a way to accomplish this, I would be very gratefull. Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You can not use "this" without creating an instance with the "new" keyword.

If you're going to use an object like a namespace, you need to refer to the object by name within your method.

JAVASCRIPT Code: [ Select ]
// User information object
User.Information = {
 getFirstname : function() {
  return User.firstname;
 }
}
  1. // User information object
  2. User.Information = {
  3.  getFirstname : function() {
  4.   return User.firstname;
  5.  }
  6. }
  • rexxy
  • Beginner
  • Beginner
  • rexxy
  • Posts: 41

Post 3+ Months Ago

Yeah i found out the hard way but i think this is not what it would be called best practice coding.
What i did instead, cause I'm using the ExtJs framework, is use their inheritance type of doing things.
Code: [ Select ]
User = Ext.extend(Ext.emptyFn, {
    firstname: 'John'
});

User.Information = Ext.extend(User, {
   getFirstname: function() {
    return this.firstname;
   }
});


User.Information = new User.Information();

alert(User.Information.getFirstname());
  1. User = Ext.extend(Ext.emptyFn, {
  2.     firstname: 'John'
  3. });
  4. User.Information = Ext.extend(User, {
  5.    getFirstname: function() {
  6.     return this.firstname;
  7.    }
  8. });
  9. User.Information = new User.Information();
  10. alert(User.Information.getFirstname());


This way it works, I just posted example for anyone else who has the same problem.

*** Topic Closed ***
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Unless you're instantiating any of those more than once, which by the looks of your example you aren't, you're doing a bunch of extra work to setup something that will perform worse.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 99 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.