我一直认为我们无法访问此处所述的同一对象中的 JS 对象文字值,
Access JavaScript Object Literal value in same object
但我遇到了this library这恰恰相反。这很可能是我遗漏的东西,但我无法弄清楚。
图书馆是这样做的, http://image.prntscr.com/image/2cd771f00f604b51be4b7befca49709e.png
而且它也没有使用“this”来访问“defaults”
编辑
似乎库的最新版本没有相同的代码。但是我正在看这个类(class)的练习文件,
https://app.pluralsight.com/library/courses/typescript/table-of-contents
最佳答案
您似乎混淆了对象属性和变量。
更多细节,让我们看看 lexical scoping example in MDN
The getOptions function has no local variables of its own. However, because inner functions have access to the variables of outer functions, getOptions() can access the variable default declared in the parent function
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}
init();
关于javascript - 访问同一对象字面量中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803024/