首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

coffeescript fat arrow访问不是父母'这个'

问题内容:coffeescript fat arrow访问不是父母'这个'

答案内容:

在 CoffeeScript 中,fat arrow(也称为 arrow function)是一种特殊的函数声明,它将函数声明与函数表达式结合在一起。fat arrow 在父函数中定义,可以访问父函数中的变量,属性等。

例如,下面的代码定义了一个 fat arrow,它访问了父函数中的 this 变量:

代码语言:txt
复制
class Person
  constructor: (@name) ->
    @name = "John"
    console.log this

p = new Person "Jane"

在这个例子中,this 变量是在 constructor 函数内部定义的,并且在 console.log this 语句中访问了它。由于 this 变量是在 fat arrow 中定义的,因此它可以在父函数中访问。

与 JavaScript 中的函数表达式不同,fat arrow 是一种函数声明,因此它们可以访问父函数中的变量和属性。这使得 fat arrow 成为在 CoffeeScript 中定义对象属性和方法的一种有用方式。

需要注意的是,虽然 fat arrow 可以访问父函数中的变量和属性,但它们不能访问父函数中的参数。在上面的例子中,name 参数是在 constructor 函数中定义的,并且被赋值为 "John",因此可以在 fat arrow 中访问它。但是,name 参数并不是在 fat arrow 中定义的,因此不能像下面这样访问它:

代码语言:txt
复制
class Person
  constructor: (@name) ->
    @name = "John"
    console.log @name

p = new Person "Jane"

在这个例子中,@name 是在 constructor 函数中定义的,并且在 fat arrow 中访问了它。但是,name 参数是在 constructor 函数中定义的,并且被赋值为 "John",因此不能像下面这样访问它:

代码语言:txt
复制
class Person
  constructor: (@name) ->
    @name = "John"
    console.log @name

p = new Person "Jane"

在这个例子中,name 参数是在 constructor 函数中定义的,并且被赋值为 "John",因此不能像下面这样访问它:

代码语言:txt
复制
class Person
  constructor: (@name) ->
    @name = "John"
    console.log @name

p = new Person "Jane"

在这个例子中,name 参数是在 `constructor

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券