GithubHelp home page GithubHelp logo

hhy5277 / observejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tencent/westore

0.0 2.0 0.0 76 KB

observe any object's any change

Home Page: http://alloyteam.github.io/Nuclear/

License: MIT License

HTML 27.93% JavaScript 72.07%

observejs's Introduction

##observe.js

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

ps:源代码未压缩版仅仅只有158行代码:)

你可以移步 Nuclear 感受observe.js在框架里不可替代的作用。

##3分钟精通observe.js

对象字面量

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

数组

var arr = [1, 2, 3];
observe(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

复杂对象

var complexObj = { a: 1, b: 2, c: [{ d: [4] }] };
observe(complexObj, function (name, value , old, path) {
    console.log(name + "__" + value + "__" + old);   //d__100__4 
	console.log(path)	                             //#-c-0
});
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);
    });
}
var user = new User("lisi", 25);
user.name = "wangwu";//name__wangwu__lisi 
user.age= 20; //什么都输出,因为没有监听age

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

observejs's People

Contributors

cnlon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.