ES3 proxy object wording:
console.log ( 'private variables defined ES3 written:' ) // ES3 var the Person = function () { var Data = { name: 'ES3' , Age: 14 , Sex: 'NV' } // read the API the this .get = function (Key) { return Data [Key] } // write the API the this .set = function (Key, value) { IF (Key! == 'Sex' ) { Data [Key] = value } } } // declare an instance var Person = new new the Person (); // read
console.table ({name: person.get ( ' name'), age: person.get ( 'age'), sex: person. GET ( 'Sex')}); // modify person.set ( 'name', 'C-ES3') // change the name of ES3-C console.table ({name: person.get ( 'name'), Age: person.get ( 'Age'), sex: person.get ( 'sex' )}); person.set ( 'sex', 'nan') // modify sex as nan modification failed because protection is provided console.table ({name: person.get ( ' name'), age: person.get ( 'age'), sex: person.get ( 'sex')});
ES5 objects agency wording:
console.log ( 'private variables defined ES5 written:' ) // ES5 var the Person = { name: 'ES5' , Age: 14 } // set the protection, can not be written Object.defineProperty (the Person, 'Sex' , { Writable : to false , value: 'NV' }) // read console.table ({ name: person.name, Age: person.Age, Sex: Person.sex }) // modify person.name = 'c-ES5' // change the name of the ES5-C console.table ({ name: person.name, Age: person.Age, sex: Person.sex }) Person.sex = 'nan' // modify sex as nan modification fails, because the protection provided console.table ({ name: person.name, Age: person.Age, sex: Person.sex })
ES6 proxy object wording:
console.log ( 'private variables defined ES6 written:' ) the let of Person1 = { name: 'ES6' , Age: 14 , Sex: 'NV' } // set the object proxy the let PERSON1 = new new the Proxy (of Person1, { // read GET (target, Key) { return target [Key] }, // write SET (target, Key, value) { IF (Key == 'Sex'! {) target [Key] = value } } }); // read the console.table ({ name: person1.name, Age: person1.age, Sex: person1.sex }) // change the name is for ES6-C person1.name = 'C-for ES6' console.table ({ name: person1.name, Age: PERSON1 .age, sex: person1.sex }) // modify failed to modify sex nan, because protection is provided person1.sex = 'NaN3' console.table ({ name: person1.name, Age: person1.age, sex: person1.sex })