1.基本概念
2.闭包,call,apply
2.1.闭包:以下是官方解释,A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).
在js中,根据链式作用域的特点,子对象可以访问父对象,父对象则不能访问子对象。这种在函数内部嵌套定义子函数就形成了闭包。闭包会使得函数的变量都保存在内存中。
闭包的作用:
1.向setTimeout函数传递带参数的函数,例如:

window.onload = function () {
setTimeout(fun1(3,5), 3000);
}
function fun1(i,j){
return function(){
alert(i+j);
}
}
setTimeout(fun1(3,5), 3000);
}
function fun1(i,j){
return function(){
alert(i+j);
}
}
2.与对象方法绑定。
例如:场景1

window.onload = function () {
var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = associateObjWithArgs(i);
}
}
function associateObjWithArgs(index) {
return (function () {
alert("第" + index + "个button")
});
}
var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = associateObjWithArgs(i);
}
}
function associateObjWithArgs(index) {
return (function () {
alert("第" + index + "个button")
});
}
当然也可以写成匿名的:

window.onload = function () {
var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function (index) {
return function () {
alert("第" + index + "个button")
}
} (i)
}
}
var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function (index) {
return function () {
alert("第" + index + "个button")
}
} (i)
}
}
场景2
《关联事件》明天补上
3.封装。
2.2.call
2.3.apply
3.正则

字符:
1.普通字符:literal,例如:/abc/
2.自定义字符:[abc]
3.通配符:
\d [0-9]
\w [0-9a-zA-Z]
\s [\f\n\r\t\v]
. [^\n]
\D [^\d]
\W [^w]
\S [^\s]
2.自定义字符:[abc]
3.通配符:
\d [0-9]
\w [0-9a-zA-Z]
\s [\f\n\r\t\v]
. [^\n]
\D [^\d]
\W [^w]
\S [^\s]
4.特殊字符:
\f 换页符
\n 换行符
\r 回车
\t 制表符
\v 垂直制表符
\f 换页符
\n 换行符
\r 回车
\t 制表符
\v 垂直制表符
5.字符转义:
\
6.字符选择:
|
量词:
{n} 重复n次
{m,n} 重复m到n次
{m,} 重复m次以上
? {0,1}
* {0,}
+ {1,}
{m,n} 重复m到n次
{m,} 重复m次以上
? {0,1}
* {0,}
+ {1,}
模式:
greedy
lazy(在修饰匹配次数的特殊符号后再加上一个 "?" 号)
举例:xxxdxxxdyyyyyd
d\w+d 匹配到的是dxxxdyyyyyd
d\w+?d 匹配到的是dxxxd
greedy
lazy(在修饰匹配次数的特殊符号后再加上一个 "?" 号)
举例:xxxdxxxdyyyyyd
d\w+d 匹配到的是dxxxdyyyyyd
d\w+?d 匹配到的是dxxxd
分组与引用
捕获型分组 ()
捕获型分组 ()
非捕获型分组 (?:)
反向引用 \1,\2……
反向引用 \1,\2……
断言: (assert position only)
静态位置断言:
^ 字符串开始
$ 字符串结尾
\b 单词边界
^ 字符串开始
$ 字符串结尾
\b 单词边界
动态位置断言(环视):
(?=xxx) positive lookahead
(?!xxx) negative lookahead
(?=xxx) positive lookahead
(?!xxx) negative lookahead

var str = "a22b360ccc";
var x1 = str.replace(/\d/, "~"); //输出 a~2b360ccc
var x2 = str.replace(/\d/g, "~"); //输出 a~~b~~~ccc
var x3 = str.replace(/\d+/g, "~");//输入 a~b~ccc
var x4 = str.replace(/(\d+)/g, "($1)"); //输出 a(22)b(360)ccc
var x5 = str.replace(/(\d{3,})/g, "($1)"); //输出 a22b(360)ccc
var x6 = str.replace(/\d+/g, function (arg) { //(将匹配结果交给函数处理)输出 a32b370ccc
return arg*1 + 10;
});
var x7 = str.replace(/\d+(?=c)/g, "~"); //输出 a22b~ccc
var x8 = str.replace(/\d+(?!c)/g, "~"); //输出 a~b~0ccc
var x9 = str.replace(/\d+?(?!c)/g, "~"); //(lazy模式)输出 a~~b~~0ccc
var y1 = str.replace(/\d+(?!c)(?!\d)/g, "~"); //(对照x8来看)输出 a~b360ccc
var y2 = str.replace(/(\S)\1/g, "~"); //(判断有重复出现的字符)输出 a~b360~c
var y3 = str.replace(/(\S)\1{2,}/g, "~"); //输出 a22b360~
var x1 = str.replace(/\d/, "~"); //输出 a~2b360ccc
var x2 = str.replace(/\d/g, "~"); //输出 a~~b~~~ccc
var x3 = str.replace(/\d+/g, "~");//输入 a~b~ccc
var x4 = str.replace(/(\d+)/g, "($1)"); //输出 a(22)b(360)ccc
var x5 = str.replace(/(\d{3,})/g, "($1)"); //输出 a22b(360)ccc
var x6 = str.replace(/\d+/g, function (arg) { //(将匹配结果交给函数处理)输出 a32b370ccc
return arg*1 + 10;
});
var x7 = str.replace(/\d+(?=c)/g, "~"); //输出 a22b~ccc
var x8 = str.replace(/\d+(?!c)/g, "~"); //输出 a~b~0ccc
var x9 = str.replace(/\d+?(?!c)/g, "~"); //(lazy模式)输出 a~~b~~0ccc
var y1 = str.replace(/\d+(?!c)(?!\d)/g, "~"); //(对照x8来看)输出 a~b360ccc
var y2 = str.replace(/(\S)\1/g, "~"); //(判断有重复出现的字符)输出 a~b360~c
var y3 = str.replace(/(\S)\1{2,}/g, "~"); //输出 a22b360~