Javascript accessors attribute instance analysis and javascript instance analysis
This article analyzes the usage of Javascript accessors and shares it with you for your reference. 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.
Copy codeCode: 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.