1

I learned javascript inheritance via the old school constructor function / new keyword method, like this:

function People(name, age){
  this.name = name;
  this.age = age;
  this.yearsUntilRetire = yearsLeft;
}

function yearsLeft(){
  var numYears = 65 - this.age;
  return numYears;
}
lady = new People("Lady Gaga", 28);
mickey = new People("Mickey Mouse", 99);

Now, when using this method I can create new instances of the constructor function "People" and pass in data that will fill out the values of the function (in this case, that's "name" and "age"). I'm just now learning prototypal inheritance and I get that objects inherit from other objects directly and that javascript is a classless language, for example, I know how to do the following, which I learned from a more updated tutorial:

var person = {
  kind: 'person'
}

var zack = Object.create(person);

zack.kind = 'zack'

console.log(zack.kind); //=> 'zack'
// zack now has a 'kind' property

console.log(person.kind); //=> 'person'
// person has not being modified

But my question is that there doesn't appear to be any method of "passing" arguments into the constructor function to make the values of the new object more dynamic and to fill in the blueprint like is the case with constructor functions ("name" and "age") and instead it appears as though in cases where I wanted to just change the values I would just OVERWRITE the original data for some new object, like above:

zack.kind = 'zack'

At this part of the code it seems that I just stamped directly over zack.kind, which would have ended up having the value of 'person'.

Is the manipulation of values in prototypal inheritance normally done like this? Where you just stamp over the data? Or is there some method of keeping the blueprint form of a value and only passing in some select pieces of data like with constructor functions arguments? It seems strange that I don't pass data into the function but just stamp over the other objects data that you want to change (I know it doesn't change the original objects values, but it does feel a little bit loose and strange). It seems odd to me since I've been using constructor functions for a few months and want to make sure I am working the correct way. Sorry if this is a newbie question.

Allan Socks
  • 261
  • 2
  • 10
  • 1
    I suggest you read [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) – Mosho Jun 17 '14 at 20:40
  • Using constructor functions is inheritance through prototype. You must have been reading Crockford. He shows some faulty code using constructor functions and then blames his problems on JavaScript instead of his faulty code. It's important to understand how prototype in JavaScript works before you dive into different patterns to create instances. Maybe the following answer can help you with that: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Jun 18 '14 at 00:25

2 Answers2

3

There is a common convention to have a factory function that is used instead of a constructor. This method encapsulates object creation and is allowed to have parameters.

In this simple example it could be:

var person = {
  kind: 'person',
  create: function(kind) { 
     var p = Object.create(person);
     p.kind = kind;
     return p;
  }
}

var zack = person.create('zack');
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
1

If you want to add properties to the new object you are creating using Object.create you can use the second argument like this:

var zack = Object.create(person, { kind: {
      value: 42,
      writable: true,
      configurable: true }
});

But you can also just use a function and new, there is no difference in this case. You can see where using Object.create might be beneficial here. The only thing it really gives you, in my opinion, is being to define properties such as writable, enumerable, etc. as defined here.

Community
  • 1
  • 1
Mosho
  • 7,099
  • 3
  • 34
  • 51