How about this code, it uses Object.extend() instead of __proto__ for
creating inheritance chain as described here:
http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/

Looks pretty clean, though I'm not sure whether iterating over object
properties won't slow down the code. Could this technique be used
safely as replacement for constructor functions with prototypes?

Object.extend = function(prototype, object) {
  var newObject = Object.create(prototype);
  for (var prop in object) {
    if (object.hasOwnProperty(prop)) {
      newObject[prop] = object[prop];
    }
  }
  return newObject;
};

var Human = {
  // Init
  init: function() {
    console.log('initializing Human object');
  },

  // Properties and methods
  legs: 2,
  hands: 2,
  eyes: 2,
  isMammal: true,
  sayHello: function() {
    alert('hello');
  }
}

//
// Man object, inherits from Human
//
var Man = Object.extend(Human, {
  // Init
  init: function() {
    Human.init.call(this);
    console.log('initilizing Man object');
    return this;
  },

  // Properties and methods
  gender: 'male',
});

//
// Man instance
//
var john = Object.create(Man).init();

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to