简体   繁体   中英

Javascript: Get Name of Current Object Property in Object Literal

File this one under "just curious" or "is it possible?"

Say for example I have...

home: function(options) {
   this._home('home', options)
}

login: function(options) {
    this._home('login', options)
}

home and login are obviously identifiers on dozens of object properties for tracking purposes. Is there a way to just return home or login without using any variables (an external function call is fine) within the object property?

UPDATE: Turns out this isn't possible. The accepted answer doesn't exactly answer the question, but it is a wonderful example of simplifying numerous calls to the same property.

If you mean that within the function that the home property references you want to be able to somehow get the string "home" from that property name without hardcoding it then no, to the best of my knowledge that isn't possible.

Just guessing at what you're trying to achieve, would something like this help at least a little bit:

function callHome(propName) {
   return function(options) {
      this._home(propName, options);
   }
}

var someObj = {
   home: callHome('home'),
   login: callHome('login')
}
someObj.home({some:"option"});

At least then you don't have to repeat the same function body for each property. Demo: http://jsfiddle.net/EeEAw/

Note: I assume that the _home() function invoked but not defined in the question would be defined somewhere in the real-world code. I don't show it in my answer, though I created a dummy one in my fiddle.

Just as an aside, note that the function doesn't really "belong" to the object or to the property - there's nothing stopping you doing this sort of thing:

var obj = {
   test : "test",
   home : function() {
      alert(this.test);
   }
};
var funcRef = obj.home;
var obj2 = {
   method1 : funcRef
}
obj.home = null;
funcRef();
obj2.method1();

That is, you can create multiple references to the same function, and the function will continue to exist even if the original obj.home property is set to some other value (as long as the additional references continue to exist).

Identifier resolution and object property look up use completely separate methods. You can use with to add an object to the top of the scope chain, but it is very much recommended against and may well fail completely in strict mode.

So if you have:

var obj = {test:'test'};
alert(test);

then the identifier test is resolved on the scope chain. Since it doesn't exist there, an error is thrown. However, for:

alert(obj.test);

then firstly obj is resolved and found as a reference to an object, then the identifier test is resolved as a property of the object. You can also use with :

with (obj) {
  alert(test);
} 

in which case obj is temporarily placed at the top of the scope chain and so test is resolved on it first.

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