proxy and defineProperty difference of vue

Object.defineProperty(obj,"name",{
  set:function(val){ if(var==='lisi'){ console.log("誓死不叫这么土的名字") }else{ objCopy.name = val } }, get:function(){ return objCopy.name.replace(/san/,'先生') } })
这个对每个data中的属性进行遍历绑定。
而,
var objCopy = new Proxy(obj,{
          get:function(target,key){ if(key=='name'){ return target[key].replace(/san/,'先生'); } }, set:function(target,key,value){ if(key == 'name'){ value=='lisi'?target[key]:target[key] = value; }else{ target[key] = value; } } })

get, target process set parameters i.e. the object obj, key attribute parameter is to be operated, value is a parameter value assignment operation.
Thereafter, accessible by way of obj objCopy.name name attribute may be provided by a name attribute of obj objCopy.name = 'lisi' manner, and do not set for each attribute set, get method, it will not trigger the cycle calls, very cool.
We objCopy through the operation of the object on the realization of the operation of the object obj, objCopy object is a proxy object obj object .
vue3.0 used to replace the original Proxy traversal object using Object.defineProperty method to add attribute set, clumsy practice get accessor.
 That should not be traversed, but directly monitoring the data objects.

Guess you like

Origin www.cnblogs.com/sweeeper/p/11104535.html