虽然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
以上就是javascript中getter/setter实现的示例代码分享的详细内容。