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

Commit 5572b40

Browse files
jbedardIgorMinar
authored andcommitted
refactor($parse): change 'this' to a $parse keyword instead of scope field
BREAKING CHANGE: - $scope['this'] no longer exits on the $scope object - $parse-ed expressions no longer allow chaining 'this' such as this['this'] or $parent['this'] - 'this' in $parse-ed expressions can no longer be overriden, if a variable named 'this' is put on the scope it must be accessed using this['this'] Closes #9105
1 parent 4a6c7cf commit 5572b40

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/ng/parse.js

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ forEach({
9292
CONSTANTS[name] = constantGetter;
9393
});
9494

95+
//Not quite a constant, but can be lex/parsed the same
96+
CONSTANTS['this'] = function(self) { return self; };
97+
CONSTANTS['this'].sharedGetter = true;
98+
99+
95100
//Operators - will be wrapped by binaryFn/unaryFn/assignment/filter
96101
var OPERATORS = extend(createMap(), {
97102
/* jshint bitwise : false */

src/ng/rootScope.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function $RootScopeProvider(){
128128
this.$$phase = this.$parent = this.$$watchers =
129129
this.$$nextSibling = this.$$prevSibling =
130130
this.$$childHead = this.$$childTail = null;
131-
this['this'] = this.$root = this;
131+
this.$root = this;
132132
this.$$destroyed = false;
133133
this.$$asyncQueue = [];
134134
this.$$postDigestQueue = [];
@@ -220,7 +220,6 @@ function $RootScopeProvider(){
220220
}
221221
child = new this.$$ChildScope();
222222
}
223-
child['this'] = child;
224223
child.$parent = parent;
225224
child.$$prevSibling = parent.$$childTail;
226225
if (parent.$$childHead) {

test/ng/rootScopeSpec.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,25 @@ describe('Scope', function() {
5151

5252

5353
describe('this', function() {
54-
it('should have a \'this\'', inject(function($rootScope) {
55-
expect($rootScope['this']).toEqual($rootScope);
54+
it('should evaluate \'this\' to be the scope', inject(function($rootScope) {
55+
var child = $rootScope.$new();
56+
expect($rootScope.$eval('this')).toEqual($rootScope);
57+
expect(child.$eval('this')).toEqual(child);
58+
}));
59+
60+
it('\'this\' should not be recursive', inject(function($rootScope) {
61+
expect($rootScope.$eval('this.this')).toBeUndefined();
62+
expect($rootScope.$eval('$parent.this')).toBeUndefined();
63+
}));
64+
65+
it('should not be able to overwrite the \'this\' keyword', inject(function($rootScope) {
66+
$rootScope['this'] = 123;
67+
expect($rootScope.$eval('this')).toEqual($rootScope);
68+
}));
69+
70+
it('should be able to access a variable named \'this\'', inject(function($rootScope) {
71+
$rootScope['this'] = 42;
72+
expect($rootScope.$eval('this[\'this\']')).toBe(42);
5673
}));
5774
});
5875

0 commit comments

Comments
 (0)