/**
* Created by Administrator on 2015/7/20 0020.
*/
Object-oriented (OO): You can create any number of objects with the same properties and methods by using a class
JS defines an object as a collection of unordered attributes whose properties can contain basic values, objects, or functions, which, strictly speaking, are equivalent to
An object is a set of values that have no specific order, and each property or method of an object has a name, and each name is mapped to a value
Understanding Objects
create an object instance
var peson=new Object ();
Peson.name= ' Nichols ';
peson.age=29;
peson.job= ' software Engineer ';
Peson.sayname=function () {
Alert (this.name)
};
Create a named peson object, and add several properties . Name,.age,.job, and a method . Sayname
The Sayname () method is used to display The value of the THIS.name and is now shown by the following code
var peson={
Name: ' Nichols ',
Age:29,
Job: ' Software Engineer ',
Sayname:function () {
alert (this.name);
}
};
Property Type : Data properties and accessor properties
/* Data Properties
* contains the location of a data value, readable and writable in this position, with 4 Descriptive behaviors
* [Configurable]: indicates whether the property can be redefined by deleting the property through the delete , can modify the attribute's properties, or can
No property is modified to accessor property, the default value is true
* [Enumerable]: Indicates whether The property can be returned through the for-in loop, the default value is true
* [writable]: Indicates whether the property can be modified, the default value is true
* [Value]: contains the data value of this property, when reading the value of the property, read from this position, when writing the property value, the new value
Save in this location, the default value is undefined*/
var peson={
Name: ' Nichols '
};
Create a named name property and specify a value for it:Nichols, which meansthat the[value] attribute is set to Nichols, while the
Any changes to this value will be reflected in this position.
to modify the default attribute:theobject.defineproperty () method, which accepts 3 Parameters: The object where the property is located, the name, and a stroke
Descriptor Object
The Descriptor (Descriptor) object property must be Configurable,emumerable,writable Value, set one or
Value, you can modify the value of the corresponding attribute, such as
var peson={};
Object.defineproperty (Peson, ' Name ', {writable:false,
Value: ' Nichols '});// Create a name property whose value is Nichols Read-only, non-modifiable
alert (peson.name);//undefined
Peson.name= ' Greg ';
alert (peson.name);//greg
You can also use the following rules (do not modify properties)
var peson={};
Object.defineproperty (Peson, ' Name ', {configurable:false,
Value: ' Nichols '});
alert (peson.name);//undefined
Peson.name= ' Greg ';
alert (peson.name);//greg
Configurable:false, which indicates that the property cannot be removed from the object, and if delete is called on this property ,in strict mode
Will not happen, in strict mode can cause errors
Object.defineproperty (Peson, ' Name ', {configurable:true,
Value: ' Nichols '});// Such modification will result in an error
The Object.defineproperty ( ) method modifies the same property, but there is a limit after configurable is false
in the call object.defineproperty () method, if not specified,configurable,Enumerable and writable features
The recognition value is false, in most cases ,
The Object.defineproperty () method method provides these advanced features
Accessor Properties
/* access properties do not contain data values, they contain a pair of getter and setter functions (but none of these 2 properties are required)
* when the accessor property is read , the Getter property is called, which is responsible for returning a valid value, which is called when the Access property is written .
Setter function and pass in the new value, how to handle the property, as follows
* [Configurable]: indicates whether the property can be redefined by deleting the property through the delete , can modify the attribute's properties, or can
No property is modified to accessor property, the default value is true
* [Enumerable]: Indicates whether The property can be returned through the for-in loop, the default value is true
* [Get] invokes a function when reading a property, default is undefined
* [Set] call function when writing property, default is undefined
* */
access in the properties of the device cannot be directly defined, you need to use the Object.defineproperty () definition , as follows
var book={_year:2004,edition:1};
Object.defineproperty (book, ' Year ', {get:function () {
Return this._year
},
Set:function (
Value
) {if (value>200) {
This._year=value;
this.edition+=value-2004
}}
});
book.year=2005;
alert (book.edition);
create an object named book object with 2 default properties defined : _year and Edition;
The underscore in front of the _year is a common notation used to indicate that a property can only be accessed through an object method, while the accessor property is wrapped
Includes a getter and setter function
The Getter function returns the _year value, andthesetter function uses the gaze to determine the correct version,
the value of year is changed to 2005, then _year will also become 2005,Edition becomes 2, and this is the usual accessor property.
/* do not have to specify getter and setter at the same time, specifying getter only means that the property cannot be written. Attempt to write property is ignored
* on Strict Yanshi, the write only specifies the getter function and an error occurs, and a similar setter property cannot be read
* to create an Access attribute, 2 non-standard methods are generally used
* __DEFINGETTER__ () and __definesetter__ (), example * /
var book={_year:2004,edition:1};
defining an accessor legacy property
BOOK.__DEFINEGETTER__ (' Year ', function () {
return this._year;
});
BOOK.__DEFINESETTER__ (' Year ', function (value) {
if (value>2004) {
This._year=value;
this.edition+=value-2004
}
});
book.year=2005;
alert (book.edition);
Define multiple properties
Object.defineproperties (): You can define multiple properties and accept 2 parameters: The first object is the one that you want to add and modify properties
Object, the property of the second object corresponds to the addition or modification of property one by one in the first object, as follows
var book={};
Object.defineproperties (book,{
_year:{
value:2004
},edition:{
Value:1
},year:{
Get:function () {
Return this._year
},
Set:function (value) {
if (value>2004) {
This._year=value;
this.edition+=value-2004
}
}
}
});
The properties of the read attribute , the object.defineproperties () method, can obtain a descriptor for a given property, which accepts 2 parameters:
Where the object is located and to read the description name, the return value is an object, and if it is an accessor property, the object has
Configurable,enumerable,get,set
if it is a data attribute, this object configurable,enumerable,writable and value, as follows
var book={};
Object.defineproperties (book,{
_year:{
value:2004
},edition:{
Value:1
},year:{
Get:function () {
Return this._year
},
Set:function (value) {
if (value>2004) {
This._year=value;
this.edition+=value-2004
}
}
}
});
var descript=object.getownpropertydescriptor (book, ' _year ');
alert (descript.value);//2004
alert (descript.configurable);//false
Alert (typeof Descript.get);//' undefined '
var descripts=object.getownpropertydescriptor (book, ' Year ');
alert (descripts.value);//undefined
alert (descripts.enumerable);//false
Alert (typeof Descripts.get);//' function '
JavaScript version 3rd (Advanced Programming) Chapter 6th: Object-Oriented Programming