当一个 {{this-as}} 表达式被放入 "作用域函数" 中,例如在 {{let}}-绑定中,通过 {{this-as}} 绑定的值指向作用域函数,而不是外部作用域。
示例
`
(def foo
(js-obj
"bar" "baz"
"getBarRight" (fn [] (this-as self (.-bar self)))
"getBarWrong" (fn []
(let [bar (this-as self (.-bar self))]
bar))))
(.log js/console (.getBarRight foo)) ;; => "baz"
(.log js/console (.getBarWrong foo)) ;; => undefined
`
而 {{foo.getBarRight}} 扩展为类似以下内容
function() { var self = this; // this refers to foo return self.bar; // 返回 "bar" }
另一方面,{{foo.getBarWrong}} 扩展为
`
function() {
var bar = function() {
var self = this; // this refers to enclosing function
return self.bar; // returns undefined
}();
return bar; // 返回 undefined
}
`