JS - 物业名称访问 [英] JS - Property Name Access

查看:165
本文介绍了JS - 物业名称访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously发布一个版本的这个问题( JS - 构造函数引用),并发现出了副本中的答案,尤其是<一个href=\"http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json\">Access /过程(嵌套)对象,数组或JSON 。从这里,我仍然有时间一个荒谬的量得到这个预期那样发挥作用。

I previously posted a version of this question (JS - Constructor referencing) and found out the answer within the duplicates, especially Access / process (nested) objects, arrays or JSON. From here, I'm still having a ridiculous amount of time getting this to work as intended.

对于以下示例code:

Given the following example code:

    function f1(args) {
        this.defaults = {
            param1 : 100,
            param2 : 900;
        }
        this.ranges = {
            param1 : { min : 0, max : 500 },
            param2 : { min : 0, max : 1000 };
        }
        this.steps = {
            param1 : 50,
            param2 : 100;
        }
    var myF1 = new f1();

我在previous重复阅读和学习通过访问未知属性名称:

I've read up on the previous duplicates and learning accessing the unknown property names by:

    for (var propertyName in myF) {
        newName = propertyName;
    }

我需要做的是通过函数周期(或任何功能)和功能构建HTML5范围输入(请记住,参数名会大大不同,或者随机的,所以我想我需要循环的。私钥本身因此,在 F1 / myF1 的情况下,所产生的HTML5将是:

What I need to do is cycle through the function (or any function) and build HTML5 range inputs for the function (keep in mind that the parameter names will be greatly different or random, so I assume I need to loop for the key itself. So, in the case of f1/myF1, the generated HTML5 would be:

    param1: <input type="range" name="param1" min="0" max="500" steps="50" />
    param2: <input type="range" name="param2" min="0" max="1000" steps="100" />

注:为范围=值,名称=,=最小,最大和= =步都动态地从函数的性质产生

我试过循环钥匙,呼吁价值观和pretty很多其他的研究和尝试,我可以,但我总是变量覆盖自己结束了,我猜这只是在深水的一端更比这个业余游泳者能搞清楚。

I've tried looping for keys, calling for values and pretty much any other research and attempt I can, but I always end up with variables overwriting themselves and I'm guessing this is just more in the deep end than this amateur swimmer can figure out.

此外,意识到每个参数将有一个默认范围步骤,所以它不是像我们担心的随机或更改这一水平。谢谢SO-JS大师!

Also, realize that each parameter will have a defaults, ranges and steps, so it is not like we have to worry about THAT level of randomization or variation. Thank you SO-JS gurus!

推荐答案

它看起来像你宁愿迭代的特性 myF1.defaults myF1.ranges myF1.steps ,不超过 myF1 本身(因为它们是固定的)。

It looks like you would rather iterate over the properties of myF1.defaults, myF1.ranges or myF1.steps, not over the properties of myF1 itself (because they are fixed).

我不知道你已经尝试了什么,但是这是你如何创建所需的DOM元素:

I don't know what you have tried, but this is how you could create the DOM elements you want:

var myF1 = new f1();
var elements = [];

for (var param in myF1.ranges) {
    var input = document.createElement('input');
    input.type = 'range';
    input.name = param;
    input.min = myF1.ranges[param].min;
    input.max = myF1.ranges[param].max;
    input.step = myF1.steps[param];
    input.value = myF1.defaults[param];

    var span = document.createElement('span');
    span.appendChild(document.createTextNode(param + ': '));
    span.appendChild(input);
    elements.push(span);
}

简单地在的属性迭代范围并访问步骤相同的属性默认值。然后,你可以做任何你想要与元素 DEMO

Simply iterate over the properties of ranges and access the same properties of steps and defaults. Then you can do whatever you want with elements. DEMO

这篇关于JS - 物业名称访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆