基础笔记
循环
outter: for (var i = 0; i < 3; i++) {
inner:for (var j = 0; j < 4; j++) {
if (i==2) {
break outter;
}
console.log('i:',i,' j:',j);
}
}
加上outter
、inner
这些label后,可以指定删除某个循环。
ECMAScript引用类型
Object类型
var obj={name:"feeU"}
console.log(obj.name);
//"feeu"
字符串类型
charCodeAt()
方法返回指定位置的字符的Unicode
编码。 默认是0,可以指定位置。
fromCharCode
是String
的一个方法,把编码转换成字符串。
var s="this is DieSSdki sile";
console.log(s.charCodeAt());
//116
console.log(String.fromCharCode(85));
//U
Global对象
属性:
Infinity
NaN
undefined
null
方法:
eval()
isFinite()
isNaN()
parseFloat()
parseInt()
decodeURI()
decodeURIComponent()
encodeURI
encodeURIComponent()
Math对象
属性:
Math.PI(圆周率)
Math.E(自然数对数)
方法:
Math.ceil()/Math.floor()/Math.round()
Math.random()
Math.max()/Math.min()
数组
检查数组
Instanceof
Array.isArray()
var arr=[32,34,1,434,34]
//undefined
arr instanceof Array
//true
arr[2] instanceof Object
//false
Array.isArray(arr)
//true
转换与排序
Object.keys(obj)
Object.keys()
是Object
的静态方法,把对象的key
值转换成数组。
var obj={a:1,b:23,c:"f"};
Object.keys(obj)
//['a','b','c']
split()
把字符串安照指定内容分割成数组。
toString()
把数组转换成字符串。
join()
用指定内容拼接数组成一个新的字符串。
sort()
按照字符串的序号排序数组。
reserve()
反向排序。
push()/pop()
向数组末尾添加或者删除一个元素。
shift()/unshift()
向数组开头删除或者添加一个元素。
concat()
把两个数组组合成一个数组。
slice(a,b)
从数组中取出一定的元素,返回由取出元素组成的数组。
splice()
从指定位置开始删除指定个数的数组,并在原来数组开头添加后续元素。
var arr=[32,34,1,434,34]
//undefined
arr.splice(2,3,"hei","dogo")
[1, 434, 34]
arr
[32, 34, "hei", "dogo"]
indexOf()/lastIndexOf()
查找指定元素的位置。
every()
比较所有数组元素,所有为true
就返回true
。
var arr=[32,34,1,434,34]
//undefined
var e=arr.every(function(m){return m>0;});
//undefined
e
//true
some()
比较所有数组元素,只要有一个为true
就返回true
。
var arr=[32,34,1,434,34]
undefined
var e=arr.some(function(m){return m>110;});
undefined
e
true
filter()
返回符合要求的元素组成的数组。
var arr=[1,4,563,2,5,4642,235,23];
undefined
var f=arr.filter(function(m){return m>10;})
//undefined
f
//[563, 4642, 235, 23]
map()
对每个数组元素进行操作,返回由新元素组成的数组。
var arr=[1,4,563,2,5,4642,235,23];
//undefined
var m=arr.map(function(m){return m+10;});
//undefined
m
//[11, 14, 573, 12, 15, 4652, 245, 33]
array.forEach(callback[, thisArg])
遍历、循环每一个数组元素。
callback 在数组每一项上执行的函数,接收三个参数:
1.currentValue
当前项(指遍历时正在被处理那个数组项)的值。
2.index
当前项的索引(或下标)。
3.array 数组本身。
thisArg 可选参数。用来当作callback 函数内this的值的对象。
reduce()
这个方法用户迭代、递归。
arr.reduce(callback,[initialValue])
callback
执行数组中每个值的函数,包含四个参数:
1.previousValue 上一次调用回调返回的值,或者是提供的初始值(initialValue)
2.currentValue 数组中当前被处理的元素
3.index 当前元素在数组中的索引
4.array 调用 reduce 的数组
initialValue 作为第一次调用 callback 的第一个参数。
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
}, 10);
//20
reduceRight()
与reduce()
相似,顺序相反。
REPL命令
REPL
— 交互式解释器环境。 R(read)
、E(evaluate)
、P(print)
、L(loop)
输入值后,交互式解释器会读取输入内容并对它求值,再返回结果,并重复此过程。
ctrl + c - 退出当前终端。
ctrl + c 按下两次 - 退出 Node REPL。
ctrl + d - 退出 Node REPL.
向上/向下 键 - 查看输入的历史命令
tab 键 - 列出当前命令
.help - 列出使用命令
.break - 退出多行表达式
.clear - 退出多行表达式
.save filename - 保存当前的 Node REPL 会话到指定文件
.load filename - 载入当前 Node REPL 会话的文件内容。
Last updated
Was this helpful?