简体   繁体   English

JavaScript-如何获取函数内部键的名称

[英]JavaScript - How to get the name of the key inside a function

I have a javascript literal object as shown below.. 我有一个JavaScript文字对象,如下所示。

var db = new Observer();
var user = {
   firstName: db.observe("abc"),
   lastName: "xyz",
   middleName: db.observe("test")
};

NOTE: the "firstName" value is a function call db.observe("abc") which takes a parameter. 注意:“ firstName”值是带有参数的函数调用db.observe(“ abc”)。

var Observer = function() {
    this.observe = function (value) {  // INITIAL value of the field
        // HOW WILL I GET "key, for e.g. firstName" key here so that i can associate "value" with it.

    return this;
    }
}

My requirement is to get the name of the key, in this case 'firstName" in the observe() function. 我的要求是获取键的名称,在这种情况下,它是observe()函数中的“ firstName”。

OR Please feel free to recommend alternatives to achieve the same. 或请随时推荐替代方案以达到相同的效果。

NOTE: This is related to a small MVVM framework which I am experimenting with and am stuck at this point. 注意:这与我正在试验的小型MVVM框架有关,目前仍停留在该框架上。

Let me know whether this is possible with JS. 让我知道JS是否可行。

REF: knockoutjs does something like this... http://knockoutjs.com/examples/helloWorld.html 参考:knockoutjs会执行以下操作... http://knockoutjs.com/examples/helloWorld.html

You have to provide that type of information to the interface of the function: 您必须将该类型的信息提供给函数的接口:

var observation = db.observe("abc");
var other_observation = db.observe("test");
var user = {
   firstName: ( todayIsFriday ? observation : other_observation ),
   lastName: "xyz",
   middleName: ( todayIsFriday ? other_observation : observation )
};

You can't really expect the parser to know what to use as "key" here in the observe function, you'd basically need to implement a time machine in Javascript first. 您不能真正期望解析器知道在observe函数中用作“键”的东西,基本上您首先需要在Javascript中实现时间机器。

So, you really need to pass it as an extra parameter, or put your db object in a sort of "state" first. 因此,您确实需要将其作为附加参数传递,或者首先将db对象置于某种“状态”。

you can achive by for..in . 您可以通过for..in实现。 see the demo below 请参阅下面的演示

http://jsfiddle.net/3SwyM/2/ http://jsfiddle.net/3SwyM/2/

although its works on static names, but if your observe() works well then you can achieve the result also 尽管它适用于静态名称,但是如果您的observe()效果很好,那么您也可以实现结果

Answering my own question as i just got a my sample ready. 我准备好样品后回答我自己的问题。 Thanks to Yoshi's comment, which probed me to dig deeper.. 感谢Yoshi的评论,这使我更加深入。

I am posting the link to sample implementation here, if anyone's interested... The code is buggy but demonstrated data binding which I was trying to do POC... 如果有人感兴趣,我会在此处发布示例实现的链接。该代码有错误,但是演示了我试图进行POC的数据绑定...

Here is the jsfiddle URL... 这是jsfiddle URL ...

http://jsfiddle.net/rajeshpillai/xQkXk/22/ http://jsfiddle.net/rajeshpillai/xQkXk/22/

Try changing the value in the textbox, the dependent objects are automatically updated.... 尝试更改文本框中的值,相关对象将自动更新。

Thanks all for the comment. 谢谢大家的评论。 This is just raw/buggy, code.. Will polish this over the next couple of days... 这只是原始的/笨拙的代码。.将在接下来的几天里进行完善...

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

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