Javascript 如何在javascript中获取对象的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5448135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:14:23  来源:igfitidea点击:

how to get object's name in javascript?

javascriptobject

提问by Larry Cinnabar

For example I have such an object:

例如我有这样一个对象:

var a = {
    'light': 'good',
    'dark' : {
        'black': 'bad',
        'gray' : 'not so bad'
    }
}

and such a code:

和这样的代码:

var test = function(obj) {
    // do smth with object
    // I need to get obj's name ('dark' in my way)
}
test(a.dark);

How to get name of object in function's body. So I mean I should know that obj's name is 'dark'.

如何在函数体中获取对象的名称。所以我的意思是我应该知道 obj 的名字是“dark”。

I've tried inspect object with firebug, but it's only show object's property. It's not showing some internal methods or properties, with which I'll be able to know

我试过用萤火虫检查对象,但它只显示对象的属性。它没有显示一些内部方法或属性,我将能够知道

Thank you.

谢谢你。

回答by deceze

You can't. You're only passing the object { black : 'bad', gray : 'not so bad' }into test. This object does not intrinsically have the name "dark", it's just an object that happened to exist as the property darkof the object a. This information is irretrievably lost when passing it into a function.

你不能。您只是将对象传递{ black : 'bad', gray : 'not so bad' }test. 这个对象本质上并没有“暗”这个名字,它只是一个碰巧作为dark对象属性存在的对象a。将其传递给函数时,此信息将不可挽回地丢失。

You're basically trying to retrieve the variable name that held the value before the value got passed into the function. That's not possible.

您基本上是在尝试在值传递到函数之前检索保存该值的变量名称。那是不可能的。

回答by Frunsi

The "name of an object" is no intrinsic property of an object. A "name" is name in a given context. When you pass an object to a function, then you just pass that object, not the context in which it was named ("dark" in your example).

“对象的名称”不是对象的固有属性。“名称”是给定上下文中的名称。当您将一个对象传递给一个函数时,您只需传递该对象,而不是它被命名的上下文(在您的示例中为“dark”)。

Whatever you want to accomplish, you are on the wrong track.

无论你想完成什么,你都走错了轨道。

回答by KooiInc

I would like to point you to the possibility to iterate through an object and recursively find the name of the parent of some property. With it your testfunction would look like:

我想向您指出遍历对象并递归查找某些属性的父级名称的可能性。有了它,您的test功能将如下所示:

var test = function(rootobj,propname,rootObjName) {
    // do smth with object AS rootobj[propname]
    var objparents = findParents(rootobj,propname,rootObjName);
}
test(a,'dark','a');

Where findParentsis:

在哪里findParents

function findParents(obj,key,rootName){
 var parentName = rootname || 'ROOT', result = [];
 function iterate(obj, doIndent){
  var parentPrevName = ''
  for (var property in obj) {
    if (obj.hasOwnProperty(property)){

        if (obj[property].constructor === Object) {
           parentPrevName = parentName;
           parentName = property;
           iterate(obj[property]);
           parentName = parentPrevName;
        }
        if (key === property) {
                result.push('Found parent for key ['
                             +key+' (value: '+obj[property]
                             +')] => '+parentName);
        }

    }
  }
 }
 iterate(obj);
 return result;
}

The problem of course is that a property wouldn't have to be unique. As in:

问题当然是属性不必是唯一的。如:

var a = 
{
    'light': 'good',
    'dark' : {
        'black': 'bad',
        'gray' : 'not so bad'
        'yellow' : {
                     'dark': 'will do', //<=there's 'dark' again!
                     'light':'never use'
                   }
    }
}

Well, may be it's usable. You can find a demo of the findParents function in http://jsfiddle.net/KooiInc/Kj2b9/

好吧,也许它是可用的。您可以在http://jsfiddle.net/KooiInc/Kj2b9/ 中找到 findParents 函数的演示

回答by Frederik.L

It is possible with :

有可能:

function loadProps(obj, container) {
    for (var p in obj) {
        container.push(p);
        if (obj[p].constructor == Object) {
            loadProps(obj[p], container);
        }
    }
}

then :

然后 :

var props = [];
loadProps(a, props);

console.log( props.join(",") );

回答by Sakul Budhathoki

var a = { name:'a', 'light': 'good', 'dark' : { name: 'dark', 'black': 'bad', 'gray' : 'not so bad' } }

var a = { name:'a', 'light': 'good', 'dark' : { name: 'dark', 'black': 'bad', 'gray' : 'not so bad' } }

This way you can do

这样你就可以做到

console.log(a.name,a.dark.name);