adding automatic 'return this' for constructors: functions that start with a Capital Letter

This commit is contained in:
Jeremy Ashkenas
2010-01-06 23:47:36 -05:00
parent 9ed8020b84
commit 18c3e79a50
3 changed files with 24 additions and 9 deletions

View File

@@ -100,9 +100,12 @@ module CoffeeScript
if node.statement?
node.compile(o.merge(:return => true))
else
constructor = o[:top] && o[:last_assign] && o[:last_assign][0..0][/[A-Z]/]
prefix = constructor ? "if (#{o[:last_assign]} !== this.constructor) " : ''
"#{o[:indent]}#{prefix}return #{node.compile(o)};"
if o[:top] && o[:last_assign] && o[:last_assign][0..0][/[A-Z]/]
temp = o[:scope].free_variable
"#{o[:indent]}#{temp} = #{node.compile(o)};\n#{o[:indent]}return #{o[:last_assign]} === this.constructor ? this : #{temp};"
else
"#{o[:indent]}return #{node.compile(o)};"
end
end
else
ending = node.statement? ? '' : ';'