让我们定义这个简单的代码 :
class Foo
@foo = blah
console.log(@foo)
class Bar extends Foo
constructor: () ->
console.log(@foo)
bar: () ->
console.log(@foo)
b = new Bar
b.bar()
结果如下:
blah
undefined
undefined
如何在继承阶级中访问
让我们定义这个简单的代码 :
class Foo
@foo = blah
console.log(@foo)
class Bar extends Foo
constructor: () ->
console.log(@foo)
bar: () ->
console.log(@foo)
b = new Bar
b.bar()
结果如下:
blah
undefined
undefined
如何在继承阶级中访问
您真的想要写入
console.log(@constructor.foo)
在 Bar < /code> sbuildor. 中 (工作示例 < a href=> http://coffeescription.org/ #try%%% 3cassion%20Foo%00A%20%20%20A%202020%2020A%202020A%202020Flah%20%20foo%20D%20D%20A%20A%20A%20A%20A%20Explends%20Foo%20Foo%20A%20A%20A%20A%28%29%20A%20Conole.log%28%40construction.foo%29%0A0Anew%20BAR="nofolpol">>here
(是的,这很奇怪,因为它是obj.constructor
是 JavaScript-sist,不是特殊的 CoffeeScript 语法。 )
为了进一步澄清:在类体中,指向该类。在构建者中,
指向该类。因此,明显不一致。我在书“ema href=”中的班级章节中花了很多时间。http://pragprog.com/book/tbcoffee/coffeescrept” rel=“nofollow”>CoffeeScript:加速JavaScript开发 。
foo
是 Foo
构造器的属性, 不是原型 :
class Bar extends Foo
constructor: () ->
console.log(Foo.foo)
bar: () ->
console.log(Foo.foo)
On Coffeescript.org: bawbag = (x, y) -> z = (x * y) bawbag(5, 10) would compile to: var bawbag; bawbag = function(x, y) { var z; return (z = (x * y)); }; bawbag(5, 10); compiling via ...
function getElementsByClassName(className) { // get all elements in the document if (document.all) { var allElements = document.all; } else { var allElements = document.getElementsByTagName("...
I ve been looking at CoffeeScript and I m not understanding how you would write code like this. How does it handle nested anonymous functions in its syntax? ;(function($) { var app = $....
I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...
I m trying to make Maven2 to compile coffeescript to javascript. As far as I m concerned there is no plugin which provides compiling coffeescript. Is there a compiler-plugin for maven which can be ...
Hi there Iam using coffeeScript for my apps now and I love it but recently I ve been having a lot of trouble with compilation, Iam using it for a rails application and when I run coffee -w -c public/...
How can I try CoffeeScript on Windows? The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation EDIT: Since I asked this a while ago, many new ...
I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don t want to use [Rhino][...