"JS Authoritative Guide Learning Summary--6.6 Property getter and Setter"

Source: Internet
Author: User

Content Highlights:

I. Object properties

Object properties are composed of a name, a value, and a set of attributes. In ES5, a property value can be substituted by one or two methods, both getter and setter. Properties defined by getter and setter are called "Memory Properties", which are different from "data Properties" and data properties are a simple value.

When the program queries the value of accessor properties, JS calls the Getter method (no parameters). The return value of this method is the value of the property access expression.

When the program sets the value of an accessor property, JS invokes the setter method, passing the value on the right side of the assignment expression as a parameter to the setter. In a sense, this method is responsible for setting the value of the property. The return value of the setter method can be ignored.

Unlike data properties, accessor properties are not writable. If the property has both getter and setter, then it is a read/write property. If it has only getter methods, then it is a read-only property. If it only has a setter method, then it is a write-only property (there are some exceptions to the data attribute), and reading the write-only property always returns undefined.

Two. Defining Accessor properties

The simplest way to define accessor properties is to use an extension of the object's direct amount syntax:

var o = {

Normal data properties

Data_prop:value,

Accessor properties are all paired-defined functions

Get Accessor_prop () {/* Here is the function body */},

Set Accessor_prop () {/* Here is the function body */}

};

Accessor properties are defined as one or two functions with the same name as the attribute, which does not use the function keyword, but rather uses get and/or set.

Note that there is no colon separating the property name from the body of the function, but there is a comma between the end of the function body and the next method or data property.

For example:

var p = {

X and Y are common, readable and writable data properties

x:1.0,

y:1.0,

R is a read-write accessor attribute, it has getter and setter,

Get R () {return math.sqrt (this.x*this.x + this.y*this.y)},

Set R (NewValue) {

var oldvalue = math.sqrt (this.x*this.x + this.y*this.y);

var ratio = Newvalue/oldvalue;

This.x *=ratio;

This.y *=ratio;

},

Theta is a read-only accessor property, and it has only getter methods

Get Theta () {return math.atan2 (This.y, this.x);}

};

Three. Accessor attributes can be inherited

As with data properties, accessor properties can be inherited, so the object p in the preceding code can be used as another "point" prototype. You can define its X and Y properties for a new object, but the R and Theta properties can be inherited:

var q = Inherit (p); Create a new object that inherits the getter and setter

Q.x=1,q.y=1; Add a two attribute to Q

Console.log (Q.R); You can use the inherited accessor properties

Console.log (Q.theta);

Four.

This object produces a strictly self-increasing serial number

var serialnum = {

This data property contains the next serial number

The $ symbol implies that this property is a private property

$n: 0,

Returns the current value, and then self-increment

Get Next () {return this. $n + +},

Set Next (n) {

if (n >= this. $n) the this. $n = n;

else throw "The serial number cannot be as low as the current value"

}

};

The following example uses the getter method to implement a "magical" property

This object has an accessor property that can return a random number

For example, the expression "Random.octet" produces a random number

Every random number generated is between 0-255.

var random = {

Get Octet () {return Math.floor (Math.random () *256);},

Get UInt16 () {return Math.floor (Math.random () *65536);},

Get Init16 () {return Math.floor (Math.random () *65536)-32768;}

};

"JS Authoritative Guide Learning Summary--6.6 Property getter and Setter"

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.