0

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.

3

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it helped to clear my doubt that scope is to share data between child and parent but i have one small doubt if I add a function to parent's scope won't my child controller get it and if it does can't it augment it to reuse ?
@Charu, yes, child scope can have access to parent functions as well, if your child scope is not isolated. But I wouldn't reuse functions defined on scope, if you need a function that is used in multiple places it's better to put it on some service if it's related to business logic or a controller, if this function prepared data for presentation
Angular does have angular.extend()
@AmyBlankenship, sure, but it's not used for inheritance through prototype
0

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.

5 Comments

I am aware of that but my doubt is what the author says "<b>Since AngularJS does not provide any built-in features for using inheritance, </b> 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 that in angular then what does he mean by that ?
scope is just an object which refers to application model, which will bind the values of ng-model and also it can watch the expressions and events
@MBalajivaishnav I understand scope my concern is not that . Kindly see my edited question and the comment above you to see my doubt .
@Charu do u think scope does the inheritance
@Charu - please check updated answer, hope this will help you to understand $Scope in AngularJs
0

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.

Comments

0

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() ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.