1, and flag attribute descriptor
A flag
Object Properties In addition there are three special attribute value, that mark
writable ---- If true, you can modify, otherwise it's just read-only.
enumerable ---- If true, you can list in a loop, or else not listed.
configurable ----- If true, this property can be deleted, the corresponding characteristics can also be modified, or not
Get the syntax of these signs:
let descriptor = Object.getOwnPropertyDescriptor(obj,propertyName);
Object obj need to get information
PropertyName name of the property
let user = {
name : "Jhon"
};
let descriptor = Object.getOwnPropertyDescriptor(user,'name');
alert(JSON.stringify(descriptor,null,2));
/* property descriptor:
{
"value": "John",
"writable": true,
"enumerable": true,
"configurable": true
}
*/
Flag modify the syntax: Object.defineProperty (obj, propertyName, descriptor)
1 let user = {}; 2 3 Object.defineProperty(user, "name", { 4 value: "John" 5 }); 6 7 let descriptor = Object.getOwnPropertyDescriptor(user, 'name'); 8 9 alert( JSON.stringify(descriptor, null, 2 ) ); 10 /* 11 { 12 "value": "John", 13 "writable": false, 14 "enumerable": false, 15 "configurable": false 16 } 17 */
Setting the read-only attribute
let user = {
name : "Jhon"
};
Object.defineProperty(user,"name",{
writable:false
});
user.name = "XiaoMing"; // error, can not set the read-only attribute 'name';
Non-enumerable
By setting enumerable flag you can set whether the property can enumerate
. 1 the let = User { 2 name: " John " , . 3 toString () { . 4 return the this .name; . 5 } . 6 }; . 7 . 8 // default, our two attributes are listed: . 9 for (the let Key in User) Alert (Key); // name, toString 10 . 11 12 is Object.defineProperty (User, " toString " , { 13 is Enumerable: to false 14 }); 15 16 //ToString now gone: 17 for (the let Key in the User) Alert (Key); // name
Not configurable
By setting configurable flag to control whether a property can be modified
. 1 the let User = {}; 2 . 3 Object.defineProperty (User, " name " , { . 4 value: " John " , . 5 Writable: to false , . 6 Configurable: to false . 7 }); . 8 . 9 // can not be modified or user.name its flag 10 // All the following operations are ineffective: . 11 // the user.name = "Pete" 12 is // Delete the user.name 13 is // Defineproperty (User, "name", ...) 14 Object.defineProperty(user, "name", {writable: true}); // 错误
Object.defineProperties
Define multiple attributes, syntax, and examples are as follows:
1 Object.defineProperties(obj, { 2 prop1: descriptor1, 3 prop2: descriptor2 4 // ... 5 }); 6 Object.defineProperties(user, { 7 name: { value: "John", writable: false }, 8 surname: { value: "Smith", writable: false }, 9 // ... 10 });
Object.getOwnPropertyDescriptors(obj)
A description of all properties acquired
Syntax: let
clone =
Object .
defineProperties
(
{
}
,
Object .
getOwnPropertyDescriptors
(
obj)
)
;
2, property getter and setter
getter and setter
1 2 let obj = { 3 get propName() { 4 // getter, the code executed on getting obj.propName 5 }, 6 7 set propName(value) { 8 // setter, the code executed on setting obj.propName = value 9 } 10 };
Access Descriptor
get
- a no function parameters, operating at read attribute,set
- with one parameter function, invoked when the property is set,enumerable
- the same as the data attributes,configurable
- the same data attribute.
compatibility
. 1 function the User (name, Birthday) { 2 the this .name = name; . 3 the this .birthday = Birthday; . 4 . 5 // Age is calculated from the current date and the date of birth . 6 Object.defineProperty ( the this , " Age " , { . 7 GET () { . 8 the let todayYear = new new a Date () the getFullYear ();. . 9 return todayYear - the this .birthday.getFullYear (); 10 } . 11 }); 12 is } 13 is 14 the let John =new new the User ( " John " , new new a Date ( 1992 , 6 , 1 )); 15 16 Alert (john.birthday); // Birthday is accessible 17 Alert (john.age); // ... Age is also available visit
This example mainly solved, stores a value of a can be interchangeable when.
3, prototypal inheritance
Prototype
Prototype is hidden inside, but can be set by _proto_
let animal = {
eats:true
};
let rabbit = {
jumps:true
};
rabbit._proto_=animal;
alert(rabbit.eats);
alert(rabbit.jumps);
4, function prototypes
F.prototype
Attributes and[[Prototype]]
different.F.prototype
The only role is: Whennew F()
is called, it sets a new object[[Prototype]]
.F.prototype
The value should be an object or null: Other values will not work."prototype"
When property set having only a constructor such special effects, and withnew
call.1 let animal = { 2 eats: true 3 }; 4 5 function Rabbit(name) { 6 this.name = name; 7 } 8 9 Rabbit.prototype = animal; 10 11 let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal 12 13 alert( rabbit.eats ); // true
5, a native of prototype
- All of the built-in objects follow the same pattern:
- Methods are stored on the prototype object
Array.prototype
(Object.prototype
, ,Date.prototype
etc.). - Only stores data objects themselves (array elements, object attributes, date).
- Methods are stored on the prototype object
- The method of the same basic data types stored on a prototype of the object to be packaged:
Number.prototype
,String.prototype
andBoolean.prototype
. Onlyundefined
andnull
no packaging objects. - The prototype may be built-in objects is filled with new or modified methods. But doing so is not recommended. Only when a new method has not been supported by the JavaScript engine when it could allow to do so.
6, prototyping
- Object.create (proto [, descriptors]) - using given
proto
as[[Prototype]]
to create an empty object. - Object.getPrototypeOf (obj) - return
obj
of[[Prototype]]
(and__proto__
the same getter). - Object.setPrototypeOf (obj, proto) - to
obj
a[[Prototype]]
setproto
(and__proto__
setter same). - Object.keys (obj) / Object.values (obj) / Object.entries (obj) - Returns the name of the attribute itself contains / values / key-pair array.
- Object.getOwnPropertySymbols (obj) - Returns an array of names of all the attributes symbol itself.
- Object.getOwnPropertyNames (obj) - Returns an array of all the property name string itself.
- Reflect.ownKeys (obj) - Returns an array containing all the names of their own property.
- obj.hasOwnProperty (key): If you
obj
have namedkey
their own attributes (non-inherited come), returntrue
.
We also made clear __proto__
that [[Prototype]]
the getter / setter, location Object.prototype
, and other methods of the same.
We can create an object without the aid of prototype, that is Object.create(null)
. These objects are used as a "pure dictionary" was for them "__proto__"
as the key is not the problem.
All attributes method returns an object (such as Object.keys
and others) - return "their" property. If we want to inherit them, we can use for...in
.