From 429938da1f45b8a649b8c77762fb0ae59b6d0cea Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Tue, 2 Dec 2014 10:46:09 +0100 Subject: [PATCH] fix($parse): Follow JavaScript context for unbound functions Use `undefined` as the context when a function is ounbound. E.g. when executing `foo()()`, then `foo()` is executed using the scope as the context, the function returned by `foo()` will have an `undefined` context --- src/ng/parse.js | 2 +- test/ng/parseSpec.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ng/parse.js b/src/ng/parse.js index 4b968b02..0d30d1ff 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -709,7 +709,7 @@ Parser.prototype = { var args = argsFn.length ? [] : null; return function $parseFunctionCall(scope, locals) { - var context = contextGetter ? contextGetter(scope, locals) : scope; + var context = contextGetter ? contextGetter(scope, locals) : isDefined(contextGetter) ? undefined : scope; var fn = fnGetter(scope, locals, context) || noop; if (args) { diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js index 519b7681..b3b16a4a 100644 --- a/test/ng/parseSpec.js +++ b/test/ng/parseSpec.js @@ -508,11 +508,18 @@ describe('parser', function() { }); it('should evaluate function call from a return value', function() { - scope.val = 33; - scope.getter = function() { return function() { return this.val; }; }; + scope.getter = function() { return function() { return 33; }; }; expect(scope.$eval("getter()()")).toBe(33); }); + // There is no "strict mode" in IE9 + if (!msie || msie > 9) { + it('should set no context to functions returned by other functions', function() { + scope.getter = function() { return function() { expect(this).toBeUndefined(); }; }; + scope.$eval("getter()()"); + }); + } + it('should evaluate multiplication and division', function() { scope.taxRate = 8; scope.subTotal = 100;