teropa/build-your-own-angularjs

Question in Integrating Expressions to Scopes

Opened this issue · 0 comments

In page 349, in the case 'accepts expressions for watch functions':

it('accepts expressions for watch functions', function() {
  var theValue;
  scope.aValue = 42;
  scope.$watch('aValue', function(newValue, oldValue, scope) {
    theValue = newValue;
  });
  scope.$digest();
  expect(theValue).toBe(42);
});

scope.js

Scope.prototype.$watch = function(watchFn, listenerFn, valueEq) {  
     var self = this;    
     var watcher = {
           watchFn: parse(watchFn),
            listenerFn: listenerFn || function() { },   
            last: initWatchVal,   
            valueEq: !!valueEq  	
       };
       ...
}

I am confused about that how watchFn : parse(watchFn) relation to the Scope. When parse(watchFn) return the function functon() {return aValue}, witch the variable aValueis not relation to the scope