Reference type
(1) What is a reference type?
The information we study is often diverse and complex, often non-isolated, and there is a connection between information, because things in reality are connected to one another.
So we need to re-abstract the simple data into a data structure that is a reference type.
(2) What is the difference between a reference type and a basic type?
We know that when we manipulate basic data types, we can declare variables, such as var a = 1; So we create a space in memory, with a to represent, a is equivalent to a container of data 1.
At this point we create a variable B, which opens up a memory space called B, what will compare A and B?
Obviously, the comparison between the two approaches is expected, A and B are congruent, that is, the basic type is accessed by value
Again, let's create a reference type data
Here we create an object person, but here the variable person is not a container for the object type data, but rather an address (or a pointer to the object).
Here we see the difference, if the reference type is accessed by value, the first comparison above should return true. The beginning of Person1 and Person2 were two completely different objects, except that two people were called Jack.
The example above shows that the reference type is accessed by reference and does not directly access the memory of the object.
In addition, basic types and reference types are stored differently in memory, and basic type data exists in the stack memory, references of reference types are stored in the stack, but the actual objects are stored in the heap area.
(3) What are the reference types?
1.Object type
An object is a composite value that aggregates many values (primitive values or other objects) and can access them by name. It can also be seen as an unordered collection of attributes, each of which is a name/value pair.
In ECMAScript, all objects are not created equally.
In general, there are three objects that can be created and used: local objects , built-in objects , and host objects .
Local objects
ECMA-262 defines local objects (native object) as "objects provided by ECMAScript implementations that are independent of the hosting environment." In simple terms, a local object is a class defined by ECMA-262 (reference type). They include:
Built-in objects
ECMA-262 defines the built-in object (built-in object) as "all objects that are provided by the ECMAScript implementation, independent of the hosting environment, and appear when the ECMAScript program starts executing." This means that the developer does not have to instantiate the built-in object explicitly, it has been instantiated. ECMA-262 only defines two built-in objects, Global and Math (they are also local objects, by definition, each built-in object is a local object)
Host Object
All non-local objects are host objects (host object), which is the object provided by the hosting environment implemented by ECMAScript.
All BOM and DOM objects are host objects.
Methods for creating a single object
1. Object literals
The object literal is a mapping table with several name/value pairs, separated by a colon in the middle of the name/value pair and enclosed in curly braces for the entire mapping table. The property name can be a JS identifier or a string literal (including an empty string), the value of the property can be any type of JS expression, the value of the expression is the value of this property (the method is most popular)
var empty = {}; object with no attributes var point = {x:0, y:0}; Two attributes var Point2 = {x:point.x, y:point.y}; More complex values var book = { "Main-title": "JavaScript", //attribute names have spaces, must be represented by a string "sub-title": "The Definitive Guide", the//attribute name has hyphens and must be represented by a string "for": "Ali audiences", //for is a reserved word, so it must be quoted auther: { //The value of this property is an object FirstName: "David", //Note that the attribute names here are not quoted surname: "Flanagan" }};
2. Creating an object from new
The new operator creates and initializes an object, followed by a function call after the keyword new, which is called construct the number of the.
var a = new Array (); var d = new Date (), var r = new RegExp ("JS");
3. Prototypes
Each JS object (except NULL) is associated with another object. This object is a prototype. Each object inherits properties from the prototype.
4.object.create ()
Object.create () is used to create a new object, the first parameter is the prototype of the object, the second parameter is optional, to further describe the object properties
Object.create () is a static function, not a method to be called by an object
1 var O1 = object.create ({x:1,y:2}); O1 inherits attributes X and Y
2017-10-08
20:43:39