Charu
Charu

Reputation: 3

AngularJS Inheritance Patterns without using scope?

I am new to angular js , I am learning it through various great links and of them I came across is inheritance which shows how to get inheritance in angular js but my doubt is the author says "Since AngularJS does not provide any built-in features for using inheritance, in this blog post I’ll describe how the general JavaScript inheritance patterns can be applied to AngularJS components." but we do have scope for inheritance in angular then what does he mean by the first statement ? Am i missing something to understand ? Kindly explain.

Upvotes: 0

Views: 130

Answers (4)

acalfo
acalfo

Reputation: 61

Another design paradigm commonly used in Angular involving inheritance lies in creating a service. One can create a service, lets call it BaseService, like so..

this.publicVar = '..'

this.method1 = function() .. 

this.method2 = function() ..

var privateMethod = function() ..

var privateVar = '...';

return this;

and then in another service, inject the BaseService, and then begin by doing

this = Object.create(BaseService);

then you can extend the BaseService in this new service with other methods

this.newServiceMethod = function() ...

Upvotes: 0

Maciej Sikora
Maciej Sikora

Reputation: 20132

Angular is framework, JavaScript is programming language, inheritance is language feature not framework feature. So start from - how to do inheritance in JavaScript and it can be used in angular ( or any other framework ) which is only library created in javascript.

Some frameworks created methods like extend in Backbone but extend is method which is doing prototype based inheritance in pure js. It is no magic. The same thing can be done in angular variables in services, scopes etc.

Upvotes: 0

Max Koretskyi
Max Koretskyi

Reputation: 105449

Since AngularJS does not provide any built-in features for using inheritance

That means that angular as a framework doesn't have any mechanism for inheritance (code reuse), like, for example, backbone's extend functionality. However, you may still need to inherit functionality from controllers or services, and so the article shows you how to do that using native JS within angular framework.

we do have scope for inheritance in angular

yes, scopes do use prototypes, but it's used not share functionality (code reuse), but to enable child scopes access data from parents without additional mechanism

Upvotes: 1

keerti
keerti

Reputation: 465

AngularJs $Scope - In AngularJS, $Scope is object that is organized into a hierarchy.There is a root scope, and the root scope has one or more child scopes. Each view has its own $scope (which is a child of the root scope), so whatever variables one view controller sets on its $scope variable, those variables are invisible to other controllers.

Inheritance - Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. Inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Upvotes: 0

Related Questions