Giter Club home page Giter Club logo

aspect's Introduction

Aspect 使用文档


使用 Aspect,可以允许你在指定方法执行的前后插入特定函数。


使用说明

基于 Base.extend 创建的类,会自动添加上 Aspect 提供的功能。

before object.before(methodName, callback, [context])

在 object[methodName] 方法执行前,先执行 callback 函数。

var Dialog = Base.extend({
    ...

    show: function() {
        console.log(2);
        this.element.show();
    },

    ...
});

var dialog = new Dialog();

dialog.before('show', function() {
    console.log(1);
});

dialog.after('show', function() {
    console.log(3);
});

dialog.show(); // ==> 1, 2, 3

callback 函数在执行时,接收的参数与传给 object[methodName] 参数相同。如果传入了 context 参数,则 callback 里的 this 指向 context。

var dialog = new Dialog();

dialog.before('show', function(a, b) {
    console.log(a);
    console.log(b);
});

dialog.show(1, 2); // ==> 1, 2

可以在 callback 中 return false 来阻止原函数执行。

dialog.before('show', function() {
    console.log(1);
    return false;
});

dialog.after('show', function() {
    console.log(3);
});

dialog.show(); // ==> 1

after object.after(methodName, callback, [context])

在 object[methodName] 方法执行后,再执行 callback 函数。

callback 函数在执行时,接收的参数是 object[methodName] 执行完成后的返回值以及传给 object[methodName] 的参数。如果传入了 context 参数,则 callback 里的 this 指向 context。

var dialog = new Dialog();

dialog.after('show', function(returned, a, b) {
    console.log(returned); // show 的返回值
    console.log(a);
    console.log(b);
});

dialog.show(1, 2); // ==> undefined, 1, 2

注意

before 和 after 是按注册的先后顺序执行的,先注册先执行。

dialog.before('show', function() {
    console.log(1);
});

dialog.before('show', function() {
    console.log(2);
});

dialog.show(); // ==> 1, 2

aspect's People

Contributors

303182519 avatar

Watchers

James Cloos avatar MrFanggesun avatar lirunzi avatar April avatar  avatar plcc avatar 浴火小青春 avatar  avatar guoshengze avatar

Forkers

bby422

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.