coffeescript类

use*_*108 2 javascript inheritance class coffeescript kineticjs

当我用新的Kinetic.Stage替换新的Gallery时,代码正常工作当我使用dirived类时,它不起作用.

为什么从Kinetic.Stage衍生出来的画廊错了?

width = window.innerWidth   || document.documentElement.clientWidth || document.body.clientWidth || 0
height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0


class Gallery extends Kinetic.Stage
  constructor: (config) -> 
      super(config)



window.onload = -> 
  list_of_photos = jQuery('#_image img')
  x_pos = width/4
  y_pos = height/4
  stage = new Gallery
                container: "gallery_container"
                width: width
                height: height
  images_layer = new Kinetic.Layer()


  for image in list_of_photos
    imageObj = new Image()
    imageObj.src = image.src
    x_pos = x_pos + 100
    y_pos = y_pos + 10
    ori = new Kinetic.Image
                x: x_pos 
                y: y_pos
                image: imageObj
                draggable: true
                width: 200
                height: 200
    images_layer.add ori
  stage.add images_layer
Run Code Online (Sandbox Code Playgroud)

lav*_*ton 5

问题 - Kineticjs对象与coffeescript类没有"兼容".

解决 - 你必须使用其他方式称为"超级"

class Gallery extends Kinetic.Stage
    constructor : (config) ->
        Kinetic.Stage.call(@, config)
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/lavrton/w2EQD/4/

UPD:我发现这个问题仅存在于"构造函数"方法中.对其他人好的:

class Gallery extends Kinetic.Stage
  constructor: (config) -> 
    Kinetic.Stage.call(@, config)
  add : (item) ->
    console.log(item)
    super item
Run Code Online (Sandbox Code Playgroud)