From 62e7d142fd030b74096130abee86935b9fe0bd3f Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Fri, 22 Jan 2010 17:53:03 +0800 Subject: [PATCH] Move tag helper generator out of loop so that it's defined only once. --- src/Template.js | 67 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/Template.js b/src/Template.js index b5edc3a..c40e681 100644 --- a/src/Template.js +++ b/src/Template.js @@ -86,46 +86,45 @@ Jaml.Template.prototype = { */ (function() { var tags = Jaml.Template.prototype.tags; - - for (var i = tags.length - 1; i >= 0; i--){ - var tagName = tags[i]; - - /** - * This function is created for each tag name and assigned to Template's - * prototype below - */ - var fn = function(tagName) { - return function(attrs) { - var node = new Jaml.Node(tagName); - - var firstArgIsAttributes = (typeof attrs == 'object') - && !(attrs instanceof Jaml.Node) - && !(attrs instanceof Jaml.TextNode); - if (firstArgIsAttributes) node.setAttributes(attrs); + /** + * This function is created for each tag name and assigned to Template's + * prototype below + */ + var makeTagHelper = function(tagName) { + return function(attrs) { + var node = new Jaml.Node(tagName); + + var firstArgIsAttributes = (typeof attrs == 'object') + && !(attrs instanceof Jaml.Node) + && !(attrs instanceof Jaml.TextNode); + + if (firstArgIsAttributes) node.setAttributes(attrs); - var startIndex = firstArgIsAttributes ? 1 : 0; + var startIndex = firstArgIsAttributes ? 1 : 0; - for (var i=startIndex; i < arguments.length; i++) { - var arg = arguments[i]; + for (var i=startIndex; i < arguments.length; i++) { + var arg = arguments[i]; - if (typeof arg == "string" || arg == undefined) { - arg = new Jaml.TextNode(arg || ""); - } - - if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) { - arg.parent = node; - } + if (typeof arg == "string" || arg == undefined) { + arg = new Jaml.TextNode(arg || ""); + } - node.addChild(arg); - }; - - this.nodes.push(node); - - return node; + if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) { + arg.parent = node; + } + + node.addChild(arg); }; + + this.nodes.push(node); + + return node; }; - - Jaml.Template.prototype[tagName] = fn(tagName); + }; + + for (var i = tags.length - 1; i >= 0; i--){ + var tagName = tags[i]; + Jaml.Template.prototype[tagName] = makeTagHelper(tagName); }; })(); \ No newline at end of file