码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript-装饰器函数(Decorator)

时间:2016-05-13 00:19:48      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:

JavaScript-装饰器函数(Decorator)

用于给对象在运行期间动态的增加某个功能,职责等。相较通过继承的方式来扩充对象的功能,装饰器显得更加灵活,首先,我们可以动态给对象选定某个装饰器,而不用hardcore继承对象来实现某个功能点。其次:继承的方式可能会导致子类繁多,仅仅为了增加某一个单一的功能点,显得有些多余了。

下面给出几个常用的装饰器函数示例,相关代码请查看github

1 动态添加onload监听函数

        function addLoadEvent(fn) {
            var oldEvent = window.onload;
            if(typeof window.onload != ‘function‘) {
                window.onload = fn;
            }else {
                window.onload = function() {
                    oldEvent();
                    fn();
                };
            }
        }
        function fn1() {
            console.log(‘onloadFunc 1‘);
        }
        function fn2() {
            console.log(‘onloadFunc 2‘);
        }
        function fn3() {
            console.log(‘onloadFunc 3‘);
        }
        addLoadEvent(fn1);
        addLoadEvent(fn2);
        addLoadEvent(fn3);

技术分享

2 前置执行函数和后置执行函数

    Function.prototype.before = function(beforfunc) {
            var self = this;
            var outerArgs = Array.prototype.slice.call(arguments, 1);

            return function() {
                var innerArgs = Array.prototype.slice.call(arguments);
                var finalArgs = outerArgs.concat(innerArgs);

                beforfunc.apply(this, finalArgs);
                self.apply(this, finalArgs);
            };
        };

        Function.prototype.after = function(afterfunc) {
            var self = this;
            var outerArgs = Array.prototype.slice.call(arguments, 1);

            return function() {
                var innerArgs = Array.prototype.slice.call(arguments);
                var finalArgs = outerArgs.concat(innerArgs);


                self.apply(this, finalArgs);
                afterfunc.apply(this, finalArgs);
            };
        };

        var func = function(name){
            console.log(‘I am ‘ + name);
        };
        var beforefunc = function(age){
            console.log(‘I am ‘ + age + ‘years old‘);
        };
        var afterfunc = function(gender){
            console.log(‘I am a ‘ + gender);
        };

        func.before(beforefunc(‘12‘)).after(afterfunc(‘boy‘));
        func(‘A‘);

技术分享

3 函数执行时间计算

    function log(func){
            return function(...args){
                const start = Date.now();
                let result = func(...args);
                const used = Date.now() - start;
                console.log(`call ${func.name} (${args}) used ${used} ms.`);
                return result;
            };
        }

        function calculate(times){
            let sum = 0;
            let i = 1;
            while(i < times){
                sum += i;
                i++;
            }
            return sum;
        }

        runCalculate = log(calculate);
        let result = runCalculate(100000);
        console.log(result);

注:这里我使用了ES2015(ES6)语法,如果你感兴趣可以查看我关于ES6的博客。
技术分享

当然,装饰器函数不仅仅这些用法。天猫使用的Nodejs框架Koa就基于装饰器函数及ES2015的Generator。希望这篇博客能起到抛砖引玉的作用,使你编写更优雅的JS代码。

JavaScript-装饰器函数(Decorator)

标签:

原文地址:http://blog.csdn.net/qiqingjin/article/details/51344684

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!