简体   繁体   English

Javascript:获取Object Literal中当前对象属性的名称

[英]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. homelogin显然是许多对象属性的标识符,用于跟踪目的。 Is there a way to just return home or login without using any variables (an external function call is fine) within the object property? 有没有办法只返回homelogin而不使用任何变量(外部函数调用很好)在对象属性中?

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. 如果你的意思是在home属性引用的函数中你希望能够以某种方式从该属性名称获取字符串"home"而不对其进行硬编码,那么就不行,据我所知,这是不可能的。

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/ 演示: 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. 注意:我假设在实际代码中的某处定义了调用但未在问题中定义的_home()函数。 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). 也就是说,您可以创建对同一函数的多个引用,即使将原始obj.home属性设置为某个其他值(只要其他引用继续存在),该函数也将继续存在。

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. 您可以使用with将对象添加到作用域链的顶部,但建议不要使用它,并且可能在严格模式下完全失败。

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. 然后首先解析obj并将其作为对象的引用,然后将标识符测试解析为对象的属性。 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. 在这种情况下, obj暂时放在范围链的顶部,因此首先解决测试问题。

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

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