ES6 the new objects in simple terms -5 -1 how to create objects

Strengthen object properties:



Can be new Object(), method, use or literal flag (flag initialization) to initialize the object. An object initializer, in braces / braces ( a zero or a comma more object attributes names and their associated values composition) comprising separated list of configuration. The above quote is about how to create a new object. The following code. a is an empty object, null object has only one property. That is _proto_ use object.create to create objects, you need to pass parameters, here pass null. It really is an empty object, and which have nothing Object.create grammar new ES6, producing objects of a real nothing. To call any of its methods are not available, for example, the following method calls the toString created above example, there are still a number own toString method ES6 create another way, passing parameters Object.prototype. C on and then the syntax to create a target ES5 is the same as the. Equivalent to {} = new Object () output d objects look the same, and a look object is, an amount which is from the surface if the object is made is a function of the non-literal ES6 is more able to empty It can have a real empty object. ES5 when production permit is not really empty object. Unless is generated, and then delete this property. ES2015 is ES6, ES6 since 2015 published. Object.create(){}































The new mark for ES6



Obtaining object attributes, the following two are equivalent

New syntax, ES6 objects will be more stringent

Custom properties, not behind a small defines a. Behind a cover in front of a

Abbreviations object

If the left is the right of a string is a variable that you can only use the following shorthand way.

Dynamic attribute names, dynamic key

Want a program under the name here, this becomes a key target of

this writing is in fact not a, output is the name, name is the key

Object to declare null objects,

doing so is too much trouble, can not be defined at the beginning of time put the value of the name into them.

So ES6 provides a new syntax. Direct [name], enclosed in square brackets, which is called the dynamic attribute name. Such es6 know you want a variable name, rather than a string name

JS this in the name without the quotation marks is a giant pit, a lot of new people here do not understand in the end is a variable or a string. Normally quoted is the best.

get&set

Some of the values of age, has been modified. Can you do some limitations on the value of age.

Read and write operation attribute customized. Reading is the time to get age is a function of access, write access time is a function of the set age. We make some judgment functions written inside.


There is such a age, it appears to be a value, in fact he is a get gae access and set age accessor, so there is a property called age. This is a dynamic property.

So underlined age is necessary? It may not be necessary.
o.age actually two functions. When you do read o.age time, the equivalent of a visit to get age function.


Add a property to the window,. To add windows to provide a property with Object.defineProperty get.
So there is a window object's properties,

access the value of a, each time changing, because we control a top of the reading process. Each read window.a actually get read return value of the function. So that we can do at the same time window equal to 1, 2,

so a is equal to 1, 2, while the final returns true is true.

The property name can do arithmetic calculation property

Extended Attributes


Write directly equal obj1 obj2, will make obj1 and obj2 is the same object, they just kept address of the object only.

Obj2 a modified value of 4. 4 is then the value of obj1. They are the same object. So it is necessary to do a full copy. They make different objects.

How to prove it is the same object? With three equal signs a look.

The simplest full copy of obj2 to define an empty object, then the for loop, the copy inside each object obj1 to obj2

this time to go to change the value of obj2, then obj1 value does not change. Because I own a a. Not your a. So there is no relationship between them

Shorthand way Object.assign

A copy key for each object obj1 behind and in front of the object to the value {}. The foregoing object and as a return value, to return back. We get the obj2

this time to seek to modify the value of 1 2 does not change 

so save for some cycles. This is the syntax ES6 provided.

ES6 also provides a single point operator grammar manner




Comprehensive Grammar

call obj4 function of x.

Change the prototype


Any object has underlined the value of proto, but es6 clearly defined you do not own to visit, this value is left to the browser uses. It's just to remember where it is in the common attributes.
is not a property, a total of toString is its property inside the.

a.valueOf is also a function.


If you do not have any of a a. toString and valueOf where they come from? See the figure


__proto__ actually Object.prototype. This object pointing Object.prototype

Object.prototype which we often have these properties and methods.

If you want a hidden attribute change it, declare a b, there is a function called sayHi,

I wanted a sayHi also have this property, time parameters Object.create create incoming b. Now a is an empty object, but there are a sayHi

i.e. a__proto__ and b are equal. We called this relationship b is the object of a prototype

before a prototype is Object.prototype. Now a prototype is b, that is, that all the b some properties can be accessed directly via a.


So is there a toString it? There's

a prototype still a prototype. In that there is toString


b prototype object is


the standard es6 Do not use an underscore proto at any time

getPrototypeOf get to the object's prototype

Providing a method to get a prototype, we recommended getPrototypeOf

with hasOwnPropertype judge there is no __proto__ property determination. But instead can output directly with a .__ proto__. Here remember it.

__proto__ is not the standard to be see.


js object with JSON an object distinction,

JSON is a data language. No function
 

End


 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11824890.html
Recommended