Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LiveScript类作用域这个自jquery问题_Livescript - Fatal编程技术网

LiveScript类作用域这个自jquery问题

LiveScript类作用域这个自jquery问题,livescript,Livescript,在jQuery函数中,我实际上需要两个作用域,所以我不能只更改匿名函数的绑定方式。我可以在方法2的开头加上self=@,然后使用self.method。我希望始终使用self.method。但我不想把它放在每个函数的开头 是否要在构造函数中设置self,使其始终存在并指向@?我尝试的一切都失败了自我在方法中始终未定义 我把Coffeescript作为一个标签,因为它与LiveScript非常相似。波浪形箭头~>让您可以携带上下文,这样您就可以避免self=@: class A () -&

在jQuery函数中,我实际上需要两个作用域,所以我不能只更改匿名函数的绑定方式。我可以在方法2的开头加上self=@,然后使用self.method。我希望始终使用
self.method
。但我不想把它放在每个函数的开头

是否要在构造函数中设置
self
,使其始终存在并指向
@
?我尝试的一切都失败了<代码>自我在方法中始终未定义


我把Coffeescript作为一个标签,因为它与LiveScript非常相似。

波浪形箭头
~>
让您可以携带上下文,这样您就可以避免
self=@

class A
    () ->
    method: ->
    method2: ->
        $ 'a' .each ->
            href = $ @ .attr 'href'
            @method href

将回调中的jQuery元素作为参数传递,并继续使用波浪箭头,以便上下文始终是实例。

未意识到传递了该元素。完美的
method2: ->
  $ \a .each (, el) ~>
    href = $ el .attr \href
    @method href