JS - Constructor referencing

2019-09-02 03:33发布

问题:

TLDR: How can you get the contents of a constructor/properties without actually knowing what it contains?

Given the following:

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

The defaults and ranges can be accessed through the myF.defaults.param1 and myF.ranges.param1.min calls. What if you do not know the name of the defaults or the ranges? How can you get the names and values within the defaults property without knowing them?

Example: Three of the above function; one like shown, F#2 with param37/param42 and F#3 with param68/param94. How can you call F#2 and return or find out it contains param37/param42 and their values?

NOTE: Obviously defining ranges without defining defaults would make no sense and be bad coding, which is why I angled the question towards getting the defaults and not both.

回答1:

How do I enumerate the properties of a JavaScript object?

The answers in here solved it and this was a duplicate by my own error. Thanks.