Skip to content

Instantly share code, notes, and snippets.

@npras
Created January 13, 2017 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npras/ce70d00a080afeb33032b51afe6773e2 to your computer and use it in GitHub Desktop.
Save npras/ce70d00a080afeb33032b51afe6773e2 to your computer and use it in GitHub Desktop.
Reproduce Vue's data object proxy behaviour
// 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