Javascript Prototype Quirk – Can Anyone Explain This?-Collection of common programming errors

The problem here is that you are returning a property named prototype with the value parent, which is an object, so you are returning the pair prototype = {name = 'keith'} and when you call new obj, you are adding to the prototype of a a new property named prototype.

You just need a bit change, I need this is what you are trying to do. This will work, just be carefull with overload properties.

var obj = function(parent){
   for(var propt in parent){
      this[propt] = parent[propt];
   }
}

var me = { name: 'keith' };
var a = new obj(me);
console.log(a);
// => Object { name="keith"}
console.log(a.name);
// => "keith"

Edit: If you are looking for inheritance using prototype, you should read this or try TypeScript, a new javascript typed and OO library

Originally posted 2013-11-09 23:29:56.