【问题标题】:How to find a property with a specified name and add a new property?如何查找具有指定名称的属性并添加新属性?
【发布时间】:2013-08-05 18:40:24
【问题描述】:

如何找到具有指定名称的属性并在所有者对象上附加一个新属性?

我有这样的数据结构:

var factoryOptions = {
            background: {
                src: 'images/background.png',
                width: 4800,
                height: 3200
            },
            player: {
                sprite: {
                    src: 'images/tanks/superTank.ss.png',
                    frameTime: 10,
                    frameCount: 3
                },
                width: 54,
                height: 32
            },
            tanks: {
                light: {
                    sprite: {
                        src: 'images/tanks/lightTank.png',
                        frameTime: 100,
                        frameCount: 1
                    },
                    width: 32,
                    height: 32
                },
                medium: {
                    sprite: {
                        src: 'images/tanks/mediumTank.png',
                        frameTime: 10,
                        frameCount: 1
                    },
                    width: 46,
                    height: 46
                },
                heavy: {
                    sprite: {
                        src: 'images/tanks/heavyTank.png',
                        frameTime: 10,
                        frameCount: 1
                    },
                    width: 64,
                    height: 64
                }
            }
        }
    }

我想找到所有属性“src”并修改所有者对象通过添加图像与这个src,所以最终的结果应该是这样的:

var factoryOptions = {
            background: {
                src: 'images/background.png',
                width: 4800,
                height: 3200,
                image: new Image()
            },
            player: {
                sprite: {
                    src: 'images/tanks/superTank.ss.png',
                    frameTime: 10,
                    frameCount: 3,
                    image: new Image()
                },
                width: 54,
                height: 32
            },
            tanks: {
                light: {
                    sprite: {
                        src: 'images/tanks/lightTank.png',
                        frameTime: 100,
                        frameCount: 1,
                        image: new Image()
                    },
                    width: 32,
                    height: 32
                },
                medium: {
                    sprite: {
                        src: 'images/tanks/mediumTank.png',
                        frameTime: 10,
                        frameCount: 1,
                        image: new Image()
                    },
                    width: 46,
                    height: 46
                },
                heavy: {
                    sprite: {
                        src: 'images/tanks/heavyTank.png',
                        image: new Image(),
                        frameTime: 10,
                        frameCount: 1
                    },
                    width: 64,
                    height: 64
                }
            }
        }
    }

【问题讨论】:

标签: javascript object properties


【解决方案1】:
function deepSrc(obj){
    for (var i in obj){
        if (i == 'src'){
            obj.image = new Image();
        }
        else if (typeof obj[i] == 'object'){
            deepSrc(obj[i]);
        }
    }
}
deepSrc(factoryOptions);

【讨论】:

    【解决方案2】:

    这可以解决问题:

    function addImg(obj) {
    
        if (obj.hasOwnProperty("src")) {
            obj.image = new Image();
        }
    
        for (prop in obj) {
            if (typeof obj[prop] === "object") {
                addImg(obj[prop]);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-18
      • 2020-09-01
      • 2020-04-05
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多