简体   繁体   English

如何在JavaScript中获得对象的名称?

[英]How do you get the name of an object in JavaScript?

If you have an object myObject , what function can you use to get its name? 如果您有一个对象myObject ,可以使用什么函数来获取其名称? I've tried stuff like myObject.name ... 我已经尝试过像myObject.name这样的东西...

You can't get the name of the variable that an object was stored in. In fact, multiple variables can refer to a single object (which one would be the "name"?), probably the very reason you asked. 您无法获取存储对象的变量的名称。实际上,多个变量可以引用一个对象(哪个将是“名称”?),这可能正是您要求的原因。

If it is important, I would suggest setting myObject.name when you create the object and then access it later, or perhaps add an additional parameter to your function that accepts the necessary information. 如果重要的话,我建议您在创建对象时设置myObject.name ,然后再访问它,或者在函数中添加一个接受必要信息的附加参数。

You can't. 你不能

Even if you could, which of the multiple variables that could be pointing at the object would you want? 即使可以,您想要指向该对象的多个变量中的哪个?

There was a similar question about this in PHP. 在PHP中也有类似的问题。 The short answer is "you can't", but the longer one is "you can to a certain extent". 简短的答案是“您不能”,而较长的答案是“您可以在一定程度上”。

Here's an example: 这是一个例子:

test1={};
test2={};
test1b=test1;

function findName(ref){
   for(var i in window)
      if(window[i]===ref)
         alert('Found: '+i);
}

findName(test1);

The result of the example would be two different popups one with 'test1' and another one with 'test1b'. 该示例的结果将是两个不同的弹出窗口,一个带有“ test1”,另一个带有“ test1b”。

Again, this is an example , no need for bashing about using global variables, etc... 再次,这是一个示例 ,无需费劲使用全局变量等。

Edit: Come to think of it, I'm pretty sure I needed something like this for debugging, and it seemed to work out nicely. 编辑:仔细想想,我很确定我需要像这样的东西进行调试,而且看起来效果很好。 But bear in mind it isn't something you should rely on. 但是请记住,这不是您应该依靠的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用JavaScript术语,您如何命名这种对象? - In JavaScript terms how do you name this kind of object? 您如何在javascript中动态命名和创建对象? - How do you dynamically name and create an object in javascript? 您如何在javascript中获取名称(文本)的值? - How do you get the value of a name (text) in javascript? 你如何在javascript映射对象中获取特定索引处的键? - How do you get the key at specifc index in javascript map object? 您如何通过键从 JavaScript 字典数组中获取 object? - How do you get an object from an array of JavaScript dicts by key? 如果类名称为字符串,如何在Javascript中创建对象? - How to do you create an object in Javascript if you have the class name as string? 在JavaScript中,如何从作为外部对象中数组元素的对象的方法到达外部对象的“ this”对象 - In JavaScript, how do you get to the 'this' object of the outer object, from a method of an object that's an element of an array in the outer object 如何通过Javascript中的名称获取对象? - How to get an object by the name in Javascript? 如何将{“name”:“myName”,“value”:“myValue”}对象的数组转换为“myName”的对象:JavaScript中的“myValue”对? - How do you convert an array of {“name”:“myName”,“value”:“myValue”} objects into an object of “myName”:“myValue” pairs in JavaScript? Javascript:创建对象时,如何设置myObject.name之类的内容? - Javascript: when creating an object, how do you set something like myObject.name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM