需要帮助获取 JavaScript 中的变量

发布于 2024-08-15 21:14:21 字数 460 浏览 4 评论 0原文

有人可以帮助我了解 JavaScript 变量的范围以及如何访问它们吗?

想象一下以下情况...

// Namespace declaration
var p = {};

p.result = {

    searchForm: $('#search-form'),
    searchAction: this.searchForm.attr('action'),

    anotherSection: {
        hello: 'Hello ',
        world: this.hello + 'world!'
    }

}

这不会起作用,并且会给出错误,指出 this.searchForm 未定义。同样的错误也出现在 anotherSection 中 (ofc)。

如何根据同一命名空间内的另一个变量来声明一个变量?

Can someone please help me understand the scoop of JavaScript variables, and how to reach them?

Imagine the following...

// Namespace declaration
var p = {};

p.result = {

    searchForm: $('#search-form'),
    searchAction: this.searchForm.attr('action'),

    anotherSection: {
        hello: 'Hello ',
        world: this.hello + 'world!'
    }

}

This won't work, and it gives the error saying this.searchForm is undefined. The same error appears in anotherSection as well (ofc).

How can I declare a variable in terms of another variable inside the same namespace?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宛菡 2024-08-22 21:14:21

this 关键字绑定到函数上下文,您不能在这样的对象文字中使用它。

你可以将函数设置为“getters”:

var p = {};
p.result = {
  searchForm: $('#search-form'),
  getSearchAction: function () {
    return this.searchForm.attr('action');
  },
  anotherSection: {
    hello: 'Hello ',
    getWorld: function () {
      return this.hello + 'world!';
    }
  }
};

The this keyword is bound to the function-context, you can't use it in object literals like that.

You could, make functions as "getters":

var p = {};
p.result = {
  searchForm: $('#search-form'),
  getSearchAction: function () {
    return this.searchForm.attr('action');
  },
  anotherSection: {
    hello: 'Hello ',
    getWorld: function () {
      return this.hello + 'world!';
    }
  }
};
雨的味道风的声音 2024-08-22 21:14:21

在构建对象时无法引用它。

您需要编写以下内容:

p.result = {
    searchForm: $('#search-form'),
    anotherSection: {
        hello: 'Hello '
    }
}

p.result.searchAction = p.result.searchForm.attr('action');
p.result.anotherSection.world = p.result.anotherSection.hello + 'world!';

There is no way to refer to an object literal as you're building it.

You need to write the following:

p.result = {
    searchForm: $('#search-form'),
    anotherSection: {
        hello: 'Hello '
    }
}

p.result.searchAction = p.result.searchForm.attr('action');
p.result.anotherSection.world = p.result.anotherSection.hello + 'world!';
凯凯我们等你回来 2024-08-22 21:14:21

您无法访问对象本身文字中的属性 - 它们还不存在。

You can't access the properties of an object inside its own literal — they don't exist yet.

西瑶 2024-08-22 21:14:21

this 是一个对象,因此当您使用 this.searchForm 时,您正在访问 this 对象的属性。您只想访问一个变量,因此可以使用searchForm

this is an object, so when you use this.searchForm, you are accessing a property of the this object. You just want to access a variable, so you can just use searchForm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文