First, create the object and initialize it
A. Creating objects and methods using new
<<! DOCTYPE html>
b, the literal representation method
<<! DOCTYPE html>"Content-type"Content="Text/html;charset=utf-8"/> Arrays and objects in <title>javascript </title> <script type="Text/javascript"Src="./out.js">varbox={name:"Zhang San", Age: at, Run:function () {return "I am a Chinese!! " } }; document.write (typeofbox); document.write (Box.name); document.write (Box.age); document.write (Box.run ()); </script>C, integrated use
To be blunt is to use them together.
function Box (obj) { if(obj.name!=undefined) document.write (obj.name+ "<br/>"); if (obj.age!=undefined) document.write (obj.age+ "<br/>"); if (obj.love!=undefined) document.write (obj.love+ "<br/>");} var obj={ name:"Zhang San" , age : +, box (obj);
Two, array type
Three ways to create a new array:
var box=new array (1,2,3,4);d ocument.write (typeof box);d ocument.write (box), var box2=new array (box2[3]=4;box2[6]=) 25;document.write (typeof Box2);d Ocument.write (box2); var box3=[1,3,4,44,5,5,23];d Ocument.write (typeof Box3); document.write (BOX3);
(1) Conversion method
objects or arrays have tolocalestring (), toString (), and valueof () methods. where ToString () and valueof () no matter who they rewrite, will return
return the same value . The array will stitch each value in string form, separated by commas.
By default, array strings are separated by commas. If you use the Join () method, you can use different delimiters to build this string
(2) Stack method
The ECMAScript array provides a way for the array to behave like other data structures. In other words, you can make the array like a stack, which can be limited
To insert and delete the data structure you want. The stack is a last-in-first-out data structure, where the newly added elements are first removed. While the insertion of stack elements and
Removed, only occurs at the top of the stack. The ECMAScript provides the push () and Pop () methods specifically for the array.
Picture of the stack manipulation array element:
The push () method can accept any number of arguments, add them to the end of the array one by one, and return the length of the modified array. and the Pop () method
Removes the last element at the end of the array, reduces the length value of the array, and then returns the removed element.
| 12345678910 |
var box=[1,2,3,4]; document.write(box+"<br/>"); box.push(5,6);//在数组末尾添加元素 document.write(box+"<br/>"); document.write(box.push(7,8)+"<br/>");//在数组末尾添加元素,并返回添加元素后数组的长度 document.write(box+"<br/>"); box.pop();//移除数组末尾的元素 document.write(box+"<br/>"); document.write(box.pop()+"<br/>");//移除数组末尾的元素,并返回移除的元素 document.write(box); |
Output:
(3) Queue method
The Stack method is LIFO, and the queue method is FIFO. The queue adds elements at the end of the array, removing elements from the front end of the array. Through push () to
An element is added at the end of the array, and an element is removed from the front end of the array through the shift () method.
Picture of the queue manipulation array element
| 12345678910 |
var box=[1,2,3,4]; document.write(box+"<br/>"); box.push(5,6);//在数组末尾添加元素 document.write(box+"<br/>"); document.write(box.push(7,8)+"<br/>");//在数组末尾添加元素,并返回添加元素后数组的长度 document.write(box+"<br/>"); box.shift();//移除数组前端的一个元素 document.write(box+"<br/>"); document.write(box.shift()+"<br/>");//移除数组前端的一个元素,并返回移除的元素 document.write(box); |
Output:
ECMAScript also provides an array of unshift () methods, which are the exact opposite of the shift () method. The Unshift () method is added to the front end of the array
An element.
?
| 12345678910 |
var box=[1,2,3,4]; document.write(box+"<br/>"); box.unshift(0);//在数组的前端添加一个元素 document.write(box+"<br/>"); document.write(box.unshift(-1)+"<br/>");//在数组的前端添加一个元素,并返回添加元素会数组的长度 document.write(box+"<br/>"); box.pop();//在数组末尾移除元素 document.write(box+"<br/>"); document.write(box.pop()+"<br/>");//在数组末尾移除元素,并返回移除元素后数组的长度 document.write(box); |
Output:
(4) Reorder method
There are already two methods in the array that are directly used for sorting: reverse () and sort ().
Reverse (): reverse sort
?
| 1234 |
varbox=[1,2,3,4,5]; box.reverse(); document.write(box+"<br/>");//输出54321 document.write(box.reverse());//再次进行逆序,输出12345 |
Sort (): from small to large
?
| 1234 |
varbox=[3,2,6,4,1,5]; box.sort(); document.write(box+"<br/>");//输出1,2,3,4,5,6 document.write(box.sort());//再次从小到大进行排序 |
If we experiment a lot, we might get back to this problem.
?
| 123 |
varbox=[0,15,10,1,5]; box.sort(); document.write(box);//输出0,1,10,15,5 |
We can see from the results that this violates the result we want, the solution:
?
| 1234567891011121314 |
function compare(value1,value2){ if(value1<value2){ return -1; } else if(value1>value2){ return 1; } else{ return 0; } } var box=[0,15,10,1,5]; box.sort(compare); document.write(box);//输出0,1,5,10,15 |
(5) Operation method
JS provides a number of methods for manipulating elements that are already contained in an array. The Concat () method can create a new array based on the current array. Slice () Fang
method to get the specified range element and create a new array based on the current array. The main purpose of the splice () method is to insert elements into the middle of the array.
A
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.concat(6);//创建新数组,并添加新元素 document.write(box1+"<br/>");//输出1,2,3,4,5,6, document.write(box);//原数组不变化 |
B
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.slice(2);//取出索引为2以后的元素组成新的数组 document.write(box1+"<br/>");//输出3,4,5 document.write(box);//原数组不变化 |
C
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.slice(2,3);//取出索引为2到3之间的元素组成新的数组 document.write(box1+"<br/>");//输出3 document.write(box);//原数组不变化 |
Delete function in Splice
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.splice(0,2);//截取索引为0开始的两个元素组成新的数组 document.write(box1+"<br/>");//返回截取的元素1,2 document.write(box);//当前数组被截取的元素被删除,输出3,4,5 |
Insert function in Splice
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.splice(4,0,6);//索引为4的位置插入了一个元素 document.write(box1+"<br/>");//返回新的数组为空,并没有截取元素 document.write(box);//当前数组索引为4的位置插入一个元素1,2,3,4,6,5 |
The substitution function in splice
| 1234 |
varbox=[1,2,3,4,5]; varbox1=box.splice(4,1,6);//索引为4的元素被替换,替换下来的元素组成新数组 document.write(box1+"<br/>");//返回新的数组5 document.write(box);//被替换后的原数组1,2,3,4,6 |
The above is a detailed introduction of JavaScript objects and arrays, hoping to help you learn.
Values and objects in JavaScript