我相信很多没有经验的人对如何在JavaScript中实现getter/setter一窍不通。因此,本文总结了出现问题的原因和解决方法,希望大家可以通过这篇文章来解决这个问题。
虽然ES5为我们提供了Object.defineProperty方法来设置getters和setters,但是这种原生方法使用起来并不方便。为什么我们不自己实现一个类,只要继承这个类并遵循一定的规范,我们就可以拥有与原生类相当的getters和setters?
现在我们定义以下规范:
赋值器和设置器遵循以下格式:_xxxGetter/_xxxSetter,xxx代表要控制的属性。例如,如果要控制foo属性,那么对象需要提供_fooGetter/_fooSetter方法作为实际的赋值器和控制器,这样我们就可以调用obj . get(lsquo;foo;)和obj . set(lsquo;foo;值)来获取和设置值;否则,调用get和set方法相当于代码:obj.foo和obj.foo=value
提供了watch函数:obj.watch (attr,函数(名称,旧值,新值){ });每次调用set方法时都会触发fucntion参数。在函数中,name表示已更改的属性,oldValue是该属性的最后一个值,newValue表示该属性的* * *值。方法返回一个句柄对象,具有一个remove方法,并调用remove从函数链中移除函数参数。
首先,使用闭包模式,使用属性变量作为私有属性来存储所有属性的getter和setter:
varstate=(function(){ 0
use strict ';
varattributes={ 0
名称:{
s: ' _ NameSetter ',
g:'_NameGetter ',
wcbs:[]
}
};
varST=function(){ };
returnST
})()其中wcbs用于存储调用watch(name,callback)时的所有回调。
* * *版本的实现代码如下:
varstate=(function(){ 0
use strict ';
var attributes={ };
function_getNameAttrs(名称){ 0
return attributes[name]| | { };
}
function_setNameAttrs(名称){ 0
if(!属性[名称]){
属性[名称]={ 0
s 3360“_”名称“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的原理。
看完上述内容,你们掌握如何进行JavaScript中getter/setter的实现的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/56686.html