简体   繁体   中英

Get name of an Array or Object by Javascript

I have a quite dummy but confusing question. How can we get the name of an exist array or object ?

For example:

thisObject={ first:1, second:2};
thisArray=[1,2,3,4]

I want to get the string "thisObject", "thisArray".

How can we get it ?

Thanks a lot.


Edited:

For more specific. I want to do something like this: console.log(someFunction(thisObject))

then it return

"thisObject"


Edited-2:

const firstArray=[1,2,3] 
const secondArray=["a","b"] 
const render=(arr)=>arr.map(arrr=>console.log(Object.keys({arr})[0]))
render(firstArray)
render(secondArray)

it will return

"arr" "arr" 

Instead of

"firstArray" "secondArray"

You can't actually accomplish what you're trying to do in Javascript, but there's a minor little trick that you can use to log an object's name without directly typing it. It's not particularly useful, and won't really work if you're trying to get the original name of a function argument, but you can do:

console.log(Object.keys({thisObject})[0]);
// "thisObject"

As I said, not particularly useful, but I'll be shocked if you can do any better.

You can use window object to access thisObject and thisArray

Like this -

 var thisObject={ first:1, second:2}; var thisArray=[1,2,3,4] console.log(window.hasOwnProperty("thisObject")); console.log(window.hasOwnProperty("thisArray")); console.log(window.thisObject); console.log(window["thisArray"]);

In javascript the variables reference objects, but the objects themselves aren't named. So given an object, you can't really ask it for it's name; it doesn't make sense. When you think about how references are passed around in javascript you realize how problematic what you are attempting is in a general case. For example consider this:

var a = {first_name: "Mark"};
var b = a;
// a and b both point to the same object

If I ask for the name of that object, should it return a , b , or both? What about now:

var a = {first_name: "Mark"};
var b = a;
a = undefined;

This is even more complicated when you consider that the same object can be referenced by names in different scopes and modules. For example:

var a = {first_name: "Mark"};
function test(obj) {
   var t = obj;
   getNames(obj) // <-- what should that return? a? obj? t? all of them?
}

Give a particular scope, you could iterate through the names and find the ones that equal your object, but this is going to be fragile and probably inefficient .

 var a = {first_name: "Mark"} var b = a var c = {foo: "bar"} function getNames(obj, scope) { for (name in scope){ try { if (scope[name] === obj) console.log(name) } catch(e){continue} // avoid localStorage security error } } getNames(b, this) // should log 'a' and 'b' because both are in global scope

But again the above code does not really give you the name of the object, it just looks at every name in a particular object (in this case the global object) and and sees which of the object's properties point to the object in question.

Of course not all objects that are in scope even have names. For example::

let arr = [{foo: "bar"}, {foo: "baz"}]
getNames(arr[0]) // <-- what should that return?

If the name of the object is important and you want to display it, you should add a name property to the object and use it instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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