Is there a means of "mirroring" properties of one object into another?

Asked

Viewed 112 times

2

Say I want to do a function debugText(obj), this function takes an object as a parameter and shows a property of it on the screen, such as debug game screens.

Something like:

function debugText(link,prop) {

  this.TextObject = new Engine.Utils.Text()

  this.link = link
  this.prop = prop

  this.update = function () {
    this.TextObject.setText( this.link[this.prop] )
  }

}

In use:

var texto1 = new debugText(Player,"speedX")
var texto2 = new debugText(Missle,"positionY")

It works great for these two examples and when I need to know other things when testing the code, more if I want to access something like:

Player.skin.currentAnimation.frame.frameInfo.index?

I could add more parameters to the function and then see how many of them are being used and create the this.update based on that.

However this function will always be limited there is a fixed number of properties and even if I will never use more than 5 I still do not like how this function works: http://pastebin.com/WT6VYE5

EDIT

The question is: has a correct means of "mirroring" the properties of an object or updating them with a Function call independent of how many depth levels Object.prop1.prop2.pro3... that has to update?

For you to know how many ways I’ve thought of how to do this is the last

debugText(Player.skin.currentAnimation.frame.frameInfo,"index")

This also works BUT it really is certain to pass an object with the properties and pass only the last property as string only to use the computed Property Names to access this value?

1 answer

3


A suggestion:

var obj = {
    foo: 12345,
    bar: {
        teste: {
            aninhado: {
                chave: 6789
            }
        }
    }
};

function mostrarProps(obj, props) {
    var valores = props.map(function(prop) {
        if (typeof prop == 'string') return obj[prop];
        // else...
        var key, val = obj;
        while (key = prop.shift()){
            val = val[key];
        }
        return val;
    });
    document.body.innerHTML = valores.join('<br>');
}

mostrarProps(obj, ['foo', ['bar', 'teste', 'aninhado', 'chave']]);

The idea is to have a function that accepts an array of properties to search for. In this array you can have _String_s to directly access properties on a first level, or an array with "the path" to go until you reach the value you want.

jsFiddle: https://jsfiddle.net/ekk2772e/

However:

I think it would be simpler to use console.log() to know the values of objects if debug, or even the JSON.stringify() which transforms the object into a string and thus creates a static image of how the object was at a given time.

  • I can’t use the console.log() because sometimes I have to show more than 5 things with updated values once per frame and this besides flodar the conosole, ends up lagging a little. Now JSON.stringfy really can help me in this case. Thanks man, it would take a long time to think about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.