Skip to content

Commit 67865d3

Browse files
committed
stopped using __proto__, instead, using a variant of goog.inherits for extends and super()
1 parent 0337513 commit 67865d3

File tree

7 files changed

+41
-24
lines changed

7 files changed

+41
-24
lines changed

documentation/cs/super.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
alert(this.name + " moved " + meters + "m.").
44

55
Snake: name => this.name: name.
6-
Snake extends new Animal()
6+
Snake extends Animal
77
Snake.prototype.move: =>
88
alert("Slithering...")
99
super(5).
1010

1111
Horse: name => this.name: name.
12-
Horse extends new Animal()
12+
Horse extends Animal
1313
Horse.prototype.move: =>
1414
alert("Galloping...")
1515
super(45).

documentation/js/super.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
this.name = name;
99
return this.name;
1010
};
11-
Snake.prototype.__proto__ = new Animal();
11+
Snake.__superClass__ = Animal.prototype;
12+
Snake.prototype = new Animal();
13+
Snake.prototype.constructor = Snake;
1214
Snake.prototype.move = function() {
1315
alert("Slithering...");
14-
return Snake.prototype.__proto__.move.call(this, 5);
16+
return Snake.__superClass__.move.call(this, 5);
1517
};
1618
var Horse = function(name) {
1719
this.name = name;
1820
return this.name;
1921
};
20-
Horse.prototype.__proto__ = new Animal();
22+
Horse.__superClass__ = Animal.prototype;
23+
Horse.prototype = new Animal();
24+
Horse.prototype.constructor = Horse;
2125
Horse.prototype.move = function() {
2226
alert("Galloping...");
23-
return Horse.prototype.__proto__.move.call(this, 45);
27+
return Horse.__superClass__.move.call(this, 45);
2428
};
2529
var sam = new Snake("Sammy the Python");
2630
var tom = new Horse("Tommy the Palomino");

examples/code.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ aliquam erat volutpat. Ut wisi enim ad."
145145
alert(this.name + " moved " + meters + "m.").
146146

147147
Snake: name => this.name: name.
148-
Snake extends new Animal()
148+
Snake extends Animal
149149
Snake.prototype.move: =>
150150
alert('Slithering...')
151151
super(5).
152152

153153
Horse: name => this.name: name.
154-
Horse extends new Animal()
154+
Horse extends Animal
155155
Horse.prototype.move: =>
156156
alert('Galloping...')
157157
super(45).

index.html

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,13 @@ <h2>Language Reference</h2>
610610
alert(<span class="Variable">this</span>.name <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span> moved <span class="String">&quot;</span></span> <span class="Keyword">+</span> meters <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span>m.<span class="String">&quot;</span></span>).
611611

612612
<span class="FunctionName">Snake</span><span class="Keyword">:</span> <span class="FunctionArgument">name</span> <span class="Storage">=&gt;</span> <span class="Variable">this</span>.name<span class="Keyword">:</span> name.
613-
Snake <span class="Variable">extends</span> <span class="Keyword">new</span> <span class="TypeName">Animal</span>()
613+
Snake <span class="Variable">extends</span> Animal
614614
<span class="FunctionName">Snake.prototype.move</span><span class="Keyword">:</span> <span class="Storage">=&gt;</span>
615615
alert(<span class="String"><span class="String">&quot;</span>Slithering...<span class="String">&quot;</span></span>)
616616
<span class="Variable">super</span>(<span class="Number">5</span>).
617617

618618
<span class="FunctionName">Horse</span><span class="Keyword">:</span> <span class="FunctionArgument">name</span> <span class="Storage">=&gt;</span> <span class="Variable">this</span>.name<span class="Keyword">:</span> name.
619-
Horse <span class="Variable">extends</span> <span class="Keyword">new</span> <span class="TypeName">Animal</span>()
619+
Horse <span class="Variable">extends</span> Animal
620620
<span class="FunctionName">Horse.prototype.move</span><span class="Keyword">:</span> <span class="Storage">=&gt;</span>
621621
alert(<span class="String"><span class="String">&quot;</span>Galloping...<span class="String">&quot;</span></span>)
622622
<span class="Variable">super</span>(<span class="Number">45</span>).
@@ -639,19 +639,23 @@ <h2>Language Reference</h2>
639639
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
640640
<span class="Keyword">return</span> <span class="Variable">this</span>.<span class="LibraryConstant">name</span>;
641641
};
642-
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">__proto__</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
642+
Snake.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
643+
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
644+
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = Snake;
643645
<span class="LibraryClassType">Snake</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span>() {
644646
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">&quot;</span>Slithering...<span class="String">&quot;</span></span>);
645-
<span class="Keyword">return</span> Snake.<span class="LibraryConstant">prototype</span>.__proto__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
647+
<span class="Keyword">return</span> Snake.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
646648
};
647649
<span class="Storage">var</span> <span class="FunctionName">Horse</span> = <span class="Storage">function</span>(<span class="FunctionArgument">name</span>) {
648650
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
649651
<span class="Keyword">return</span> <span class="Variable">this</span>.<span class="LibraryConstant">name</span>;
650652
};
651-
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">__proto__</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
653+
Horse.__superClass__ <span class="Keyword">=</span> Animal.<span class="LibraryConstant">prototype</span>;
654+
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span> = <span class="Keyword">new</span> <span class="TypeName">Animal</span>();
655+
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">constructor</span> = Horse;
652656
<span class="LibraryClassType">Horse</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span>() {
653657
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">&quot;</span>Galloping...<span class="String">&quot;</span></span>);
654-
<span class="Keyword">return</span> Horse.<span class="LibraryConstant">prototype</span>.__proto__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
658+
<span class="Keyword">return</span> Horse.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
655659
};
656660
<span class="Storage">var</span> sam <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Snake</span>(<span class="String"><span class="String">&quot;</span>Sammy the Python<span class="String">&quot;</span></span>);
657661
<span class="Storage">var</span> tom <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Horse</span>(<span class="String"><span class="String">&quot;</span>Tommy the Palomino<span class="String">&quot;</span></span>);
@@ -666,19 +670,23 @@ <h2>Language Reference</h2>
666670
this.name = name;
667671
return this.name;
668672
};
669-
Snake.prototype.__proto__ = new Animal();
673+
Snake.__superClass__ = Animal.prototype;
674+
Snake.prototype = new Animal();
675+
Snake.prototype.constructor = Snake;
670676
Snake.prototype.move = function() {
671677
alert("Slithering...");
672-
return Snake.prototype.__proto__.move.call(this, 5);
678+
return Snake.__superClass__.move.call(this, 5);
673679
};
674680
var Horse = function(name) {
675681
this.name = name;
676682
return this.name;
677683
};
678-
Horse.prototype.__proto__ = new Animal();
684+
Horse.__superClass__ = Animal.prototype;
685+
Horse.prototype = new Animal();
686+
Horse.prototype.constructor = Horse;
679687
Horse.prototype.move = function() {
680688
alert("Galloping...");
681-
return Horse.prototype.__proto__.move.call(this, 45);
689+
return Horse.__superClass__.move.call(this, 45);
682690
};
683691
var sam = new Snake("Sammy the Python");
684692
var tom = new Horse("Tommy the Palomino");

lib/coffee_script/grammar.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ rule
259259
260260
# Extending an object's prototype.
261261
Extends:
262-
Value EXTENDS Expression { result = ExtendsNode.new(val[0], val[2]) }
262+
Value EXTENDS Value { result = ExtendsNode.new(val[0], val[2]) }
263263
;
264264

265265
# A generic function invocation.
@@ -316,6 +316,7 @@ rule
316316
;
317317

318318
# Array comprehensions, including guard and current index.
319+
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
319320
For:
320321
Expression FOR
321322
ForVariables ForSource { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) }

lib/coffee_script/nodes.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,12 @@ def compile(o={})
211211
def compile_super(args, o)
212212
methname = o[:last_assign].sub(LEADING_DOT, '')
213213
arg_part = args.empty? ? '' : ", #{args}"
214-
"#{o[:proto_assign]}.prototype.__proto__.#{methname}.call(this#{arg_part})"
214+
"#{o[:proto_assign]}.__superClass__.#{methname}.call(this#{arg_part})"
215215
end
216216
end
217217

218218
# Node to extend an object's prototype with an ancestor object.
219+
# After goog.inherits from the Closure Library.
219220
class ExtendsNode < Node
220221
attr_reader :sub_object, :super_object
221222

@@ -224,7 +225,10 @@ def initialize(sub_object, super_object)
224225
end
225226

226227
def compile(o={})
227-
"#{@sub_object.compile(o)}.prototype.__proto__ = #{@super_object.compile(o)}"
228+
sub, sup = @sub_object.compile(o), @super_object.compile(o)
229+
"#{sub}.__superClass__ = #{sup}.prototype;\n#{o[:indent]}" +
230+
"#{sub}.prototype = new #{sup}();\n#{o[:indent]}" +
231+
"#{sub}.prototype.constructor = #{sub}"
228232
end
229233

230234
end

test/fixtures/execution/calling_super.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
'zero/' + string.
44

55
FirstChild: => .
6-
FirstChild extends new Base()
6+
FirstChild extends Base
77
FirstChild.prototype.func: string =>
88
super('one/') + string.
99

1010
SecondChild: => .
11-
SecondChild extends new FirstChild()
11+
SecondChild extends FirstChild
1212
SecondChild.prototype.func: string =>
1313
super('two/') + string.
1414

1515
ThirdChild: => .
16-
ThirdChild extends new SecondChild()
16+
ThirdChild extends SecondChild
1717
ThirdChild.prototype.func: string =>
1818
super('three/') + string.
1919

0 commit comments

Comments
 (0)