简体   繁体   English

我必须使用coffeescript将为图表编写的每一行代码转换为一个类(面向对象)

[英]I have to Transform every line of code that I have written for charts into a class (object oriented) using coffeescript

Lets say I have 5 Fusion Charts Most of the charts have same attributes. 可以说我有5个Fusion Charts大多数图表都具有相同的属性。 I have to pass all this attributes into coffeescript contructor But how to achieve it? 我必须将所有这些属性传递给coffeescript建设者,但是如何实现呢?

any sample code would be helpful 任何示例代码都会有所帮助

Below is a example class declaration that take a parameter and when we call draw method it print input params on console. 下面是一个带有参数的类声明示例,当我们调用draw方法时,它将在控制台上打印输入params

class Chart
  constructor: (params) ->
    # initialize your variable 
    # example @foo = params
    @attr = params

  draw: ->
    # access @attr instance variable   
    console.log "your input params is #{@attr}"


 new Chart('this is test params').draw()

I highly recommended you first read coffeescript official guide before start. 我强烈建议您在开始之前先阅读coffeescript官方指南。 There are great books available that may help you The Little Book on CoffeeScript and coffeescript-cookbook . 有很多好书可以帮助您有关CoffeeScriptcoffeescript-cookbook 的小书 And of-course use http://js2.coffee/ site for experiment js to coffee and vise versa . 当然可以使用http://js2.coffee/网站进行实验js,反之亦然 Hope it will help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM