Skip to content
forked from Tencent/westore

用于观察任意对象的任意变化的类库,以轻巧、实用、强大而闻名。

License

Notifications You must be signed in to change notification settings

cchh2025/observejs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

##observe.js

用于观察任意对象的任意变化的类库,以轻巧、实用、强大而闻名。

##3分钟精通observe.js

对象字面量

var obj = { a: 1 };
//watch obj
observe(obj, function (name, value) {
    console.log(name + "__" + value);//a__2 
});
obj.a = 2;

数组

var arr = [1, 2, 3];
//watch obj
observe(arr, function (name, value, old) {
    console.log(name + "__" + value+"__"+old);
});
arr.push(4);//array__push_4 
arr[3] = 5;//3__5_4

复杂对象

var complexObj = { a: 1, b: 2, c: [{ d: [4] }] };
//watch complexObj
observe(complexObj, function (name, value) {
    console.log(name + "__" + value);    //d__100 
});
complexObj.c[0].d = 100;

普通对象

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

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

About

用于观察任意对象的任意变化的类库,以轻巧、实用、强大而闻名。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 74.6%
  • HTML 25.4%