Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor($parse): change 'this' to a $parse keyword instead of scope field #9105

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ forEach({
CONSTANTS[name] = constantGetter;
});

//Not quite a constant, but can be lex/parsed the same
CONSTANTS['this'] = function(self) { return self; };
CONSTANTS['this'].sharedGetter = true;


//Operators - will be wrapped by binaryFn/unaryFn/assignment/filter
var OPERATORS = extend(createMap(), {
/* jshint bitwise : false */
Expand Down
3 changes: 1 addition & 2 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function $RootScopeProvider(){
this.$$phase = this.$parent = this.$$watchers =
this.$$nextSibling = this.$$prevSibling =
this.$$childHead = this.$$childTail = null;
this['this'] = this.$root = this;
this.$root = this;
this.$$destroyed = false;
this.$$asyncQueue = [];
this.$$postDigestQueue = [];
Expand Down Expand Up @@ -212,7 +212,6 @@ function $RootScopeProvider(){
}
child = new this.$$ChildScope();
}
child['this'] = child;
child.$parent = this;
child.$$prevSibling = this.$$childTail;
if (this.$$childHead) {
Expand Down
21 changes: 19 additions & 2 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,25 @@ describe('Scope', function() {


describe('this', function() {
it('should have a \'this\'', inject(function($rootScope) {
expect($rootScope['this']).toEqual($rootScope);
it('should evaluate \'this\' to be the scope', inject(function($rootScope) {
var child = $rootScope.$new();
expect($rootScope.$eval('this')).toEqual($rootScope);
expect(child.$eval('this')).toEqual(child);
}));

it('\'this\' should not be recursive', inject(function($rootScope) {
expect($rootScope.$eval('this.this')).toBeUndefined();
expect($rootScope.$eval('$parent.this')).toBeUndefined();
}));

it('should not be able to overwrite the \'this\' keyword', inject(function($rootScope) {
$rootScope['this'] = 123;
expect($rootScope.$eval('this')).toEqual($rootScope);
}));

it('should be able to access a variable named \'this\'', inject(function($rootScope) {
$rootScope['this'] = 42;
expect($rootScope.$eval('this[\'this\']')).toBe(42);
}));
});

Expand Down