简体   繁体   English

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

[英]how to get object's name in javascript?

For example I have such an object:例如我有这样一个 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.如何在函数体中获取 object 的名称。 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.我试过用萤火虫检查 object,但它只显示对象的属性。 It's not showing some internal methods or properties, with which I'll be able to know它没有显示一些我可以知道的内部方法或属性

Thank you.谢谢你。

You can't. 你不能。 You're only passing the object { black : 'bad', gray : 'not so bad' } into test . 你只是将对象{ black : 'bad', gray : 'not so bad' }传递给test This object does not intrinsically have the name "dark", it's just an object that happened to exist as the property dark of the object a . 这个对象本质上没有名称“dark”,它只是一个碰巧作为对象a的属性dark存在的对象。 This information is irretrievably lost when passing it into a function. 将此信息传递给函数时,此信息将无法挽回地丢失。

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. 那是不可能的。

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(",") );

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. 无论你想做什么,你都走错了路。

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 test function 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 findParents is: 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函数的演示

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

这样你就可以做到

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

This might be a little silly approach, but you can include a new key with the value of the Object Name.这可能有点愚蠢,但您可以包含一个新键,其值为 Object 名称。

obj = {objName: "obj"}

It worked for me.它对我有用。 Let me know if that helps.让我知道这是否有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM