This article mainly introduces the attributes of Javascript accessors. The example analyzes the skills for establishing attribute associations and has some reference value, for more information about the usage of Javascript accessors, see the examples in this article. The specific analysis is as follows:
This is similar to the constructor, but it has different functions. It allows two attributes to be associated and changed by modifying one attribute.
The Code is as follows:
Var book = {
_ Year: 2004,
Edition: 1
};
Object. defineProperty (book, "year ",{
Get: function (){
Return this. _ year;
},
Set: function (newValue ){
If (newValue> 2004 ){
This. _ year + = newValue;
This. edition + = newValue-2004;
}
}
});
Book. year = 2006;
Console. log (book. edition );
As shown in the preceding example, the value of _ year is changed only by year. However, when year is modified through set, the edition is also changed.
I hope this article will help you design javascript programs.