js进阶
1.IFE(立即调用函数表达式)
1.示例
(function(){
//匿名函数自调用
console.log('232323232')
})()
2.作用
- 隐藏实现
- 不会污染外部命名空间,一般为全局命名空间
3.向外暴露一个全局函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
<script>
(function(){
var a=1;
function test(){
console.log(++a)
}
window.$=function(){
return {
test:test
}
}
})()
$().test()
</script>
</html>
2.this
1.js中call函数的用法
A.call(B,x,y)
1`改变函数A的this指向,使之指向B;
2` 把A函数放到B中运行,x和y是A函数的参数。
//父类 Person
function Person() {
this.sayName = function() {
return this.name;
}
}
//子类 Chinese
function Chinese(name) {
//借助 call 实现继承
Person.call(this);
this.name = name;
this.ch = function