1

I have this piece of code in node.js:

'use strict';
var Controller  = require('../api/Controller');
var _Promise    = require('bluebird');

var BookingController = function(model) {
    Controller.call(this);
    this.model = _Promise.promisifyAll(model);
};

BookingController.prototype = Object.create(Controller.prototype);
BookingController.prototype.constructor = BookingController;

module.exports = function(model) {
    return new BookingController(model);
};

I'm using the Object.create to inherit all the methods from my SuperController and I'm using the Controller.call to inherit this.model from BookingController in my SuperController, everything ok, basic stuff.

What I want know, if it is possible achieve the same effect with normal objects, I will explain:

var SuperObject = {
 printName: function() {
  console.log(this.name);
 }
};

var NormalObject = {
  this.name = 'Amanda'
};

console.log(NormalObject.printName) // will print Amanda.

This is possible?

That's exactly what I'm doing with prototype inheritance, but I want do with normal objects.

Other question if you guys can help..

About velocity and performance, what is better use, object inheritance (if possible) or prototype inheritance?

Thanks!

Hannes Johansson
  • 1,794
  • 2
  • 15
  • 28
  • 1
    All "normal objects" use prototype inheritance. What do you mean by "object inheritance"? – melpomene Aug 08 '15 at 18:30
  • If by "normal object" you mean something not created with `Object.create` but rather with the object literal notation, then [this S.O. question and its accepted answer](http://stackoverflow.com/q/7015693/831878) should help you. – Ray Toal Aug 08 '15 at 18:32
  • I guess you are looking for `var NormalObject = {__proto__: SuperObject, name: 'Amanda'};`. Not sure how many browsers support it already, Chrome does. – Felix Kling Aug 08 '15 at 18:56

2 Answers2

0

Yes - prototypal inheritance is all about objects! These Controller and BookingController constructor functions with their .prototype properties only obscure what is going on behind the scenes when you call it with the new keyword.

However you've already peeked behind the curtains, using Object.create for inheritance correctly (basic stuff, as you say :-D). You just didn't seem to realise that you are indeed using it here with "normal objects" already: the BookingController.prototype object (from which then the instances will inherit) is inheriting from the Controller.prototype object.

And you can do exactly the same thing with Amanda:

var superObject = {
 printName: function() {
  console.log(this.name);
 }
};

var normalObject = Object.create(superObject);
normalObject.name = 'Amanda';

console.log(normalObject.printName()) // Amanda

About velocity and performance, what is better use?

Before asking for performance, you should be using what is more appropriate. If you create only single objects, it's typically Object.create, if you create multiple instances and also need to initialise them, a constructor function and new are the appropriate tools.

Both are using the same prototype inheritance after all. However, especially for many instances, JS engines are better at optimising constructors. While Gecko's Object.create is super fast, in V8 Object.create is considerably slower than a new call.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I assume you're familar with the basics of prototype inheritance and how `Object.create` works. Please comment if I should detail those. – Bergi Aug 08 '15 at 19:41
-1

JavaScript (ES5) doesn't have "inheritance" built into the language. Prototype lookups are called "behavior delegation". Maybe you know that conceptually, but it's important to use the correct terms.

If you want some awesome coverage of the concept, check out this chapter from Kyle Simpson's open source book, You Don't Know JavaScript.

Object.create is the best-practice way to do what you are talking about.

ECMAScript 6 Classes (MDN) have an extends keyword (MDN) that will look more like traditional "inheritance" syntax.

rojobuffalo
  • 3,113
  • 3
  • 15
  • 16
  • "*ECMAScript 6 Classes have an extends keyword that will work more like traditional "inheritance""* It's more or less just syntactic sugar for `Foo.prototype = Object.create(Bar.prototype);`. – Felix Kling Aug 08 '15 at 18:54
  • @Felix: it is just syntatic sugar. You can read this great article by Zakas. http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/ – Aravind Aug 08 '15 at 19:09
  • @FelixKling It has 'super' keyword that makes it out of syntax sugar only. – zb' Aug 08 '15 at 19:17