通過傳入的類屬性名獲取、修改對應的類屬性:
1 var label_set = ''; 2 var type_set = ''; 3 var labelOption = ''; 4 class Service_data{ 5 constructor() { 6 //固定屬性 7 this.conversationCount = {name:'會話數',type:type_set, label: label_set ,data: []}; 8 this.doubleConversationCount = {name: '兩人會話總數', type: type_set, label: label_set ,data: []}; 9 this.temporaryConversationCount = {name: '臨時多人會話', type: type_set, label: label_set ,data: []}; 10 this.groupConversationCount = {name: '群組會話總數', type: type_set , label: label_set , data: []}; 11 this.informationNumCount = {name: '資訊號總數', type: type_set , label: label_set , data: []}; 12 this.informationCount = {name: '資訊總數', type: type_set , label: label_set , data: []}; 13 this.informationReadCount = {name: '閱讀數', type: type_set , label: label_set,data: []}; 14 this.informationCommentCount = {name: '資訊評論數', type: type_set , label:label_set , data: []}; 15 this.informationPraiseCount = {name: '資訊點贊總數', type: type_set , label: label_set, data: []}; 16 this.colleaguesCount = {name: '同事圈總數', type: type_set , label: label_set,data: []}; 17 this.colleaguesCommentCount = {name: '同事圈評論總數', type: type_set , label:label_set, data: []}; 18 } 19 setdata(name,list){ 20 if(this.hasOwnProperty(name)){ 21 let save_data = Object.getOwnPropertyDescriptor(this,name).value; 22 save_data.data = list; 23 }else{ 24 console.log(name+"沒有對應屬性!"); 25 } 26 } 27 getdata(name){ 28 if(this.hasOwnProperty(name)){ 29 return Object.getOwnPropertyDescriptor(this,name).value; 30 }else{ 31 console.log(name+"沒有對應屬性!"); 32 } 33 } 34 } 35 var service_dataInit = new Service_data(); 36 var setdata_resule = service_dataInit.setdata('conversationCount',['1','2']); 37 var getdata_resule = service_dataInit.getdata('conversationCount'); 38 console.log(getdata_resule);
使用方法:
Object.getOwnPropertyDescriptor() 方法返回指定對象上一個自有屬性對應的屬性描述符。(自有屬性指的是直接賦予該對象的屬性,不需要從原型鏈上進行查找的屬性)
參數
-
obj - 需要查找的目標對象
-
prop - 目標對象內屬性名稱(String類型)
返回值
如果指定的屬性存在於對象上,則返回其屬性描述符對象(property descriptor),否則返回 undefined。
hasOwnProperty() 方法會返回一個布爾值,指示對象自身屬性中是否具有指定的屬性
參數
返回值
用來判斷某個對象是否含有指定的屬性的 Boolean 。
