Last active
August 29, 2015 14:17
-
-
Save tweeeety/3312ba67bc9b03a7d184 to your computer and use it in GitHub Desktop.
Object.observe sample 03
This file contains 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
// ネイティブjavascriptオブジェクト | |
var model = { | |
title: 'this is Object.observe' | |
}; | |
// 監視用コールバックを定義 | |
function observer(changes){ | |
// changesのlengthを見てみる | |
console.log('changes length: ' + changes.length); | |
changes.forEach(function(change, i){ | |
console.log("[change " + i + "]"); | |
console.log(change.name); // 変更があったpropertyの名前 | |
console.log(change.type); // 変更の種類 | |
}); | |
} | |
// Object.observeでmodelを監視 | |
// 変更があればobserverを呼び出す | |
Object.observe(model, observer); | |
// model.titleを変更してみる | |
console.log("change 1"); | |
model.title = 'change!'; | |
console.log("change 2"); | |
model.title = 'change!'; | |
console.log("change 3"); | |
model.title = 'change!'; | |
console.log("change 4"); | |
model.title = 'change!'; | |
console.log("change 5"); | |
model.title = 'henkou!'; | |
console.log("change 6"); | |
model.title = 'modify!'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment