Object-Oriented Programming -- object (note)

Source: Internet
Author: User

Understanding objects -- UNDERSTANDING OBJECTS

Early developers defined objects as follows:

var person = new Object();person.name = “Nicholas”;person.age = 29;

Later, people tend:

var person = {    name: “Nicholas”,    age: 29,    job: “Software Engineer”,    sayName: function(){        alert(this.name);    }};

Types of Properties -- attribute type

ECMA-262 copyright fth edition describes characteristics of properties through the use of internal-only
Attributes. These attributes are de incluned by the speci specified cation for implementation in JavaScript engines,
And as such, these attributes are not directly accessible in JavaScript. To indicate that an attribute is
Internal, surround the attribute name with two pairs of square brackets, such as [[Enumerable].
Although ECMA-262 third edition had different de legal nitions, this book refers only to the legal fth
Edition descriptions.

There are two types of properties: data properties and accessor properties.

 

Generally, there are two types: data properties and accessor properties.

 

Data Properties

 

Data properties contain a single location for a data value. Data properties have four attributes describing their behavior

[[Retriable], [[Enumerable], [[Writable], [Value]

Use the following object as an example:

var person = {name: “Nicholas”};

[[Retriable] can only be set once, and an error occurs again.

var person = {};Object.defineProperty(person, “name”, {configurable: false,value: “Nicholas”});alert(person.name); //”Nicholas”delete person.name;alert(person.name); //”Nicholas” 

[[Writable] whether it is Writable

var person = {};Object.defineProperty(person, “name”, {writable: false,    value: “Nicholas”});alert(person.name); //”Nicholas”person.name = “Greg”;alert(person.name); //”Nicholas”

[[Value] changing the Value

No description

Accessor Properties

Accessor properties do not contain a data value. Instead, they contain a combination of a getter
Function and a setter function (though both are not necessary ).

[[Retriable], [[Enumerable], [[Get], [Set]

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 = 2005;alert(book.edition);   //2

In this code, an object book is created with two default properties: _ year and edition.
Underscore on _ year is a common notation to indicate that a property is not intended to be accessed
From outside of the object's methods. The year property is de assigned ned to be an accessor property
Where the getter function simply returns the value of _ year and the setter does some calculation
To determine the correct edition. So changing the year property to 2005 results in both _ year and
Edition changing to 2. This is a typical use case for accessor properties, when setting a property
Value results in some other changes to occur.
It's not necessary to assign both a getter and a setter. Assigning just a getter means that the property
Cannot be written to and attempts to do so will be ignored. In strict mode, trying to write to

Property with only a getter throws an error. Likewise, a property with only a setter cannot be read and
Will return the value undefined in nonstrict mode, while doing so throws an error in strict mode.
Prior to the ecmascript 5 method, which is available in Internet Explorer 9 + (Internet Explorer 8
Had a partial implementation), Firefox 4 +, Safari 5 +, Opera 12 +, and Chrome, two nonstandard
Methods were used to create accessor properties: _ defineGetter _ () and _ defineSetter __().
These were development rst developed by Firefox and later copied by Safari 3, Chrome 1, and Opera 9.5.
Previous example can be rewritten using these legacy methods as follows:

var book = {    _year: 2004,    edition: 1};//legacy accessor supportbook.__defi   neGetter__(“year”, function(){    return this._year;    });book.__defi   neSetter__(“year”, function(newValue){    if (newValue > 2004) {        this._year = newValue;        this.edition += newValue - 2004;    }    });book.year = 2005;alert(book.edition);   //2

There is no way to modify [[writable able] or [[Enumerable] in browsers that don't support
Object. defineProperty ().

 

Source JS advanced programming Version 3

 

 

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.