Re: Coffeescript inheritance?

63 views
Skip to first unread message

Toby Nicholas

unread,
Nov 29, 2012, 9:55:32 AM11/29/12
to therub...@googlegroups.com
Oops - apologies for the typos in that code.

Anyway, I've got it minimally working using this code, but I really need to understand Javascript's prototypal inheritance a bit better to make any more progress!

T



class Square
  def area(length)
  length * length
  end
end

cxt = V8::Context.new
cxt["Square"] = Square
coffeescript = "class BlueSquare extends Square"
cxt.eval CoffeeScript.compile(coffeescript, :bare => true)
cxt.eval "BlueSquare.prototype = new Square"
cxt.eval "azure = new BlueSquare"
cxt.eval "azure.area(3)"




On Thursday, November 29, 2012 8:15:32 AM UTC, Toby Nicholas wrote:
Hi folks,

I'm wondering if it's possible to use CoffeeScript inheritance with Ruby classes passed to the V8 context... something like:

class Square
  def area(length)
  length * length
  end
end

cxt = V8::Context.new
cxt["Square"] = Square
coffeescript = "class BlueSquare extends Square"
cxt.eval CoffeeScript.compile(cs), :bare => true
cxt.eval "azure = new BlueSquare"
cxt.eval "azure.area(3)"
# => V8::Error: Object #<BlueSquare> has no method 'area'

I've tried poking around but I can't figure out quite how Ruby classes are being represented. Worth persevering with, or am I barking up the wrong tree?

Thanks!

Toby


Charles Lowell

unread,
Nov 29, 2012, 9:56:55 AM11/29/12
to therub...@googlegroups.com
On Nov 29, 2012, at 8:55 AM, Toby Nicholas wrote:

Oops - apologies for the typos in that code.

Anyway, I've got it minimally working using this code, but I really need to understand Javascript's prototypal inheritance a bit better to make any more progress!

T

Perhaps if you were to paste the generated JavaScript source it would be easier to diagnose the problem.


cheers,
Charles
Charles Lowell 
thefrontside.net | twitter: @cowboyd | github: cowboyd




Toby Nicholas

unread,
Dec 12, 2012, 12:46:58 PM12/12/12
to therub...@googlegroups.com
Right, yes, sorry. Thanks Charles.

CoffeeScript.compile("class BlueSquare extends Square", :bare => true) 

gives this:

var BlueSquare,
  __hasProp = {}.hasOwnProperty,
  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

BlueSquare = (function(_super) {

  __extends(BlueSquare, _super);

  function BlueSquare() {
    return BlueSquare.__super__.constructor.apply(this, arguments);
  }

  return BlueSquare;

})(Square);


The syntax I'd like to use is this:

class Square
  def area(length)
  length * length
  end
end

cxt = V8::Context.new
cxt["Square"] = Square
coffeescript = "class BlueSquare extends Square"
cxt.eval CoffeeScript.compile(coffeescript, :bare => true)
cxt.eval "azure = new BlueSquare"
cxt.eval "azure.area(3)"

Which results in:

# => V8::Error: Object #<BlueSquare> has no method 'area'

Of course, the same syntax works fine if the 'Square' class is simply defined as a function in javascript:

cxt = V8::Context.new
cxt.eval(<<-JS)
Square = function() { 
  this.area = function(length) { 
    return length * length; 
  } 
}
JS
coffeescript = "class BlueSquare extends Square"
cxt.eval CoffeeScript.compile(coffeescript, :bare => true)
cxt.eval "azure = new BlueSquare"
cxt.eval "azure.area(3)"
# => 9

... so I'm wondering about the structure of the 'Square' class passed by The Ruby Racer, and how (whether) I can use CoffeeScript's 'subclassing' syntax with it in this way. 

Thanks!

Toby


Reply all
Reply to author
Forward
0 new messages