javascript对象属性拼接

时间:2018-04-25 00:16:36   收藏:0   阅读:2461

对象方法里要通过一个变量来调用显示对象属于名称,可以这样做

var person = {
            borther1: { name: "tom" },
            borther2: { name: "jack" },
            borther3: { name: "david" },
            sayBorther: function (num) {
                return this["borther"+num]
            }
        }

我们知道person.borther1等同于person["borther"],

person["borther"]用字符串拼接的写法就是person["borther"+1]

应用到上述方法

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!