topical media & game development

talk show tell print

#javascript-code-22-prototype.js / js



  // Create a new User constructor
  function User( name, age ){
      this.name = name;
      this.age = age;
  }
  
  // Add a new function to the object prototype
  User.prototype.getName = function(){
      return this.name;
  };
  
  // And add another function to the prototype
  // Notice that the context is going to be within
  // the instantiated object
  User.prototype.getAge = function(){
      return this.age;
  };
  
  // Instantiate a new User object
  var user = new User( "Bob", 44 );
  
  // We can see that the two methods we attached are with the
  // object, with proper contexts
  alert( user.getName() == "Bob" );
  alert( user.getAge() == 44 );
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.