CoffeeScript クラスの変数とメソッドの定義方法まとめ
7183 ワード
CoffeeScriptのクラスまわりのシンタックスが分かりづらすぎるのでまとめました。
privateなインスタンス変数・メソッドは未実装なようです。悲しい。
protectedなんて勿論ありません。
メソッド
インスタンスメソッド(publicのみ)
hoge.coffee
class Hoge
hello: -> console.log('hello')
a = new Hoge()
a.hello()
クラスメソッド
public
二種類の定義方法がある。
hoge.coffee
class Hoge
@hello: -> console.log('hello')
@hi = -> console.log('hi')
Hoge.hello()
Hoge.hi()
private
hoge.coffee
class Hoge
hello = -> console.log('hello')
@moge: -> hello()
piyo: -> hello()
変数
インスタンス変数(publicのみ)
三種類の定義方法がある。
hoge.coffee
class Hoge
x: 10
constructor: (@y) -> @z = 30
print: -> console.log(@x)
a = new Hoge(20)
console.log(a.x)
クラス変数
public
二種類の定義方法がある。
hoge.coffee
class Hoge
@x = 10
@y: 20
@printClass: -> console.log(Hoge.x)
printInstance: -> console.log(Hoge.x)
console.log(Hoge.x)
private
hoge.coffee
class Hoge
x = 10
@printClass: -> console.log(x)
printInstance: -> console.log(x)
参考
http://memo.devjam.net/clip/943
http://memo.devjam.net/clip/983
http://qiita.com/mm-git/items/ced8482a41cd070c245a
Author And Source
この問題について(CoffeeScript クラスの変数とメソッドの定義方法まとめ), 我々は、より多くの情報をここで見つけました https://qiita.com/malt03/items/9f5a5646046fbc0cc50c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .