Home >Web Front-end >JS Tutorial >Implement multicast events, property setters/readers in JavaScript
I did an interesting experiment. Most people on the Internet use function (similar to person.getName(), person.setName()). Of course, this is a general approach.
But in non-IE browsers, you can try running the following code:
view sourceprint?function Person()
{
var _name
,_age,_sex;
var _nameChanged = new Array ();
this.__defineGetter__(NameChanged,function()
{
return _nameChanged;
});
this.__defineSetter__(NameChanged,function(value)
value);
});
this.__defineGetter__(Name,function()
{
return _name;
});
this.__defineS etter__(Name,function(value)
{
f](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.NameChan ged = 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
So, using javascript to implement object-oriented properties and events is also very elegant things. But regarding the multicast incident, do you have any good ideas?