我无法处理Google Apps脚本中的对象属性

时间:2018-11-01 15:30:29

标签: object google-apps-script

我正在尝试在Google Apps脚本中创建对象,但无法处理它们的属性。在下面的代码中,我不明白为什么我的构建功能不起作用,然后手册也没有设置

var my_object = Object.create(null,{
    type:     {value:"abc", enumerable:true},
    name:     {value:"abc", enumerable:true},

    build: {
      value:function(my_type,my_name){
        this.type =     my_type;
        this.name =     my_name;
        return this;
      }
    }
  });

  var my_type = "my type";
  var my_new_object = Object.create(my_object).build(my_type, "my name");

  // In my_new_object I found the "abc" from definition but not the values from my_range and "my name"
  console.log(my_type);      
  console.log(my_new_object.type);
  console.log(my_new_object.name);

  // And I don't succeed to set directly the properties neither
  my_new_object.type = "my type";
  my_new_object.name = "my name";      
  console.log(my_new_object.type);
  console.log(my_new_object.name);

  // I always get the "abc" from object definition

我需要特殊的setter和getter来处理对象属性吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

将可写属性添加到对象中

  type:     {value:"abc", enumerable:true, writable: true},
  name:     {value:"abc", enumerable:true, writable: true},

我很好奇,为什么不使用简单的方法呢?

function MyObject(type,name) {
  this.type = type;
  this.name = name;
}

var my_object = new MyObject("my_type","my_name");