0

I can understand another_stooge.name return javascript because of the prototype and also understand the return new f() create a brand new object and didn't understand how prototype share details just by inside the "f.prototype = stooge" function.?

if (typeof Object.beget !== 'funcion') {
  Object.beget = function() {
    var f = function() {};
    f.prototype = stooge;
    return new f();
  }
}
var stooge = {}; // empty object;
var new_stooge = {}; // empty object;
new_stooge.title = "My first title";
new_stooge.color = "white";
var another_stooge = Object.beget(stooge);
stooge.name = 'Javscript';
console.log(another_stooge.name); // Return Javscript ?.
alexyorke
  • 4,224
  • 3
  • 34
  • 55
A.K
  • 25
  • 1
  • 2
  • cause Object.beget doesnt make sense? use Object.create – Jonas Wilms Apr 19 '17 at 17:36
  • You put the `stooge` object in the prototype chain, so looking up the "name" property checks the object and then checks its constructor's protoype. – Pointy Apr 19 '17 at 17:37
  • `funcion` is misspelled too – mplungjan Apr 19 '17 at 17:37
  • Maybe a duplicate of [How does the new operator work in JavaScript?](http://stackoverflow.com/questions/6750880/how-does-the-new-operator-work-in-javascript) Calling `new f()` creates a new object whose prototype is `f.prototype`. Therefore, setting `f.prototype` to `stooge` causes `new f()` to create an object whose prototype is `stooge`. – apsillers Apr 19 '17 at 17:52
  • Thanks for the help this is just for learning javascript objects and its prototype.... – A.K Apr 20 '17 at 06:07

0 Answers0