如何在Coffeescript中扩展jQuery,以便用jQuery附加Coffeescript类?
我想做的是在main中添加一个如何在Coffeescript中扩展jQuery,以便用jQuery附加Coffeescript类?,jquery,coffeescript,extend,Jquery,Coffeescript,Extend,我想做的是在main中添加一个圆圈,如下所示:stage.append圆圈。我认为Circle扩展jQuery是一个很好的解决方案,但这似乎不起作用 我当前的代码: 圆形咖啡: define [], () -> class Circle constructor: (@x, @y, @radius) -> @circle = $ document.createElement "div" @circle.addClass "circle"
圆圈
,如下所示:stage.append圆圈
。我认为Circle扩展jQuery是一个很好的解决方案,但这似乎不起作用
我当前的代码:
圆形咖啡:
define [], () ->
class Circle
constructor: (@x, @y, @radius) ->
@circle = $ document.createElement "div"
@circle.addClass "circle"
@circle.width @radius * 2
@circle.height @radius * 2
@circle.css
left: @x
top: @y
梅因:咖啡:
define [
'object/Circle'
], (Circle) ->
stage = $ "#main"
circle = new Circle 350, 350, 100
stage.append circle.circle
这是可能的。你必须考虑两种情况。第一种情况是选择器是指向DOM中现有对象的字符串。第二种情况是选择器指向“新”元素或选择器指向另一个对象 对于第一种情况,需要将新创建的jQuery对象复制到实例中。这是为了让jQuery有机会搜索DOM。最简单的检查方法是检查选择器是否是字符串而不是HTML标记。这种方法可能不够彻底,但对于基本用法来说是有效的 对于第二种情况,可以使用this作为第一个参数调用init。这将围绕当前实例创建一个新的jQuery对象
class Element extends jQuery
constructor: (selector, context) ->
# When selector is a string and not an html tag:
# body, #my-id, .error-class etc.
if typeof selector is 'string' and not selector.match /^<.*>$/
jQuery.extend true, this, jQuery.fn.init(selector, context, jQuery document)
# When selector is a jQuery object
else if selector instanceof jQuery
jQuery.extend true, this, selector
else
# When selector points to an object which doesn't exist in the DOM:
# <div>, document.createElement('span')
# Or when selector is an object
jQuery.fn.init.call this, selector, context
@constructor = jQuery
并使用类似于$
这是可能的。你必须考虑两种情况。第一种情况是选择器是指向DOM中现有对象的字符串。第二种情况是选择器指向“新”元素或选择器指向另一个对象 对于第一种情况,需要将新创建的jQuery对象复制到实例中。这是为了让jQuery有机会搜索DOM。最简单的检查方法是检查选择器是否是字符串而不是HTML标记。这种方法可能不够彻底,但对于基本用法来说是有效的 对于第二种情况,可以使用this作为第一个参数调用init。这将围绕当前实例创建一个新的jQuery对象
class Element extends jQuery
constructor: (selector, context) ->
# When selector is a string and not an html tag:
# body, #my-id, .error-class etc.
if typeof selector is 'string' and not selector.match /^<.*>$/
jQuery.extend true, this, jQuery.fn.init(selector, context, jQuery document)
# When selector is a jQuery object
else if selector instanceof jQuery
jQuery.extend true, this, selector
else
# When selector points to an object which doesn't exist in the DOM:
# <div>, document.createElement('span')
# Or when selector is an object
jQuery.fn.init.call this, selector, context
@constructor = jQuery
并使用类似于$
这是可能的。你必须考虑两种情况。第一种情况是选择器是指向DOM中现有对象的字符串。第二种情况是选择器指向“新”元素或选择器指向另一个对象 对于第一种情况,需要将新创建的jQuery对象复制到实例中。这是为了让jQuery有机会搜索DOM。最简单的检查方法是检查选择器是否是字符串而不是HTML标记。这种方法可能不够彻底,但对于基本用法来说是有效的 对于第二种情况,可以使用this作为第一个参数调用init。这将围绕当前实例创建一个新的jQuery对象
class Element extends jQuery
constructor: (selector, context) ->
# When selector is a string and not an html tag:
# body, #my-id, .error-class etc.
if typeof selector is 'string' and not selector.match /^<.*>$/
jQuery.extend true, this, jQuery.fn.init(selector, context, jQuery document)
# When selector is a jQuery object
else if selector instanceof jQuery
jQuery.extend true, this, selector
else
# When selector points to an object which doesn't exist in the DOM:
# <div>, document.createElement('span')
# Or when selector is an object
jQuery.fn.init.call this, selector, context
@constructor = jQuery
并使用类似于$
这是可能的。你必须考虑两种情况。第一种情况是选择器是指向DOM中现有对象的字符串。第二种情况是选择器指向“新”元素或选择器指向另一个对象 对于第一种情况,需要将新创建的jQuery对象复制到实例中。这是为了让jQuery有机会搜索DOM。最简单的检查方法是检查选择器是否是字符串而不是HTML标记。这种方法可能不够彻底,但对于基本用法来说是有效的 对于第二种情况,可以使用this作为第一个参数调用init。这将围绕当前实例创建一个新的jQuery对象
class Element extends jQuery
constructor: (selector, context) ->
# When selector is a string and not an html tag:
# body, #my-id, .error-class etc.
if typeof selector is 'string' and not selector.match /^<.*>$/
jQuery.extend true, this, jQuery.fn.init(selector, context, jQuery document)
# When selector is a jQuery object
else if selector instanceof jQuery
jQuery.extend true, this, selector
else
# When selector points to an object which doesn't exist in the DOM:
# <div>, document.createElement('span')
# Or when selector is an object
jQuery.fn.init.call this, selector, context
@constructor = jQuery
并使用类似于$