JavaScript:访问数组文字中自己的对象属性

发布于 2024-09-11 11:53:10 字数 633 浏览 10 评论 0原文

给定 JavaScript 对象内的数组文字,访问其自身对象的属性似乎不起作用:

 var closure =  {

         myPic : document.getElementById('pic1'),
         picArray: [this.myPic]
 }    

 alert(closure.picArray[0]); // alerts [undefined]


而通过访问其他 JavaScript 对象来声明数组项似乎可行

 ​var closure1 = {
 ​    
 ​     myPic : document.getElementById('pic1')
 ​}
 ​    
 ​var closure2 =  {
 ​  
 ​        picArray: [closure1.myPic]
 ​}    
 ​    
 ​alert(closure2.picArray[0]); // alerts [object HTMLDivElement]


例子: http://jsfiddle.net/5pmDG/

Given an Array Literal inside a JavaScript Object, accessing its own object's properties does not seem to work:

 var closure =  {

         myPic : document.getElementById('pic1'),
         picArray: [this.myPic]
 }    

 alert(closure.picArray[0]); // alerts [undefined]

Whereas declaring an Array Item by accessing an other JavaScript Object seem to work

 ​var closure1 = {
 ​    
 ​     myPic : document.getElementById('pic1')
 ​}
 ​    
 ​var closure2 =  {
 ​  
 ​        picArray: [closure1.myPic]
 ​}    
 ​    
 ​alert(closure2.picArray[0]); // alerts [object HTMLDivElement]

Example:
http://jsfiddle.net/5pmDG/

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

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

发布评论

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

评论(2

隔岸观火 2024-09-18 11:53:10

this 值不会那样工作,它指的是由实际执行上下文确定的值,而不是对象字面量。

例如,如果您声明对象的函数成员,则可以获得所需的结果:

var closure =  {
  myPic: document.getElementById('pic1'),
  getPicArray: function () {
    return [this.myPic];
  }
};
//...
closure.getPicArray();

因为 getPicArray 函数内的 this 值将引用您的 闭包对象。

请参阅另一个问题的此答案,其中我解释了 this 关键字。

编辑:为了回应您的评论,在我提供的示例中,getPicArray 方法将在每次调用时生成一个新的 Array 对象,并且因为您想要存储数组并对其进行更改,我建议您这样做,分两步构建对象:

var closure =  {
  myPic: document.getElementById('pic1')
};
closure.picArray = [closure.myPic];

然后您可以毫无问题地修改 closure.picArray 成员。

The this value will not work like that, it refers to a value determined by the actual execution context, not to your object literal.

If you declare a function member of your object for example, you could get the desired result:

var closure =  {
  myPic: document.getElementById('pic1'),
  getPicArray: function () {
    return [this.myPic];
  }
};
//...
closure.getPicArray();

Since the this value, inside the getPicArray function, will refer to your closure object.

See this answer to another question, where I explain the behavior of the this keyword.

Edit: In response to your comment, in the example that I've provided, the getPicArray method will generate a new Array object each time it is invoked, and since you are wanting to store the array and make changes to it, I would recommend you something like this, construct your object in two steps:

var closure =  {
  myPic: document.getElementById('pic1')
};
closure.picArray = [closure.myPic];

Then you can modify the closure.picArray member without problems.

时光无声 2024-09-18 11:53:10

this 属性不指向 closure 对象。如果我们要在全局范围内定义 myPic 属性,那么您将看到使用该值初始化的 picArray。考虑这个例子:

<script>
window.myPic = "Global myPic";

var closure =  {
    myPic : document.getElementById('pic1'),
    picArray: [this.myPic] // this refers to the global object
};

console.log(closure.picArray); // ["Global myPic"];
</script>

this 是 JavaScript 中最难的概念之一。您可能会喜欢这篇关于该主题的文章

The this property does not point to the closure object. If we were to define the myPic property on the global scope, then you will see picArray initialized with that value. Consider this example:

<script>
window.myPic = "Global myPic";

var closure =  {
    myPic : document.getElementById('pic1'),
    picArray: [this.myPic] // this refers to the global object
};

console.log(closure.picArray); // ["Global myPic"];
</script>

this is one of the hardest concepts in JavaScript. You might like this article on the topic.

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