Alternatives by Daniel Oines | CC-BY
Is it a single thing?
Does it need to by really fast at large sizes?
Is it a key value pair?
A collection of things?
Does order of its parts matter?
Do you want to use the Array functions to operate on it?
Is it a list?
var boat = {};
Object.observe(boat, function (changes) {
//Iterate over the changes that have happened
changes.forEach(function (change) {
//do something here...
console.log(change.name, change.type);
});
});
boat.type = "skiff";
//observe would output:
type add
boat.type = "canoe";
//observe would output:
type update
var boat = {
motor: 'Johnson Outboard'
};
Object.observe(boat, function (changes) {
//Iterate over the changes that have happened
changes.forEach(function (change) {
if(change.name === 'motor' && change.object.motor !== 'Johnson Outboard'){
//We don't want people to be able to change this element...
// So we roll it back
change.object.motor = change.oldValue;
}
});
});
boat.motor = "Honda Outboard"; // boat.motor still equals Johnson Outboard
<input type="text" class=".boat-motor">
var boat = {
motor: 'Johnson Outboard'
}
, elem = document.querySelector('.boat-motor');
Object.observe(boat, function (changes) {
changes.forEach(function (change) {
if(change.name === 'motor' && change.type === 'update'){
elem.value = change.object.motor;
}
});
});
boat.motor = "Honda Outboard"; // The change will be reflected in the DOM
* not really