Wednesday, August 27, 2014

Angular.js: Using the baked-in ‘extend’ function

Inheritance in JavaScript remains a complex beast. Even when using Angular.js, you sooner or later will need some inheritance hierarchy. Your best friend to implement an inheritance hierarchy in Angular.js is the ‘extend’ functionExtend automatically applies the properties and functions from the base ‘class’ to the child ‘class’. Important to notice is that you’ll use ‘extend’ on the object prototype; this will ensure that the properties and functions are part of any instance that is created. Angular will also bind the properties and functions so this refers to the instance itself.

Extend can be called multiple times and you can pass a collection of objects if you want to. This enables your components to inherit from multiple ‘base classes’.

No comments: