English 中文(简体)
静态财产的继承
原标题:static property inheritance CoffeeScript

让我们定义这个简单的代码 :

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 points%20Foo%20A20A%20A20A20A%20A%20A%20A20A%ccode_code_codecode_codeal_Fode_Fode_Fode_Focol_Focol_%2_%202020202020202020Col_col_

(是的,这很奇怪,因为它是,而不是,但这是因为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)




相关问题
How do I define global variables in CoffeeScript?

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 ...

I m trying to re-write this in CoffeeScript. Coming unstuck

function getElementsByClassName(className) { // get all elements in the document if (document.all) { var allElements = document.all; } else { var allElements = document.getElementsByTagName("...

Anonymous functions syntax in CoffeeScript

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 = $....

Why are my CoffeeScript/backbone.js events not firing?

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" ...

Custom compiler with maven

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 ...

what is wrong when I compile my .coffee files

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/...

CoffeeScript on Windows?

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 ...

How can I compile CoffeeScript from .NET?

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][...