Skip to content

Instantly share code, notes, and snippets.

@gokulkrishh
Created November 3, 2015 07:33
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 gokulkrishh/17129d24e80a7b18c3e4 to your computer and use it in GitHub Desktop.
Save gokulkrishh/17129d24e80a7b18c3e4 to your computer and use it in GitHub Desktop.
Use Object.observe with polyfill
/*
Example for Object.observe
Polyfill: https://github.com/MaxArt2501/object-observe
Detects: Add, Update, Delete properties
Supported Browser: http://caniuse.com/object-observe
*/
var myObj = {
name: 'Gokul'
};
Object.observe(myObj, function (changes) {
//changes argument will come as an array
//Check if changes is occured
if (changes[0] !== undefined) {
//Added new property to myObj
if (changes[0].type === 'add') {
console.log('Added a new property ----->', changes[0]);
}
//Deleted a property to myObj
else if (changes[0].type === 'delete') {
console.log('Deleted a property ----->', changes[0]);
}
//Updated a property to myObj
else if (changes[0].type === 'update') {
console.log('Updated a property ----->', changes[0]);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment