Object.defineProperty

Object.defindPorperty can limit the change of properties, monitor the assignment and acquisition of properties, vue2 is based on this feature to synchronize data to the view, so vue2 will not support ie8

 

 

/*
es5 adds
var obj Object.create(prototype, [object])
The first parameter is the prototype of the created object: that is, obj.__proto__=prototype
The second parameter is to add properties to the object: {
height: {
value: "xxx",//Value
writable:"false",//Writable
enumerable:"false",//Enumerable
configurable:"false",//Configurable, false means that it cannot be deleted, and it is not You can change the properties through Object.defineProperty, except for value
}
} The default is all false, that is, it cannot be enumerated, cannot be written, and can be configured and deleted through Object.defineProperty.

 

// es3 has ie 8 but cannot use
Object.defineProperty(obj,propertyName,config)
1. Object to be changed
2. Property name
3. Configuration {
value:"xxx",//value
writable:"false",/ /Writable
enumerable:"false",//Enumerable
configurable:"false",//Configurable, false means that you can't delete, and you can't change properties through Object.defineProperty, except for value
get:function (){}
set:function(v){} // ie9+ support
}

*/

 

/* var pepole = {name:"muyi",age:24};
var muyi = Object.create(pepole,{height:{value:175,enumerable:"true"}});
muyi.height=178;
//delete muyi.height;
for (var i in muyi)
{

console.log(i+" "+muyi[i]);


}
*/
// Object.defineProperty(muyi,"height",{
// value:175
// })
// for (var i in muyi)
// {
// if(muyi.hasOwnProperty(i)){
// console.log(i+" "+muyi[i]);

// }
// }

(function(){

// Set the listener for property get and set
var obj = {a:1,b:2};
Object.defineProperty(obj,"a",{
set:function(v)
{
this._a = v;

console.log ("Set a"+v);
},
get:function()
{

console.log("Get a");
// return this.a;
return this._a;
}

})

obj.a=12;
console.log(obj.a)


})()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854826&siteId=291194637