1

So I was wondering if there is a way to use function.name but for objects. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

What I mean by this is to have something like this:

function myFunction() {}

console.log(myFunction.name); // logs "myFunction"

// but like

var myObj = {};

console.log(myObj.name); // logs "myObj"

and if it is possible, how would it handle something like this?

var myObj = {};
var myObj2 = {};

console.log(myObj2.name); // logs "myObj" or "myObj2" or both?
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Julian Avar
  • 476
  • 1
  • 5
  • 24
  • 2
    No, there is not. Functions have names because it's part of the syntax of function instantiation. The same is not true for objects in general. – Pointy Jun 01 '16 at 13:57
  • In fact you're asking for an equivalent to `var x = function myFunction() {};` that would print `x`. – Álvaro González Jun 01 '16 at 14:07
  • I added a way to find it for global scope... – Akxe Jun 01 '16 at 14:07
  • @Pointy so can I add that "syntax of function instantiation" to objects in any way? – Julian Avar Jun 03 '16 at 00:05
  • @ÁlvaroGonzález yes – Julian Avar Jun 03 '16 at 00:05
  • No. You can add a "name" property to an object you create, but there is no intrinsic name of an object. – Pointy Jun 03 '16 at 00:06
  • If you would describe the actual problem you're trying to solve - like, what it is that you're trying to achieve in some code that leads you to want to do what you're asking - you might get more useful help. What you are asking is frankly weird, so you have probably made some incorrect assumptions or design decisions that have led you to this impasse. – Pointy Jun 03 '16 at 00:09
  • @Pointy I shall explain it here since I believe that it will be hard to do anything else since I have looked everywhere for a solution and this is all i could think off. I am trying to debug a program that was created using this question http://stackoverflow.com/questions/37356622/automated-nested-objects . I am making objects and somewhere along the way I get a `cannot read property` error. Of course I customized my own code based on the one given by the answer, and I think I did somewhere incorrect along the way, so I thonght that I could do this it would lead me to my problem. – Julian Avar Jun 03 '16 at 00:19
  • Well for debugging, you could use some well-known property name as a "standard" object name and assign that whenever you create an object, I guess. That seems like a mess but it might help. The browser debug tools are probably your best bet to tracking down the issue. The fact is that objects simply don't have any intrinsic "name" or visible identity if you don't give them one. – Pointy Jun 03 '16 at 00:31

2 Answers2

6

Two things

  1. Object don't have name property by default, unless you specify the same while defining the object,

for example

var myObj = {
  name : "myObj"
};
  1. In myObj2.name, myObj2 is not the name of the object, it is the name of reference (variable) to object.
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • This ain't a true answer... Yet a solution – Akxe Jun 01 '16 at 14:19
  • If this is true, how come functions have it, and if it's a built-in function that is doing this, how come I can't apply it to objects? I mean, functions are objects, right? – Julian Avar Jun 02 '16 at 16:55
  • Imagine you have array of persons you can just add name to it since it was there allready – Akxe Jun 02 '16 at 18:52
  • not understanding. What do the array and people symbolize? And yes, I can nickname them however I want, but they'll still have a name. The government knows exactly who is who, and they keep all of the records, so who in the js world can I ask to get their name, and if there is none, how can I make a system for all objects so I don't have to manually add their name, which is just ridiculously time-consuming. And since this is stack overflow, a system which works in any scenario would be best. – Julian Avar Jun 02 '16 at 23:47
2

Short answer? No.

On the other hand, as you may know, global variables are part of window variable. Therefore, you could do something like:

function findName(obj, scope){
    if(scope === void 0){
        scope = window
    }
    for (prop in scope) {
        if(scope.hasOwnProperty(prop) && scope[prop] == obj){
            return prop
        }
    }
}

NOTE: This is extremly inefficent way to get variable name!

Akxe
  • 9,694
  • 3
  • 36
  • 71
  • How would this respond to the second part of the question? If I were to reference the object with a variable name and I logged that reference, what would I get as output? – Julian Avar Jun 02 '16 at 16:53
  • The variable name, let's say `var bar = {foo:[]}; var a = bar.foo` and you do `findName(bar)` you'll get bar, as you wanted... I hope and you'll get `foo` if you do `findName(a, bar)` – Akxe Jun 02 '16 at 18:51
  • I read your code, and I see that technically speaking I could use any scope, not just `window`, how does it work, when would you call this function, since I'm kinda new to the language this seems a bit confuzling. Do I use it inside functions objects or inside the object I want to get the name of? Could you please explain? – Julian Avar Jun 02 '16 at 23:50
  • Yes I edited my code to be able to do it for any scope as long as you specify the scope in second parameter (if no scope is specified, the window scope is taken) and for the call, you can do it everywhere. PS: cops it and try it it is the best way to understand it ;) – Akxe Jun 03 '16 at 11:17