Rumah > hujung hadapan web > tutorial js > JavaScript中getter/setter实现的示例代码分享

JavaScript中getter/setter实现的示例代码分享

黄舟
Lepaskan: 2017-03-13 17:13:41
asal
1379 orang telah melayarinya

虽然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;
})()
Salin selepas log masuk

其中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 === &#39;string&#39;){
            _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 === &#39;string&#39;) {
            return _getNameValue.call(this, name);
        }
    };

    ST.prototype.watch = function(name, wcb) {
        var attrs = null;
        if (typeof name === &#39;string&#39;) {
            _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 === &#39;function&#39;){
            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;
})()
Salin selepas log masuk

测试工作:

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(&#39;Name&#39;, function(name, oldValue, newValue){
        console.log(name + &#39;be changed from &#39; + oldValue + &#39; to &#39; + newValue);
    });
    a.set(&#39;Name&#39;, &#39;AAA&#39;);
    console.log(a.name);

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

    handle.remove();
    a.set(&#39;Name&#39;, &#39;new AAA&#39;);
    console.log(a.get(&#39;Name&#39;), b.get(&#39;Name&#39;))
Salin selepas log masuk

输出:

function ST(){}
Namebe changed from undefined to AAA
AAA
Namebe changed from undefined to BBB
BBB
new AAA BBB
Salin selepas log masuk

可以看到将所有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[&#39;_&#39; + n]);
                execute(callbacks[&#39;*&#39;]);// 通配符
            }
        }

        var _name = &#39;&#39;;
        if (typeof name === &#39;string&#39;) {
            var _name = &#39;_&#39; + name;
        } else if (typeof name === &#39;function&#39;) {//如果name是函数,则所有属性改变时都会调用该函数
            _name = &#39;*&#39;;
            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);
                }
            }
        };
    };
Salin selepas log masuk

经过改变后整体代码如下:

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[&#39;_&#39; + n]);
                execute(callbacks[&#39;*&#39;]);// 通配符
            }
        }

        var _name = &#39;&#39;;
        if (typeof name === &#39;string&#39;) {
            var _name = &#39;_&#39; + name;
        } else if (typeof name === &#39;function&#39;) {//如果name是函数,则所有属性改变时都会调用该函数
            _name = &#39;*&#39;;
            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;
})()
Salin selepas log masuk

测试:

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(&#39;Name&#39;, function(name, oldValue, newValue){
        console.log(name + &#39;be changed from &#39; + oldValue + &#39; to &#39; + newValue);
    });
    a.set(&#39;Name&#39;, &#39;AAA&#39;);
    console.log(a.name);

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

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

    a.set({
        foo: &#39;FOO&#39;,
        goo: &#39;GOO&#39;
    });

    console.log(a.get(&#39;goo&#39;));

    a.set(&#39;Name&#39;, &#39;AAA+&#39;);

    handle.remove();
    a.set(&#39;Name&#39;, &#39;new AAA&#39;);
    console.log(a.get(&#39;Name&#39;), b.get(&#39;Name&#39;))
Salin selepas log masuk

输出:

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
Salin selepas log masuk











Atas ialah kandungan terperinci JavaScript中getter/setter实现的示例代码分享. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan