stopped using __proto__, instead, using a variant of goog.inherits for extends and super()

This commit is contained in:
Jeremy Ashkenas
2009-12-25 13:57:47 -08:00
parent f55b4cd8b6
commit e494d520ea
7 changed files with 41 additions and 24 deletions

View File

@@ -259,7 +259,7 @@ rule
# Extending an object's prototype.
Extends:
Value EXTENDS Expression { result = ExtendsNode.new(val[0], val[2]) }
Value EXTENDS Value { result = ExtendsNode.new(val[0], val[2]) }
;
# A generic function invocation.
@@ -316,6 +316,7 @@ rule
;
# Array comprehensions, including guard and current index.
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
For:
Expression FOR
ForVariables ForSource { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) }

View File

@@ -211,11 +211,12 @@ module CoffeeScript
def compile_super(args, o)
methname = o[:last_assign].sub(LEADING_DOT, '')
arg_part = args.empty? ? '' : ", #{args}"
"#{o[:proto_assign]}.prototype.__proto__.#{methname}.call(this#{arg_part})"
"#{o[:proto_assign]}.__superClass__.#{methname}.call(this#{arg_part})"
end
end
# Node to extend an object's prototype with an ancestor object.
# After goog.inherits from the Closure Library.
class ExtendsNode < Node
attr_reader :sub_object, :super_object
@@ -224,7 +225,10 @@ module CoffeeScript
end
def compile(o={})
"#{@sub_object.compile(o)}.prototype.__proto__ = #{@super_object.compile(o)}"
sub, sup = @sub_object.compile(o), @super_object.compile(o)
"#{sub}.__superClass__ = #{sup}.prototype;\n#{o[:indent]}" +
"#{sub}.prototype = new #{sup}();\n#{o[:indent]}" +
"#{sub}.prototype.constructor = #{sub}"
end
end