如何在javascript中获取对象的名称? [英] how to get object's name in javascript?

查看:129
本文介绍了如何在javascript中获取对象的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个这样的对象:

For example I have such an object:

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

和这样的代码:

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

如何获取函数体内的对象名称.所以我的意思是我应该知道obj的名字是'dark'.

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

我已经尝试使用Firebug检查对象,但是它仅显示对象的属性.它没有显示一些我可以知道的内部方法或属性

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

谢谢.

推荐答案

您不能.您只是将对象{ black : 'bad', gray : 'not so bad' }传递到test中.该对象本质上不具有名称"dark",它只是一个对象,恰好以对象a的属性dark的形式存在.将这些信息传递给函数时,这些信息将不可避免地丢失.

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 dark of the object a. 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.

这篇关于如何在javascript中获取对象的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆