Skip to content
observer
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
dist fix bugs, recode proxyEvent, performance optimization
src fix bugs, recode proxyEvent, performance optimization
tool init
.bowerrc init
.editorconfig init
.eslintrc init
.gitignore init
README.md read me
bower.json change bower cfg
gulpfile.js fix bugs
karma.conf.js update
package.json fix bugs

README.md

observer.js

用于观察任意对象的任意变化的类库。 支持 ES7 Object.observe, IE 6 7 8

对象字面量

var obj = { a: 1 };
observer.on(obj, function (name, value , old) {
    console.log(name + "__" + value + "__" + old);
});
obj.a = 2; //a__2__1 

数组

var arr = [1, 2, 3];
observer.on(arr, function(name, value, old) {
    console.log(name + "__" + value + "__" + old);
});
arr.push(4); //Array-push__[1,2,3,4]__[1,2,3]
arr[2] = 5; //2__5__3   length__4__3

复杂对象

var complexObj = {
    a: 1,
    b: 2,
    c: [{
        d: [4]
    }]
};
observer.on(complexObj, 'c[0].d', function(name, value, old) {
    console.log(name + "__" + value + "__" + old); 
});
complexObj.c[0].d = 100; //c[0].d__100__4

普通对象

var User = function(name, age) {
    this.name = name;
    this.age = age;
    //只监听name
    observer.on(this, ["name"], function(name, value, oldValue) {
        console.log(name + "__" + value + "__" + oldValue);
    });
}
var user = new User("lisi", 25);
user.name = "wangwu"; //name__wangwu__lisi
user.age = 20; //nothing,no listener

This content is released under the (http://opensource.org/licenses/MIT) MIT License.

Something went wrong with that request. Please try again.