What is an object
is actually a type, that is, a reference type. The value of an object is an instance of the reference type. A reference type in ECMAScript is a data type that is used to organize data and functionality together. It is also often called a class.
1.Object type
Use the new Object ();
Create with literal volume
| The code is as follows |
Copy Code |
var box = { Name: ' Caibaojian.com ', Age:24 } Output Alert (Box.name) Alert (box[' name ')); var box = { Run:function () { Return ' 123 '; } } alert (Box.run); Print out the entire function code Alert (Box.run ()); Print return value |
You can use the Delete output property www.111cn.net
Delete Box.name
In the actual development, we generally prefer the literal declaration way. Because it is clear, the syntax code is small, but also gives the person a kind of encapsulation feeling
Literal is also the preferred way for a function to pass a large number of optional arguments.
| The code is as follows |
Copy Code |
function box (obj) { alert (obj.name); if (obj.name!=undefined) alert (); } var obj = { Name: ' Caibaojian.com ', Age:24, height:178 }; box (obj); |
Two: Array type
In addition to the object type, the array type is the most commonly used type of ECMAScript. Also, the array type in the ECMAScript differs greatly from the arrays in other languages. Although arrays are ordered, each element of an array in ECMAScript can hold any type. The size can also be adjusted.
There are two different ways to create:
1. Using new
| The code is as follows |
Copy Code |
var box = new Array (); var box = new Array (10); var box = new Array (' Caibaojian ', 24, ' front-end development ', ' Zhongshan '); Alert (typeof box); belongs to type Object |
2. The above can omit new
3. Use literal methods to create
| code is as follows |
copy code |
| var box = []; var box = [' Caibaojian ',]; Var box =[]; box[0]= ' Caibaojian '; Box[1] =; Alert (box); Box.length = 10;//force element amount box[box.length]= ' Caibaojian '; var box = [ { Name: ' Caibaojian ', age:24 }, [1,2,3,new Object ()], ' computer programming ', 25+25, New Array (1,2,3) ; Alert (box); Alert (box[0].name); |