Home > Web Front-end > JS Tutorial > body text

Implement multicast events, property setters/readers in JavaScript

高洛峰
Release: 2016-11-25 10:58:59
Original
1098 people have browsed it

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?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!