could anyone explain what's the difference between inherit and extend in AngularJS?
many thanks!
Lior
EDIT: look here: github.com/angular/angular.js/blob/master/src/Angular.js and search for 'inherit' to find it (line 246 currently)
could anyone explain what's the difference between inherit and extend in AngularJS?
many thanks!
Lior
EDIT: look here: github.com/angular/angular.js/blob/master/src/Angular.js and search for 'inherit' to find it (line 246 currently)
There is no inherit function in Angular.
Extend just copies properties from one object to another. It's a common bit of JavaScript juju. JQuery has an extend function as well.
var x = { foo: 'hello' };
var y = { bar: 'world' };
angular.extend(x, y);
console.log(x); //{ foo: 'hello', bar: 'world' }
The only inheritance I can presume you're talking about in Angular would be how child scopes inherit the properties of the parent scopes. You're quite right that there is a difference here, because it's not done via anything like extend... it's done via prototypical inheritance. Where the parent scope is actually the prototype of the child scope. This means that when you try to access a property on a child scope that doesn't exist, it will fall down to it's prototype and check that property, if it doesn't exist there, it will fall down to THAT prototype and check the property there.
This means that when you update the property on a parent, it updates for all of it's children... HOWEVER, if you update for a child, it will not update the parents... because it sets the property directly on the child, and not on it's prototype (which is the parent).
If that's not clear, there are a ton of blog entries / StackOverflow questions about this already: