1、Object框架
新增的方法有:this
還須要重視的是,get/set屬性讀取訪問器。定義屬性時使用get/set,能夠在屬性被讀寫時作一些處理:編碼
var person = { _age: 18, get age(){ return this._age; }, set age(val){ if(val<0 || val>100){ console.log('設置年齡無效!'); } else { this._age = val; } } } person.age; //18 person.age = 300; //設置年齡無效! person.age = 30; person.age; //30
結合Object.defineProperty,下面是VUE這種MVVM框架實現雙向綁定的原理:spa
function Archiver() { var temperature = null; var archive = []; Object.defineProperty(this, 'temperature', { get: function() { console.log('get!'); return temperature; }, set: function(value) { temperature = value; archive.push({ val: temperature }); } }); this.getArchive = function() { return archive; }; } var arc = new Archiver(); arc.temperature; // 'get!' arc.temperature = 11; arc.temperature = 13; arc.getArchive(); // [{ val: 11 }, { val: 13 }]
代碼直接摘自MDN。prototype
2、Array雙向綁定
3、Stringcode
4、其餘對象