javascript基本数据类型
1.js常用的基本数据类型包括undefined、null、number、boolean、string
2.js的引用数据类型也就是对象类型Object,比如:Object、array等
1.Number的常用操作
Number.isNaN()
确定传递的值是否是 NaN。Number.isFinite()
方法用来检测传入的参数是否是一个有穷数。Number.toString()
方法可以返回一个字符串,把数字转化成字符串
2.String的常用操作
Str.length
返回字符串中字符编码单元的数量,JavaScript 使用 UTF-16 编码,该编码使用一个 16 比特的编码单元来表示大部分常见的字符。
说明:一个汉字是两个字节也就是16比特,所以对于汉字而言一个汉字对应的length也是一。str.charAt(index)
返回字符串中返回指定的字符。
说明:index一个介于0 和字符串长度减1之间的整数,若是负数则返回为空。Str[index]
返回字符串中返回指定的字符,可以代替上述方法。
说明:index一个介于0 和字符串长度减1之间的整数,若是负数则返回为undefined。str.concat(string2, string3[, ..., stringN])
将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回,concat 方法并不影响原字符串。
说明:处于性能方面的考虑,使用 +或者是+= 来代替concat。str.includes(searchString[, position])
判断一个字符串是否包含在另一个字符串中。
position:可选。从当前字符串的哪个索引位置开始搜寻子字符串,默认值为0。str.endsWith(searchString[, length])
判断当前字符串是否是以另外一个给定的子字符串“结尾”的。
length:可选。作为 str 的长度。默认值为 str.length。
说明:
let a = 'hello,world'
a.endsWith(‘o’,5) //返回true
str.startsWith(searchString[, position])
判断当前字符串是否是以另外一个给定的子字符串“开始”的。str.indexOf(searchValue, fromIndex)
返回查找内容第一次出现的指定值的索引。
fromIndex: 表示开始查找的位置。可以是任意整数,默认值为 0。如果 fromIndex 小于 0,则查找整个字符串(等价于传入了 0)。如果 fromIndex 大于等于 str.length,则必返回 -1。str.search(regexp)
利用正则表达式的查询方法,返回正则表达式在字符串中首次匹配项的索引;否则,返回 -1。str.match(regexp)
返回一个数组包含有所有匹配到的元素,如果你没有给出任何参数并直接使用match() 方法 ,你将会得到一 个包含空字符串的 Array :[""] 。
说明:在match中可以使用正则表达式中的gi,如果没有gi那么它的功能和search很像。str.replace(regexp|substr, newSubStr|function)
返回替换字符串之后的新字符串。
// 几种使用方式
// 1
let a = '123abc'
a.replace('123','abc') // 输出abcabc
// 2
a.replace(/[a-z]+/gi, '123') // 输出123123
// 3
a.replace(/(^[1-9]+)([a-z]+$)/gi, "$2$1") //输出abc123
// 4
function upper(math,p1,p2,offset,string) {
console.log(math) // // 输出匹配到的字符串,输出123abc
console.log(p1,p2) // 输出每一个括号匹配的字符串,// 输出123 abc
console.log(offset) // 输出匹配的偏移量 输出0
console.log(string) // 输出123abc
return [p2,p1].join('-')
}
console.log(a.replace(/(^[1-9]+)([a-z]+$)/gi, upper)) // 输出abc-123
str.slice(beginIndex[, endIndex])
方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。str.split([separator[, limit]])
方法使用指定的分隔符字符串将一个String对象分割成子字符串数组。str.toUpperCase()/str.toLowerCase()
方法将字符串转化成大写或者小写形式。
3.Array的常用操作
array.forEach()
遍历数组。
let a = [1, 2, 3]
a.forEach((item, index, array) => {
console.log(item, index, array)
})
/*
1 0 [1, 2, 3]
2 1 [1, 2, 3]
3 2 [1, 2, 3]
*/
array.push()
添加元素到数组的末尾。array.pop()
删除数组末尾的元素。array.shift()
删除数组最前面。array.unshift()
添加元素到数组的头部。array.indexOf()
找出某个元素在数组中的索引array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
let a = ["1", "2", "3", "4"]
let b = a.splice(2, 0, "5") // 在第2位置(从0开始),不删除元素,插入一个5
console.log(a)
console.log(b)
/*
["1", "2", "5", "3", "4"]
[]
*/
let a = ["1", "2", "3", "4"]
let b = a.splice(2, 1, "5") // 在第2位置(从0开始),删除一个元素即3,插入一个5
console.log(a)
console.log(b)
/*
["1", "2", "5", "4"]
["3"]
*/
4.Object的常用操作
for (variable in object) {statement}
遍历对象,在每次迭代时,variable会被赋值为不同的属性名。Object.keys(obj)
一个表示给定对象的所有可枚举属性的字符串数组。obj.hasOwnProperty(prop)
方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性。
5.其他
*function.call(thisArg, arg1, arg2, ...)/function.apply(thisArg, [argsArray])
修改函数执行的this指针
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price); // 这个地方的this == funciton Food
this.category = 'food';
}
function Toy(name, price) {
Product.call(this, name, price); // 这个地方的this == funciton Toy
this.category = 'toy';
}
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
console.log(cheese.name)
console.log(fun.name)
/*
feta
robot
*/