节点属性的设置器和获取器

时间:2018-08-13 10:58:55

标签: javascript getter-setter

我正在尝试在某个节点上设置吸气剂,因此当其id / className / src属性将被调用时,我的函数也将被调用,我的代码是:

function mapGenerate(){
    var map=createMap(); // this function returns a 2d array filled of random 1 and 0 

/* example 
map = [[1,1,1,1,0],
       [1,0,0,0,0],
       [1,0,1,1,1],
       [1,0,0,0,1],
       [1,1,1,0,1]]
*/    

        //loop the 2d array map and change the number with the appropriate img    
        for(var i = 0; i < map.length; i++) {
            var innerArrayLength = map[i].length;
            for(var j = 0; j<innerArrayLength; j++){
                //loop the nested arrays so i can change the 1 and the 0 with the appropriate img
                if(map[i][j] === 0){
                    map[i][j]="<img class=\"walkble\" src=\"mapTiles/floorResized.png\">"; //you can walk this part of the map
                }else{
                    map[i][j]="<img class=\"nonWalkble\"// you cannot walk This part of the map src=\"mapTiles/volcanoresize.png\">";
                }    
                ;
            }
            $("#tableGame").append("<tr><td>"+ map[i] + "</td></tr>") // Here i print the elements of the array
        }  
    }


mapGenerate();

当我调用node.className时,我确实获取了RealClass,但是当我尝试设置类时却什么也没有发生,为什么?我只定义了一个getter而不是一个setter ...,如果我还需要定义一个setter,我又该如何为该节点调用原始的setter,因为如果我将这样定义一个setter:

 let RealClass = node.className;
 Object.defineProperty(node, "className", {
   get(value) {
      //some more code
      return RealClass
   }
 });

它实际上会导致无限循环。想法?

0 个答案:

没有答案