0

i have 2 objects, human and sam, with 2 given properties each

var human = {
  legs: 2,
  group: "mammal"
}
var sam = {
  age: 23,
  married: true
}

Can I attach the human object to the prototype of sam so that the properties of human are delegated to sam, and the sam object remains intact.

The desired result: console.log(sam.legs); //=> 2


One way i am aware of (but i know is "bad practice") is to use the __proto__ property:

var sam = { 
  age: 23,
  married: true,
  __proto__: human  // *NOT IDEAL*
} 

or

sam.__proto__ = human;  // *NOT IDEAL*


I am aware of Object.create() but i do not know how to implement it without erasing everything that is already stored in the sam variable

var human = {
  legs: 2,
  group: "mammal"
}
var sam = {
  age: 23,
  married: true
}

sam = Object.create(human);

console.log(sam.age);   //=> undefined


I know that I can just attach the human object to sam FIRST, and THEN assign properties to sam:

var human = {
  legs: 2,
  group: "mammal"
}

var sam = Object.create(human);

sam.age = 23;
married: true;

...but the whole point of my question is if i attach two objects, using the prototype property, that already have their own properties? Can i use Object.create() in a way that I'm not aware of?

I must have read everything on the first page of google results for object.create() and i never saw the right type of examples

EDIT: I don't want to just copy properties over because i am working on a game and i don't want the changes to be permanent. Let's say that sam has the human prototype, but at any minute in the game, his prototype could change to zombie or whatever.

Sam Eaton
  • 1,795
  • 1
  • 14
  • 19
  • This is about prototypes, but it doesn't seem to be about Prototype.js, the framework. Did you really mean to include that tag? – Barmar Oct 25 '14 at 02:11
  • The usual javascript way to do this would be to just copy the properties of `human` to `sam` so `sam` becomes a combined object with the union of properties. Perhaps you should describe what problem you have that makes the usual way of doing this not applicable. `.__proto__` is deprecated. There is an experimental `.setPrototypeOf()`, but it seems you need to explain why the usual copying of properties (which is 100% supported and safe) wouldn't work for you. – jfriend00 Oct 25 '14 at 02:50

1 Answers1

0

You could try constructor functions and turn an instance into something else.

Define both the Human and Zombie type that take a Human or Zombie as constructor parameter.

function Human(obj){
  obj = obj || {};
  this.legs = obj.legs === 0 ? 0 :
    obj.legs || 2;//defaults to 2
  this.name = obj.name || 'nameless';
  this.dead = false;
}
Human.prototype.saySomething = function(){
  console.log('Hello, I\'m ' + this.name);
};
//other functions of Human
function Zombie(obj){
  //Zombie is a Human but it's dead
  // re use Human constructor
  Human.call(this,obj);
  //re set the parameters that differ from Human
  this.dead = true;
  //add Zombie specific parameters if needed
}
//inherit prototype from Human
Zombie.prototype = Object.create(Human.prototype);
//repair constructor
Zombie.prototype.constructor=Zombie;
//override saySomething
Zombie.prototype.saySomething = function(){
  console.log('Huarrrghhh');
}

var ben = new Human({name:'ben'});
ben.saySomething();
console.log(ben);
// now ben becomes a Zombie
ben = new Zombie(ben);
ben.saySomething();
console.log(ben);

You could try different factory functions like so:

Human.toZombie = function(human){
  return new Zombie(human);
}
var ben = new Human();
ben = Human.toZombie(ben);

More on constructor functions and prototype: https://stackoverflow.com/a/16063711/1641941

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160