Rob
Rob

Reputation: 11388

Angular $digest not in $rootScope

How com in angular when I console the $rootScope it does not show the $digest function in the object:

console.log($rootScope);

But if I console $rootScope.$digest it will show the function.

console.log($rootScope.$digest);

Same applies to $apply.

Upvotes: 0

Views: 283

Answers (1)

Philipp Gayret
Philipp Gayret

Reputation: 5070

Because these functions are indeed not defined on the object itself, but on the prototype it inherits from ( JavaScript has prototypical inheritance, read more here. This goes for all $scope objects in AngularJS.

For example with my browser's built in console object:

enter image description here

You can see it only has two real properties, a memory object, and a reference to its prototype, available as the deprecated __proto__ ( And that prototype, inherits from another! ). So anything defined on the prototype ConsoleBase is also accessible via the object console.

AngularJS uses this even further, making scopes inherit from eachother ; when you have a controller inside of another controller, the outer controller can define properties and the inner controller can read them - but not the other way around, as anything defined on the inner scope does not appear on the outer scope, because it is its own object. This is probably not the best explanation, so for the full story see here.

Upvotes: 1

Related Questions