javascript如何管理对象属性名称作为动态更改的对象?(How javascript is managing Object property name as Object which chang

编程入门 行业动态 更新时间:2024-10-27 18:21:43
javascript如何管理对象属性名称作为动态更改的对象?(How javascript is managing Object property name as Object which changes dynamically?)

我正在查看关于MDN的文档,发现Object的属性名称可以是Object(它将被转换为字符串)。 我写了一个简单的代码来测试它并且它工作但稍后更改被指定为主Object的键的obj应该更改结果键(字符串表示)因此主对象属性不应该可以使用新对象访问但是它是可以访问的。

我想知道它如何管理这个javascript?

var objee = {};
var rajat = "me";
var rand = Math.random();
var ob = new Object();

objee[ob] = "hey";


console.log(ob,objee[ob]);//javascript is converting the Object key to string using Object.toString().

ob.name = "rajat";

// Why this property is still accesible if the "ob" has changed now..and resulted string is changed,thus key changed?

console.log(ob,objee[ob]); 
  
 

我知道这个问题很混乱。希望你能理解。

I was looking in docs on MDN and found that Object poperty names can be Object(which will be converted to string). I wrote a simple code to test it out and it worked but later changing the obj which is being assigned as a key to main Object,should change the resulted key (string representation) thus the Main object property should not be accesible with new object but It is accesible.

I wanna know how javascript it managing this?

var objee = {};
var rajat = "me";
var rand = Math.random();
var ob = new Object();

objee[ob] = "hey";


console.log(ob,objee[ob]);//javascript is converting the Object key to string using Object.toString().

ob.name = "rajat";

// Why this property is still accesible if the "ob" has changed now..and resulted string is changed,thus key changed?

console.log(ob,objee[ob]); 
  
 

I know this is question is very confusing..Hope you will understand.

最满意答案

该对象可能已更改,但其字符串表示形式不具有:

var ob = new Object();
console.log(ob.toString());
ob.name = "foo";
console.log(ob.toString()); 
  
 

...所以它引用的属性名称也不会改变。

The object might have changed, but its string representation has not:

var ob = new Object();
console.log(ob.toString());
ob.name = "foo";
console.log(ob.toString()); 
  
 

… so the property name being referenced by it is also unchanged.

更多推荐

本文发布于:2023-08-05 20:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   属性   名称   动态   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!