84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
比如我有一个大类
class Shapes defaultFill = '#9999D2' defaultStroke = '#2727A6' overFill = '#FFF076' overStroke = '#ED1818'
然后有一个继承它的类
class Dots extends Shapes constructor: -> @defaultFill = defaultFill
我发现这样写是错误的,那应该怎么去使用父类的属性呢?谢谢
欢迎选择我的课程,让我们一起见证您的进步~~
@huandu 的方法很好,但是解决不了题主的问题,因为基类写的有点问题。可以看下下面这段代码编译出来的结果:
class Shapes defaultFill = '#9999D2' @defaultStroke = '#2727A6' overFill : '#FFF076' @overStroke : '#ED1818'
-->
Shapes = (function() { var defaultFill; function Shapes() {} defaultFill = '#9999D2'; Shapes.defaultStroke = '#2727A6'; Shapes.prototype.overFill = '#FFF076'; Shapes.overStroke = '#ED1818'; return Shapes; })();
因此,题主基类中的defaultFill声明其实是在一个闭包中,无法在子类中访问到。应该写成:
defaultFill
class Shapes @defaultFill = '#9999D2'
class Dots extends Shapes constructor: -> @defaultFill = @constructor.defaultFill
this.constructor.defaultFill
参考:http://coffeescript.org/#classes
@huandu 的方法很好,但是解决不了题主的问题,因为基类写的有点问题。
可以看下下面这段代码编译出来的结果:
-->
因此,题主基类中的
defaultFill
声明其实是在一个闭包中,无法在子类中访问到。应该写成:
参考:http://coffeescript.org/#classes