问题内容:coffeescript fat arrow访问不是父母'这个'
答案内容:
在 CoffeeScript 中,fat arrow(也称为 arrow function)是一种特殊的函数声明,它将函数声明与函数表达式结合在一起。fat arrow 在父函数中定义,可以访问父函数中的变量,属性等。
例如,下面的代码定义了一个 fat arrow,它访问了父函数中的 this
变量:
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 中定义的,因此不能像下面这样访问它:
class Person
constructor: (@name) ->
@name = "John"
console.log @name
p = new Person "Jane"
在这个例子中,@name
是在 constructor
函数中定义的,并且在 fat arrow 中访问了它。但是,name
参数是在 constructor
函数中定义的,并且被赋值为 "John",因此不能像下面这样访问它:
class Person
constructor: (@name) ->
@name = "John"
console.log @name
p = new Person "Jane"
在这个例子中,name
参数是在 constructor
函数中定义的,并且被赋值为 "John",因此不能像下面这样访问它:
class Person
constructor: (@name) ->
@name = "John"
console.log @name
p = new Person "Jane"
在这个例子中,name
参数是在 `constructor
领取专属 10元无门槛券
手把手带您无忧上云