Monday, December 13, 2010

Scars On Stomach From Ingrown

disadvantages of functional inheritance in JavaScript

In my last blog post I introduced the functional inheritance in JavaScript. This functional inheritance has some disadvantages.

Each time a new sub-object will be generated, creates two new functions: once for the sub-object and once for the super-object. These objects are not light weight, because the inner super object is referenced as a closure in the outer sub-object. Using many closures may lead to memory leaks.

types can not be tested by the instanceof operator. Both the super-object "Square" and the sub-object "box" are simple functions. A box object is an instance of the square object. This is because no constructor is used.

print (MyBox instanceof square) / / false


types can not expand also. The super-object is held in a closure. It is not visible from the outside.

The syntax for creating a new object instance does not conform to the conventions of JavaScript.

MyBox var = box (5);


New object instances are created in JavaScript with the new operator to a constructor. Since the design pattern known to the functional Konstrutkorfunktionen no inheritance, the new operator can not be used. This can lead to errors in the Using the code execution.

new Box (5), / / reference error: Box is not defined

Functional inheritance should therefore be applied only in exceptional cases where a functional paradigm seems most reasonable.

This is a cross-post from Ajaxer.de.

0 comments:

Post a Comment