From 9cbf2a82ec628b02e02a8a6342eaecc99f840491 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Fri, 27 May 2011 18:41:31 -0400 Subject: [PATCH] __bind helper: caching ctor --- lib/nodes.js | 22 +++++++++++----------- src/nodes.coffee | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index e97366d1..1e5c7d0f 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -7,16 +7,16 @@ child.prototype = new ctor; child.__super__ = parent.prototype; return child; - }, __bind = (Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){ - return function bound(){ - if (!(this instanceof bound)) - return fn.apply(me, arguments); - var ctor = function(){}; - ctor.prototype = fn.prototype; - var obj = new ctor(); - fn.apply(obj, arguments); - return obj; - }; + }, __bind = (Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){ + var ctor = function(){}; + ctor.prototype = fn.prototype; + return function bound(){ + if (!(this instanceof bound)) + return fn.apply(me, arguments); + var obj = new ctor; + fn.apply(obj, arguments); + return obj; + }; }), __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; @@ -2258,7 +2258,7 @@ }; UTILITIES = { "extends": 'function(child, parent) {\n for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n child.__super__ = parent.prototype;\n return child;\n}', - bind: ' (Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){\n return function bound(){\n if (!(this instanceof bound))\n return fn.apply(me, arguments);\n var ctor = function(){};\n ctor.prototype = fn.prototype;\n var obj = new ctor();\n fn.apply(obj, arguments);\n return obj;\n };\n})', + bind: '(Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){\n var ctor = function(){};\n ctor.prototype = fn.prototype;\n return function bound(){\n if (!(this instanceof bound))\n return fn.apply(me, arguments);\n var obj = new ctor;\n fn.apply(obj, arguments);\n return obj;\n };\n})', indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (this[i] === item) return i;\n }\n return -1;\n}', hasProp: 'Object.prototype.hasOwnProperty', slice: 'Array.prototype.slice' diff --git a/src/nodes.coffee b/src/nodes.coffee index 2a877799..e31d9662 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1770,12 +1770,12 @@ UTILITIES = # Create a function bound to the current value of "this". bind: ''' (Function.prototype.bind ? Function.prototype.call.bind(Function.prototype.bind) : function(fn, me){ + var ctor = function(){}; + ctor.prototype = fn.prototype; return function bound(){ if (!(this instanceof bound)) return fn.apply(me, arguments); - var ctor = function(){}; - ctor.prototype = fn.prototype; - var obj = new ctor(); + var obj = new ctor; fn.apply(obj, arguments); return obj; };