我想把this.换成@符号,就像咖啡脚本中的那样。我写了宏:
macro (@) {
case { return $a } => { return this.$a }
}
function LogSmth(name) {
this.name = name;
console.log(@name);
}但得到
SyntaxError: [macro] Macro `@` could not be matched with `name...`
57: console.log(@name);怎么解决这个问题?
发布于 2016-03-03 03:26:46
请允许我详述麦克·C的回答。如果我们尝试使用@来做一些事情(常见的操作是将一个对象绑定到this),会发生什么。有人可能会写:X.bind(@, ...),但是上面的宏会失败。另一种可能是这样做的能力:@['some property with a weird name'],但对于上面的宏,这也会失败。
这是我的版本:
macro @ {
rule { [$x:expr] } => { this[$x] }
rule { $x:ident } => { this.$x }
rule {} => { this }
}这也暴露了将规则应用于宏的一个有用属性,即顺序很重要。
发布于 2016-01-22 20:43:11
案例必须返回一个语法数组。因此,您可以通过以下操作来修复您的问题:
macro @ {
case { _ } => { return #{ this. } }
}或者,您可以使用一个不使用任何模式的简单规则来生成它。
macro @ {
rule {
} => {
this.
}
}https://stackoverflow.com/questions/34955525
复制相似问题