Created
January 13, 2017 13:36
-
-
Save npras/ce70d00a080afeb33032b51afe6773e2 to your computer and use it in GitHub Desktop.
Reproduce Vue's data object proxy behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://vuejs.org/v2/guide/instance.html | |
// | |
// | |
//function Vue(opts){ | |
//for(var key in opts.data){ | |
//this[key] = opts.data[key] | |
//} | |
//} | |
function Vue(opts){ | |
this._data = opts.data; | |
Object.keys(opts.data) | |
.forEach(key => { | |
props = { | |
configurable: true, | |
enumerable: true, | |
get() { return this._data[key]; }, | |
set(val) { this._data[key] = val; } | |
} | |
Object.defineProperty(this, key, props); | |
}); | |
} | |
///////// | |
let dataObj = { | |
greetMsg: 'greet', | |
byeMsg: 'bye' | |
} | |
let opts = { | |
el: '#app1', | |
data: dataObj | |
} | |
let vue = new Vue(opts); | |
console.log(vue.greetMsg === dataObj.greetMsg); | |
vue.greetMsg = 'modifiedGreet' | |
console.log(vue.greetMsg) | |
console.log(dataObj.greetMsg) | |
dataObj.greetMsg = 'yetAgainModifiedGreet' | |
console.log(dataObj.greetMsg) | |
console.log(vue.greetMsg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment