时间:2021-07-01 10:21:17 帮助过:8人阅读
但是在非 IE 的浏览器下,你可以尝试运行以下代码:
view sourceprint?function Person()
{
var _name
,_age,_sex;
var _nameChanged = new Array();
this.__defineGetter__(NameChanged,function()
{
return _nameChanged;
});
this.__defineSetter__(NameChanged,function(value)
{
_nameChanged.push(value);
});
this.__defineGetter__(Name,function()
{
return _name;
});
this.__defineSetter__(Name,function(value)
{
if(this.NameChanged != null)
{
for(var f in _nameChanged)
{
_nameChanged[f](value);
}
}
_name = value;
});
this.__defineGetter__(Age,function()
{
return _age;
});
this.__defineSetter__(Age,function(value)
{
_age = value;
});
}
Person.prototype =
{
get Sex()
{
return _sex;
},
set Sex(value)
{
_sex = value;
}
};
var person = new Person();
person.NameChanged = function()
{
alert(Event 1);
}
person.NameChanged = function()
{
alert(Event 2);
}
person.Name = Treenew;
person.Age = 22;
person.Sex = Man
alert(The + person.Name + Age is + person.Age + . The Sex Value is + person.Sex);
alert(person._sex); //undefined
所以,用javascript实现面向对象中的属性和事件,也是一件很优雅的事情。不过关于多播事件,不知园友有没有什么好主意?