js如何实现模拟监听事件

本文介绍了如何使用JavaScript实现模拟监听事件,特别是在对象属性变化时触发指定的回调函数。通过Animal构造函数及其prototype上的onChange方法,当name属性改变时,会调用注册的回调函数通知属性变化。
摘要由CSDN通过智能技术生成
function Animal(name) {
  this.name = name;
  this.getName = function() {
      return this.name;
  }
  this.setName = function(name) {
      this.name = name;
      this._valueChangeHandler('name');
  }
  }
  Animal.prototype = {
    onChange: function(valueName, fun) {
        this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
        this["_" + valueName + "ChangeHandlers"].push(fun);
    },
    _valueChangeHandler: function(valueName) {
        var o = this["_" + valueName + "ChangeHandlers"];
        if (o) {
            for (var i = 0, n = o.length; i < n; i++) {
                var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
                o[i](this[methodName]());
            }
        }
       
    }
  }

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
  console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
  console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");

function Animal(name) {
this.name = name;
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
this._valueChangeHandler('name');
}
}
Animal.prototype = {
onChange: function(valueName, fun) {
this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
this["_" + valueName + "ChangeHandlers"].push(fun);
},
_valueChangeHandler: function(valueName) {
var o = this["_" + valueName + "ChangeHandlers"];
if (o) {
for (var i = 0, n = o.length; i < n; i++) {
var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
o[i](this[methodName]());
}
}

}
}

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");function Animal(name) {
this.name = name;
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
this._valueChangeHandler('name');
}
}
Animal.prototype = {
onChange: function(valueName, fun) {
this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
this["_" + valueName + "ChangeHandlers"].push(fun);
},
_valueChangeHandler: function(valueName) {
var o = this["_" + valueName + "ChangeHandlers"];
if (o) {
for (var i = 0, n = o.length; i < n; i++) {
var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
o[i](this[methodName]());
}
}

}
}

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");function Animal(name) {
this.name = name;
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
this._valueChangeHandler('name');
}
}
Animal.prototype = {
onChange: function(valueName, fun) {
this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
this["_" + valueName + "ChangeHandlers"].push(fun);
},
_valueChangeHandler: function(valueName) {
var o = this["_" + valueName + "ChangeHandlers"];
if (o) {
for (var i = 0, n = o.length; i < n; i++) {
var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
o[i](this[methodName]());
}
}

}
}

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");

function Animal(name) {
this.name = name;
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
this._valueChangeHandler('name');
}
}
Animal.prototype = {
onChange: function(valueName, fun) {
this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
this["_" + valueName + "ChangeHandlers"].push(fun);
},
_valueChangeHandler: function(valueName) {
var o = this["_" + valueName + "ChangeHandlers"];
if (o) {
for (var i = 0, n = o.length; i < n; i++) {
var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
o[i](this[methodName]());
}
}

}
}

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");function Animal(name) {
this.name = name;
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
this._valueChangeHandler('name');
}
}
Animal.prototype = {
onChange: function(valueName, fun) {
this["_" + valueName + "ChangeHandlers"] = this["_" + valueName + "ChangeHandlers"] || [];
this["_" + valueName + "ChangeHandlers"].push(fun);
},
_valueChangeHandler: function(valueName) {
var o = this["_" + valueName + "ChangeHandlers"];
if (o) {
for (var i = 0, n = o.length; i < n; i++) {
var methodName = "get" + valueName.charAt(0).toUpperCase() + valueName.slice(1);
o[i](this[methodName]());
}
}

}
}

var a = new Animal('zhangsan');
a.onChange('name', function(o) {
console.log("1.my name is " + o);
});
a.onChange('name', function(o) {
console.log("2.my name is " + o);
});
a.setName("zhangsanfeng");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值