Commonly used built-in objects in JS are: arrays, Boolean classes, Number classes, and strings. The commonly used methods are described below.
Arrays in JS
1. Array declaration
① Literal declaration Use [] to declare an array directly:
var arr=[1,2,"3",true,null,undefined];
In JS, the same array can store various data types.
②Use the new keyword to declare: var arr = new Array(); The
parameter can have three forms:
>>> No parameter is passed, which means to create an empty array with a length of 0;
>>> A value is passed in, which means the creation length is An array of lengths. But the length of an array in JS can change dynamically at any time;
>>> passing in multiple values means creating an array and using these values as array elements.
2. Read, write, add and delete elements in the array:
①Use [] to read the array elements.
②The operation of adding and deleting elements in the
array: >>> Delete an element in the array: delete arr[1];
>>> .push(): Append an element at the end of the array
>>> .pop(): Denote the deletion of the array The last element
>>> .shift(): delete the first element of the array
>>> .unshift(): at the beginning of the array, insert a new element
3. Various methods of arrays in JS:
①.join(" -"): Pass in a delimiter, indicating that the array is concatenated into a string with the specified delimiter.
If the parameter is set to empty, the default is comma-separated.
②.
[1,2].concat([3,4])-->[1,2,3,4]
[1,2].concat([3,4,[5,6]])-->[ 1,2,3,4,[5,6]]
③.sort(): Sort the array.
>>> By default, it will be sorted in ascending order according to ASCII code;
>>> Parameters can be directly passed to the comparison function:
arr.sort(function(){
return ab;//ascending
return ba;//descending
});
④. reverse(): Reverse the order of the elements of the original array.
⑤.slice(start,end): Receive two values, intercept a subarray, and return.
>>> Pass in one value, which means from the current subscript to the end;
>>> Pass in two values, means from start to end, including start but not end
>>> start and end can be positive or negative. A positive number means from left to right, starting from 0;
a negative number means from right to left, starting from -1.
⑥.indexOf(): Find the subscript of the first occurrence of an array element.
lastindexOf(): Find the subscript of the last occurrence of an array element
⑦.forEach(); Receive a callback function to traverse the array
[this function cannot be used before IE8 Use]
.forEach(function(item,index){
console.log(item+"
});
⑧.map():数组映射,在回调函数中,可以有返回值。表示将返回的每一个值,赋给新数组。
[这个函数在IE8之前不能用]
var arr1=arr.map(function(item,index,arr){
return item;
});
Boolean类
Boolean类有两种声明方式:字面量声明和new关键字声明。
1.使用new关键字声明的变量,用typeof检测是object类型;
2.使用字面量声明的变量,用typeof检测是boolean类型
如果不用new关键字而直接作为Boolean()函数使用,则可以将各种数据类型转换为boolean类型的变量
Number类
Number类的常用方法有:
1、.toFixed(2):将数值转为字符串,同时四舍五入,保留指定位数的小数。
2、.toString():将数值转为字符串
3、toLocaleString():将数值按本地格式转为字符串,千位分隔符,每三个数为一组。
(12,345,678.5678)
4、.valueOf():将对象类型,取到基本数字值。
字符串
1、.length属性返回字符串的长度
2、字符串中读取每个字符:
str[0] 或者 str.charAt(0)
3、其他常用方法:
①.toLowerCase():把字符串转化为小写
②.toUpperCase():把字符串转化为大写
③.substring(index1,index2):返回index1和index2之间的字符串,包括index1对应的字符,不包括index2对应的字符
④.indexOf():查找某个字符值在字符串中首次出现的位置
⑤.lastIndexOf():在字符串中查找指定的字符或子串最后一次出现的位置
⑥.split("") :传入指定字符,将字符串分割为多个子串,返回一个字符串类型的数组
⑦.replace(/h/g,"*"):如果第一个参数是字符串,则只能替换掉第一个字符串;如果想要替换多个,可以使用正则表达式。
JS中自定义对象的声明
1.使用字面量声明:
var obj={
age:14,
name:"zhangsan",
func:function(){
}
}
注意:
①属性和属性值之间用:分隔表示键值对
②多对属性之间,用逗号分隔
③对象的键,可以是各种数据类型(除了数组[]和对象{})。
对象的值,可以是各种数据类型
2.使用new 关键字声明
var obj1=new Object;
obj1.name="zhangsan";
obj1.func=function(){
}
3.对象属性和方法的读写
①使用、调用
在对象外部,可以使用obj.age
在对象内部,可以使用obj.age或者this.age
注意:在对象中,直接使用变量名默认是使用全局的变量。如果要使用对象的属性,那么必须用对象名或者this调用
②使用[key]调用:obj[age] obj[1] obj["name"]
如果对象的键包含特殊的字符,无法使用.调用时,可以使用中括号调用。
4.obj.hasOwnProperty(keys) 检测一个键是否属于一个对象
5.delete obj.name 删除对象的属性