HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Instantiation/Inheritance Helper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
inheritanceinstantiationhelper

Problem

For the sake of learning JavaScript better and getting used to the Google Chrome Extension API, I'm currently writing a little Chrome extension.

To keep things simple and being able to make use of prototypal inheritance, I decided to write a little instantiation/inheritance helper before I get started.

But as I am still deep in the process of learning JavaScript, I would greatly appreciate if anyone could take a quick look at the code and clarify some points:

  • Are there any pitfalls I could run into with some of the approaches?



  • Is the code, as-is, or parts of it considered bad practice some weird constructions or similar?



  • Did I miss some important aspects regarding inheritance itself?



```
/ Inheritance Helper/
var base = (function baseConstructor() {
'use strict';
var obj = {
create: function instantiation() {
if (this === base) {
throw new SyntaxError("You can't create instances of base");
} else if (!this.hasOwnProperty("initclosure")) {
throw new SyntaxError("Cannot create instances without an constructor");
} else if (this.singleton && this.instances.length !== 0) {
throw new SyntaxError("You can't create more than one Instance of a Singleton Class");
} else {
var instance = Object.create(this.pub);
this.init.apply(instance, arguments);
this.instances.push(instance);
return instance;
}
},
inherit: function inheritation(specsOpt) {
specsOpt = specsOpt || {};
applyDefaults(specsOpt, {
singleton: false,
anonymous: false
});
var sub = Object.create(this);
sub.pub = Object.create(this.pub);
sub.instances = [];
sub.anonymous = specsOpt.anonymous;
sub.sup = this;

if (specsOpt.singleton) {
sub.singleton

Solution

Did I miss some important aspects regarding inheritance itself

JavaScript uses prototypal inheritance, which is already quite simple. Here's an example:

function Animal() {
this.name = 'Animal';
}

Animal.prototype.speak = function() {
console.log('My name is ' + this.name);
};

var animal = new Animal();
animal.speak(); // My name is Animal

function Cat() {
this.name = 'Cat';
}

Cat.prototype = new Animal();

var cat = new Cat();
cat.speak(); // My name is Cat



Are there any Pitfalls I could run into with some of the appraches

This whole approach feels quite complicated, and I'm not sure that I understand the benefits of using this over prototypal inheritance. My advice would be to stick with existing patterns which are widely used and understood (e.g. this Singleton pattern).


Is the code, as is, or parts of it considered bad practice some weird constructions or similar

Aside from feeling a little over-engineered, the only other feedback I have is that you don't need to name functions that are assigned to a variable. You could change

var obj = {
create: function instantiation() {
}
};


To

var obj = {
create: function() {
}
};

Context

StackExchange Code Review Q#21469, answer score: 2

Revisions (0)

No revisions yet.