Many high-level programming languages are object-oriented, such as C, C# and Java and other high-level programming languages. So what are the basic requirements for an object-oriented language? Now let's talk about some object-oriented knowledge.
An object-oriented language needs to provide developers with four basic capabilities:
Since ECMAScript supports these requirements, it can be regarded as object-oriented. In ECMAScript, you cannot access the physical representation of an object, only its reference. Every time an object is created, what is stored in the variable is a reference to the object, not the object itself. Therefore, JavaScript is a weakly typed web script language based on object-oriented.
1. Object type
The Object type contains properties (also called fields) and methods (also called functions). Therefore, there are key points that must be explained when creating the Object type. There are generally two ways to create Object type numbers:
(1) Use new operator
1 2 3 4 5 6 7 8 9 10 11 |
|
Output: object
Zhang San
23
I am Chinese!
(2) Literal representation
1 2 3 4 5 6 7 8 9 10 11 |
|
Output: Same as above
(3) Comprehensive use
When we pass multiple parameters, we need to enter them in order. In order to solve this cumbersome process, we can encapsulate multiple parameters
To an Object type, use the Object type as a parameter. We can also judge the non-existent or extra parameters, which makes it easier to call the function
Count and pass parameters.
1 2 3 4 5 6 7 8 9 10 |
|
Output: Zhang San
23
2. Array type
Arrays in ECMAScript are very different from other languages. The elements in an array in JS can be of any data type, and the size of the array is also
It can be adjusted. It reflects from the side that JS is a weakly typed language. There are two ways to create Array type numbers:
(1) Use new operator (new can be omitted)
1 2 3 |
|
Index subscripts start from 0
1 2 |
|
Create an array with ten elements
1 2 3 4 |
|
(2) Use literals to create arrays
1 2 3 4 |
|
Create a complex array (can be of various types)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The output result of the page is:
3. Methods in objects
(1)Conversion method
Objects or arrays have toLocaleString(), toString() and valueOf() methods. Among them, toString() and valueOf() will return
no matter who is overridden.
return the same value. The array concatenates each value in string form, separated by commas.
1 2 3 4 5 |
|
By default, array strings are separated by commas. If you use the join() method, you can use different delimiters to build this string
1 2 3 4 5 |
|
页面输出的结果为:
(2)栈方法
ECMAScript数组提供了一种让数组的行为类似于其他数据结构的方法。也就是说,可以让数组像栈一样,可以限
制插入和删除想的数据结构。栈是一种后进先出的数据结构,也就是最新添加的元素最早被移除。而栈元素的插入和
移除,只发生在栈的顶部。ECMAScript为数组专门提供了push()和pop()方法。
栈操作数组元素的图片:
push()方法可以接受任意数量的参数,把它们逐个添加到数组的末尾,并返回修改数组的长度。而pop()方法则从
数组末尾移除最后一个元素,减小数组的length值,然后返回移除的元素。
1 2 3 4 5 6 7 8 9 10 |
|
输出:
(3)队列方法
栈方法是后进先出,队列方法是先进先出。队列在数组的末端添加元素,从数组的前端移除元素。通过push()向
数组末端添加一个元素,然后通过shift()方法从数组的前端移除一个元素。
队列操作数组元素的图片
1 2 3 4 5 6 7 8 9 10 |
|
输出:
ECMAScript还为数组提供了一个unshift()方法,它和shift()方法的功能完全相反。unshift()方法为数组的前端添加
一个元素。
1 2 3 4 5 6 7 8 9 10 |
|
输出:
(4)重排序方法
数组中已经存在两个直接用来排序的方法:reverse()和sort()。
reverse():逆向排序
1 2 3 4 |
|
sort():从小到大排序
1 2 3 4 |
|
如果我们实验次数多的话可能回遇到这样的问题,
1 2 3 |
|
我们从结果可以看出,这违背了我们想要的结果,解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
(5)操作方法
JS为操作已经包含在数组中的元素提供了许多的方法。concat()方法可以基于当前数组创建一个新数组。slice()方
法可以基于当前数组获取指定区域元素并创建一个新数组。splice()方法主要用途是向数组的中部插入元素。
a
1 2 3 4 |
|
b
1 2 3 4 |
|
c
1 2 3 4 |
|
splice中的删除功能
1 2 3 4 |
|
splice中的插入功能
1 2 3 4 |
|
splice中的替换功
1 2 3 4 |
|
以上就是关于JavaScript对象和数组的详细介绍,希望对大家的学习有所帮助。