Question is quite old, but I created some possible solution how to create abstract "class" and block creation of object that type.
//our Abstract class_x000D_
var Animal=function(){_x000D_
_x000D_
this.name="Animal";_x000D_
this.fullname=this.name;_x000D_
_x000D_
//check if we have abstract paramater in prototype_x000D_
if (Object.getPrototypeOf(this).hasOwnProperty("abstract")){_x000D_
_x000D_
throw new Error("Can't instantiate abstract class!");_x000D_
_x000D_
_x000D_
}_x000D_
_x000D_
_x000D_
};_x000D_
_x000D_
//very important - Animal prototype has property abstract_x000D_
Animal.prototype.abstract=true;_x000D_
_x000D_
Animal.prototype.hello=function(){_x000D_
_x000D_
console.log("Hello from "+this.name);_x000D_
};_x000D_
_x000D_
Animal.prototype.fullHello=function(){_x000D_
_x000D_
console.log("Hello from "+this.fullname);_x000D_
};_x000D_
_x000D_
//first inheritans_x000D_
var Cat=function(){_x000D_
_x000D_
Animal.call(this);//run constructor of animal_x000D_
_x000D_
this.name="Cat";_x000D_
_x000D_
this.fullname=this.fullname+" - "+this.name;_x000D_
_x000D_
};_x000D_
_x000D_
Cat.prototype=Object.create(Animal.prototype);_x000D_
_x000D_
//second inheritans_x000D_
var Tiger=function(){_x000D_
_x000D_
Cat.call(this);//run constructor of animal_x000D_
_x000D_
this.name="Tiger";_x000D_
_x000D_
this.fullname=this.fullname+" - "+this.name;_x000D_
_x000D_
};_x000D_
_x000D_
Tiger.prototype=Object.create(Cat.prototype);_x000D_
_x000D_
//cat can be used_x000D_
console.log("WE CREATE CAT:");_x000D_
var cat=new Cat();_x000D_
cat.hello();_x000D_
cat.fullHello();_x000D_
_x000D_
//tiger can be used_x000D_
_x000D_
console.log("WE CREATE TIGER:");_x000D_
var tiger=new Tiger();_x000D_
tiger.hello();_x000D_
tiger.fullHello();_x000D_
_x000D_
_x000D_
console.log("WE CREATE ANIMAL ( IT IS ABSTRACT ):");_x000D_
//animal is abstract, cannot be used - see error in console_x000D_
var animal=new Animal();_x000D_
animal=animal.fullHello();
_x000D_
As You can see last object give us error, it is because Animal in prototype has property abstract
. To be sure it is Animal not something which has Animal.prototype
in prototype chain I do:
Object.getPrototypeOf(this).hasOwnProperty("abstract")
So I check that my closest prototype object has abstract
property, only object created directly from Animal
prototype will have this condition on true. Function hasOwnProperty
checks only properties of current object not his prototypes, so this gives us 100% sure that property is declared here not in prototype chain.
Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain. More about it:
In my proposition we not have to change constructor
every time after Object.create
like it is in current best answer by @Jordão.
Solution also enables to create many abstract classes in hierarchy, we need only to create abstract
property in prototype.