indicating various properties of html objects

前端 未结 1 955
终归单人心
终归单人心 2021-01-24 12:14

I am going to set property of html objects.

var property1 = \'style.visibility\';
var property2 = \'style.display\';
var property3 = \'style\';

相关标签:
1条回答
  • 2021-01-24 12:55

    You can create function using reduce() to access nested properties.

    var property1 = 'style.visibility';
    var property2 = 'style.display';
    var property3 = 'style';
    
    var obj = {style: {visibility: 1, display: 2}}
    
    function getProp(prop, obj) {
      return prop.split('.').reduce(function(r, e) {
        return r[e]
      }, obj)
    }
    
    console.log(getProp(property1, obj))
    console.log(getProp(property2, obj))
    console.log(getProp(property3, obj))

    0 讨论(0)
提交回复
热议问题