【发布时间】:2018-10-28 01:04:24
【问题描述】:
假设我有这样的代码:
var opts = {hello: "it's me", imusthavetried: "a thousand times"}
function myFunction (options) {
}
myFunction(opts)
有什么办法可以让myFunction 只写hello 而不是options.hello?我知道我可以遍历每个选项对象子对象并重新定义它们,但是有没有办法自动将选项对象用作函数的范围?
【问题讨论】:
-
“只能写 hello 而不是 options.hello”是什么意思?
-
with(options) {alert(hello);} -
你的意思是在
myFunction里面做console.log(options['hello'])?这应该记录'its me' -
@VincentNguyen 不,@NiettheDarkAbsol 回答了我的问题。我的意思是你可以在不写
options或显式地为hello赋值的情况下执行console.log(hello)。 -
@NiettheDarkAbsol 但如果
hello是在外部定义的,它会返回那个值,对吧?
标签: javascript object scope