Basic Object extending


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function BaseClass(extended_props){
  2. this.extend = function(props){
  3. for(var i in props){
  4. this[i] = props[i]
  5. }
  6. }
  7.  
  8. if(extended_props){
  9. this.extend(extended_props);
  10. }
  11.  
  12. if(this.init){
  13. this.init();
  14. }
  15. }
  16.  
  17. var Foo = new BaseClass({
  18. name: "Jamie Allison",
  19. init: function(){
  20. alert("Called from Foo.init()");
  21. }
  22. });
  23.  
  24. Foo.extend({
  25. age: 32
  26. });
  27.  
  28. alert("Age is: " + Foo.age);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.