xxx代表要求被调控的品质澳门新蒲京平台

虽说ES5中为大家提供了Object.defineProperty方法来安装getter与setter,但此原生方法运用起来并不便利,大家何不自身来实现三个类,只要继续该类并依据一定的正规化就足以具有比美原生的getter与setter。

现今大家定义以下规范:

取值器跟设值器信守格式:_xxxGetter/_xxxSetter,xxx代表须求被决定的品质。比如,假如要调整foo属性,则对象急需提供_fooGetter/_fooSetter方法来作为实际上的取值器与调控器,那样大家能够带代码中调用obj.get(‘foo’卡塔尔(قطر‎和obj.set(‘foo’,
value卡塔尔来进展取值与设值;不然调用get与set方法也正是代码:obj.foo和obj.foo
= value;

提供watch函数:obj.watch(attr, function(name, oldValue,
newValue卡塔尔{}卡塔尔;每回调用set方法时,便会触发fucntion参数。
function中name代表被退换的天性,oldValue是上三次该属性的值,newValue代表该属性的风尚值。该措施再次来到叁个handle对象,具备remove方法,调用remove将function参数从函数链中移除。

先是接收闭包情势,使用attributes变量作为个人属性存放全部属性的getter与setter:

var Stateful = (function(){
    'use strict';

    var attributes = {
        Name: {
            s: '_NameSetter',
            g: '_NameGetter',
            wcbs: []
        }
    };

    var ST = function(){};

    return ST;
})()

里头wcbs用来积攒调用watch(name, callback卡塔尔(قطر‎时具有的callback。

第豆蔻梢头版实今世码如下:

var Stateful = (function(){
    'use strict';

    var attributes = {};

    function _getNameAttrs(name){
        return attributes[name] || {};
    }

    function _setNameAttrs(name) {
        if (!attributes[name]) {
            attributes[name] = {
                s: '_' + name + 'Setter',
                g: '_' + name + 'Getter',
                wcbs: [] 
            }
        }
    }

    function _setNameValue(name, value){
        _setNameAttrs(name);
        var attrs = _getNameAttrs(name);
        var oldValue = _getNameValue.call(this, name);
        //如果对象拥有_nameSetter方法则调用该方法,否则直接在对象上赋值。
        if (this[attrs.s]){
            this[attrs.s].call(this, value);
        } else {
            this[name] = value;
        }

        if (attrs.wcbs && attrs.wcbs.length > 0){
            var wcbs = attrs.wcbs;
            for (var i = 0, len = wcbs.length; i < len; i++) {
                wcbs[i](name, oldValue, value);
            }
        }
    };

    function _getNameValue(name) {
        _setNameAttrs(name);
        var attrs = _getNameAttrs(name);

        var oldValue = null;
        // 如果拥有_nameGetter方法则调用该方法,否则直接从对象中获取。
        if (this[attrs.g]) {
            oldValue = this[attrs.g].call(this, name);
        } else {
            oldValue = this[name];
        }

        return oldValue;
    };

    function ST(){};

    ST.prototype.set = function(name, value){
        //每次调用set方法时都将name存储到attributes中
        if (typeof name === 'string'){
            _setNameValue.call(this, name, value);
        } else if (typeof name === object) {
            for (var p in name) {
                _setNameValue.call(this, p, name[p]);
            }
        }

        return this;
    };

    ST.prototype.get = function(name) {
        if (typeof name === 'string') {
            return _getNameValue.call(this, name);
        }
    };

    ST.prototype.watch = function(name, wcb) {
        var attrs = null;
        if (typeof name === 'string') {
            _setNameAttrs(name);
            attrs = _getNameAttrs(name);
            attrs.wcbs.push(wcb);

            return {
                remove: function(){
                    for (var i = 0, len = attrs.wcbs.length; i < len; i++) {
                        if (attrs.wcbs[i] === wcb) {
                            break;
                        }
                    }

                    attrs.wcbs.splice(i, 1);
                }
            }
        } else if (typeof name === 'function'){
            for (var p in attributes) {
                attrs = attributes[p];
                attrs.wcbs.splice(0,0, wcb); //将所有的callback添加到wcbs数组中
            }

            return {
                remove: function() {
                    for (var p in attributes) {
                        var attrs = attributes[p];
                        for (var i = 0, len = attrs.wcbs.length; i < len; i++) {
                            if (attrs.wcbs[i] === wcb) {
                                break;
                            }
                        }

                        attrs.wcbs.splice(i, 1);
                    }
                }
            }
        }
    };

    return ST;
})()

测量检验职业:

console.log(Stateful);
    var stateful = new Stateful();

    function A(name){
        this.name = name;
    };
    A.prototype = stateful;
    A.prototype._NameSetter = function(n) {
        this.name = n;
    };
    A.prototype._NameGetter = function() {
        return this.name;
    }

    function B(name) {
        this.name = name;
    };
    B.prototype = stateful;
    B.prototype._NameSetter = function(n) {
        this.name = n;
    };
    B.prototype._NameGetter = function() {
        return this.name;
    };

    var a = new A();
    var handle = a.watch('Name', function(name, oldValue, newValue){
        console.log(name + 'be changed from ' + oldValue + ' to ' + newValue);
    });
    a.set('Name', 'AAA');
    console.log(a.name);

    var b = new B();
    b.set('Name', 'BBB');
    console.log(b.get('Name'));

    handle.remove();
    a.set('Name', 'new AAA');
    console.log(a.get('Name'), b.get('Name'))

输出:

function ST(){}
Namebe changed from undefined to AAA
AAA
Namebe changed from undefined to BBB
BBB
new AAA BBB

能够看看将享有watch函数贮存于wcbs数组中,全体子类重名的性质访问的都以同四个wcbs数组。有何方法可以既保障每种实例具有自个儿的watch函数链又不发出传染?能够思虑这种方法:为各样实例增添一个_watchCallbacks属性,该属性是贰个函数,将有所的watch函数链都寄存到该函数上,首要代码如下:

ST.prototype.watch = function(name, wcb) {
        var attrs = null;

        var callbacks = this._watchCallbacks;
        if (!callbacks) {
            callbacks = this._watchCallbacks = function(n, ov, nv) {
                var execute = function(cbs){
                    if (cbs && cbs.length > 0) {
                        for (var i = 0, len = cbs.length; i < len; i++) {
                            cbs[i](n, ov, nv);
                        }
                    }
                }
                //在函数作用域链中可以访问到callbacks变量
                execute(callbacks['_' + n]);
                execute(callbacks['*']);// 通配符
            }
        }

        var _name = '';
        if (typeof name === 'string') {
            var _name = '_' + name;
        } else if (typeof name === 'function') {//如果name是函数,则所有属性改变时都会调用该函数
            _name = '*';
            wcb = name;
        }
        callbacks[_name] = callbacks[_name] ? callbacks[_name] : [];
        callbacks[_name].push(wcb);

        return {
            remove: function(){
                var idx = callbacks[_name].indexOf(wcb);
                if (idx > -1) {
                    callbacks[_name].splice(idx, 1);
                }
            }
        };
    };

透过改造后完全代码如下:

var Stateful = (function(){
    'use strict';

    var attributes = {};

    function _getNameAttrs(name){
        return attributes[name] || {};
    }

    function _setNameAttrs(name) {
        if (!attributes[name]) {
            attributes[name] = {
                s: '_' + name + 'Setter',
                g: '_' + name + 'Getter'/*,
                wcbs: []*/
            }
        }
    }

    function _setNameValue(name, value){
        if (name === '_watchCallbacks') {
            return;
        }
        _setNameAttrs(name);
        var attrs = _getNameAttrs(name);
        var oldValue = _getNameValue.call(this, name);

        if (this[attrs.s]){
            this[attrs.s].call(this, value);
        } else {
            this[name] = value;
        }

        if (this._watchCallbacks){
            this._watchCallbacks(name, oldValue, value);
        }
    };

    function _getNameValue(name) {
        _setNameAttrs(name);
        var attrs = _getNameAttrs(name);

        var oldValue = null;
        if (this[attrs.g]) {
            oldValue = this[attrs.g].call(this, name);
        } else {
            oldValue = this[name];
        }

        return oldValue;
    };

    function ST(obj){
        for (var p in obj) {
            _setNameValue.call(this, p, obj[p]);
        }
    };

    ST.prototype.set = function(name, value){
        if (typeof name === 'string'){
            _setNameValue.call(this, name, value);
        } else if (typeof name === 'object') {
            for (var p in name) {
                _setNameValue.call(this, p, name[p]);
            }
        }

        return this;
    };

    ST.prototype.get = function(name) {
        if (typeof name === 'string') {
            return _getNameValue.call(this, name);
        }
    };

    ST.prototype.watch = function(name, wcb) {
        var attrs = null;

        var callbacks = this._watchCallbacks;
        if (!callbacks) {
            callbacks = this._watchCallbacks = function(n, ov, nv) {
                var execute = function(cbs){
                    if (cbs && cbs.length > 0) {
                        for (var i = 0, len = cbs.length; i < len; i++) {
                            cbs[i](n, ov, nv);
                        }
                    }
                }
                //在函数作用域链中可以访问到callbacks变量
                execute(callbacks['_' + n]);
                execute(callbacks['*']);// 通配符
            }
        }

        var _name = '';
        if (typeof name === 'string') {
            var _name = '_' + name;
        } else if (typeof name === 'function') {//如果name是函数,则所有属性改变时都会调用该函数
            _name = '*';
            wcb = name;
        }
        callbacks[_name] = callbacks[_name] ? callbacks[_name] : [];
        callbacks[_name].push(wcb);

        return {
            remove: function(){
                var idx = callbacks[_name].indexOf(wcb);
                if (idx > -1) {
                    callbacks[_name].splice(idx, 1);
                }
            }
        };
    };

    return ST;
})()

测试:

console.log(Stateful);
    var stateful = new Stateful();

    function A(name){
        this.name = name;
    };
    A.prototype = stateful;
    A.prototype._NameSetter = function(n) {
        this.name = n;
    };
    A.prototype._NameGetter = function() {
        return this.name;
    }

    function B(name) {
        this.name = name;
    };
    B.prototype = stateful;
    B.prototype._NameSetter = function(n) {
        this.name = n;
    };
    B.prototype._NameGetter = function() {
        return this.name;
    };

    var a = new A();
    var handle = a.watch('Name', function(name, oldValue, newValue){
        console.log(name + 'be changed from ' + oldValue + ' to ' + newValue);
    });
    a.set('Name', 'AAA');
    console.log(a.name);

    var b = new B();
    b.set('Name', 'BBB');
    console.log(b.get('Name'));

    a.watch(function(name, ov, nv) {
        console.log('* ' + name + ' ' + ov + ' ' + nv);
    });

    a.set({
        foo: 'FOO',
        goo: 'GOO'
    });

    console.log(a.get('goo'));

    a.set('Name', 'AAA+');

    handle.remove();
    a.set('Name', 'new AAA');
    console.log(a.get('Name'), b.get('Name'))

输出:

function ST(obj){
        for (var p in obj) {
            _setNameValue.call(this, p, obj[p]);
        }
    }
Namebe changed from undefined to AAA
AAA
BBB
* foo undefined FOO
* goo undefined GOO
GOO
Namebe changed from AAA to AAA+
* Name AAA AAA+
* Name AAA+ new AAA
new AAA BBB

如上代码正是dojo/Stateful的原理。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website