omi/packages/obaa
张磊 1197cf2a5d add omi-mp-create 2018-12-19 10:11:13 +08:00
..
test obaa v1.0.0 2018-12-12 17:46:17 +08:00
README.md update readme 2018-12-12 18:01:31 +08:00
index.js add omi-mp-create 2018-12-19 10:11:13 +08:00
package.json add omi-mp-create 2018-12-19 10:11:13 +08:00

README.md

obaa

Observe any object's any change in 0.1KB javascript.

Install

npm install obaa

Usage

observe object

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

observe array

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

observe class instance

var User = function (name, age) {
    this.name = name;
    this.age = age;
    //observe name only
    obaa(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 output

Other

arr.push(111) //trigger observe callback
//every method of array has a pureXXX function
arr.purePush(111) //don't trigger observe callback

arr.size(2) //trigger observe callback
arr.length = 2 //don't trigger observe callback

//if obj.c is undefined
obaa.set(obj, 'c', 3)
obj.c = 4 //trigger observe callback

//if obj.c is undefined
obj.c = 3
obj.c = 4 //don't trigger observe callback

License

MIT © dntzhang