javascript - 访问同一对象字面量中的属性

标签 javascript

我一直认为我们无法访问此处所述的同一对象中的 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

最佳答案

您似乎混淆了对象属性和变量。

Default 是变量而不是对象属性。 enter image description here

更多细节,让我们看看 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/

相关文章:

javascript - 如何正确检查输入是否不为零

javascript - 是否可以从父窗口绑定(bind)到子窗口上的事件?

javascript - 添加新按钮时 jQuery 单击事件未触发

javascript - 在 Javascript 中抓取表格元素

javascript - Chrome 扩展程序会重定向每一帧

javascript - knockout 无效 : Value from external Json file

javascript - JSF 在 f :ajax 之后执行 javascript

javascript - onload后加pre标签不生效

javascript - 将通用逻辑提取到 JavaScript 中的可重用函数中

javascript - 在 Dexie 中调用后已与 IndexedDB 数据库建立连接