JavaScript Multicast events, property settings/Reader classes

Source: Internet
Author: User

Most of the Web is using function (similar to Person.getname (), Person.setname ()). Of course, this is a common way.

But in a browser other than IE, you can try to run the following code

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 are ' + Person.age + '. The sex value is ' + person.sex ');
alert (person._sex); Undefined

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.