mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-23 11:37:38 +08:00
added compiled getterFN for better performance
This commit is contained in:
@@ -151,9 +151,7 @@ Lexer.prototype = {
|
||||
}
|
||||
var fn = Lexer.OPERATORS[ident];
|
||||
if (!fn) {
|
||||
fn = function(self){
|
||||
return getter(self, ident);
|
||||
};
|
||||
fn = getterFn(ident);
|
||||
fn.isAssignable = ident;
|
||||
}
|
||||
this.tokens.push({index:start, text:ident, fn:fn});
|
||||
@@ -563,8 +561,9 @@ Parser.prototype = {
|
||||
|
||||
fieldAccess: function(object) {
|
||||
var field = this.expect().text;
|
||||
var getter = getterFn(field);
|
||||
var fn = function (self){
|
||||
return getter(object(self), field);
|
||||
return getter(object(self));
|
||||
};
|
||||
fn.isAssignable = field;
|
||||
return fn;
|
||||
|
||||
35
src/Scope.js
35
src/Scope.js
@@ -43,6 +43,41 @@ function setter(instance, path, value){
|
||||
return value;
|
||||
}
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
var getterFnCache = {};
|
||||
function getterFn(path){
|
||||
var fn = getterFnCache[path];
|
||||
if (fn) return fn;
|
||||
|
||||
var code = 'function (self){\n';
|
||||
code += ' var last, fn, type;\n';
|
||||
foreach(path.split('.'), function(key) {
|
||||
code += ' if(!self) return self;\n';
|
||||
code += ' last = self;\n';
|
||||
code += ' self = self.' + key + ';\n';
|
||||
code += ' if(typeof self == "function") \n';
|
||||
code += ' self = function(){ return last.'+key+'.apply(last, arguments); };\n';
|
||||
if (key.charAt(0) == '$') {
|
||||
// special code for super-imposed functions
|
||||
var name = key.substr(1);
|
||||
code += ' if(!self) {\n';
|
||||
code += ' type = angular.Global.typeOf(last);\n';
|
||||
code += ' fn = (angular[type.charAt(0).toUpperCase() + type.substring(1)]||{})["' + name + '"];\n';
|
||||
code += ' if (fn)\n';
|
||||
code += ' self = function(){ return fn.apply(last, [last].concat(slice.call(arguments, 0, arguments.length))); };\n';
|
||||
code += ' }\n';
|
||||
}
|
||||
});
|
||||
code += ' return self;\n}';
|
||||
fn = eval('(' + code + ')');
|
||||
fn.toString = function(){ return code; };
|
||||
|
||||
return getterFnCache[path] = fn;
|
||||
}
|
||||
|
||||
///////////////////////////////////
|
||||
|
||||
var compileCache = {};
|
||||
function expressionCompile(exp){
|
||||
if (isFunction(exp)) return exp;
|
||||
|
||||
Reference in New Issue
Block a user