严格模式
1、在严格模式下,变量必须先用var命令声明,然后再使用
2、不能删除已经声明好的变量
严格模式this指向
1、以前在全局作用域函数中的this指向window对象
2、严格模式下全局作用域中的this是undefined
3、严格模式下,如果构造函数不加new调用,this会报错
4、定时器里面的this还是指向window
5、严格模式下函数里面的参数不允许有重名
6、禁止在if()for()里面声明函数
‘use strict’;为整个脚本开启严格模式,下面的代码就会按照严格模式执行代码;
(function(){
‘use strict’;
})();
1、自定义函数(命名函数)
function fn(){};
2、函数表达式(匿名函数)
var fun=function(){};
3、利用new Function(“参数1”,“参数2”,“函数体”)
var f=new Function(‘a’,‘b’,‘console.log(a+b)’);
f(1,2);
二、函数调用以及this的指向
函数的不同调用方式决定了this 的指向不同
1. 普通函数 this 指向window
function fn() {
console.log(‘普通函数的this’ + this);
}
window.fn();
2. 对象的方法 this指向的是对象 o
var o = {
sayHi: function() {
console.log(‘对象方法的this:’ + this);
}
}
o.sayHi();
3. 构造函数 this 指向 ldh 这个实例对象 原型对象里面的this 指向的也是 ldh这个实例对象
function Star() {};
Star.prototype.sing = function() {
}
var ldh = new Star();
4. 绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
var btn = document.querySelector('button');
btn.onclick = function() {
console.log('绑定时间函数的this:' + this);
};
5. 定时器函数 this 指向的也是window
window.setTimeout(function() {
console.log('定时器的this:' + this);
}, 1000);
6. 立即执行函数 this还是指向window
(function() {
console.log('立即执行函数的this' + this);
})();
三、改变函数内this指向 js提供了三种方法 call() apply() bind()
1. call()方法 传递参数
var o = {
name: 'andy'
}
function fn(a, b) {
console.log(this);
console.log(a + b);
};
fn.call(o, 1, 2);
// call 第一个可以调用函数 第二个可以改变函数内的this 指向
// call 的主要作用可以实现继承
function Father(uname, age, sex) {
this.uname = uname;
this.age = age;
this.sex = sex;
}
function Son(uname, age, sex) {
Father.call(this, uname, age, sex);
}
var son = new Son('刘德华', 18, '男');
console.log(son);
求数组最大值
var arr = [1, 66, 3, 99, 4];
// var max = Math.max.apply(null, arr);
var max = Math.max.apply(Math, arr);
var min = Math.min.apply(Math, arr);
apply() 应用 传递的是数组
var o = {
name: ‘andy’
};
function fn(arr) {
console.log(this);
console.log(arr); // 'pink'
};
fn.apply(o, ['pink']);
1. 也是调用函数 第二个可以改变函数内部的this指向
2. 但是他的参数必须是数组(伪数组)
3. apply 的主要应用 比如说我们可以利用 apply 借助于数学内置对象求数组最大值
Math.max();
bind()方法 传递的是参数 不需要立即调用使用
bind()方法不会调用函数。但是能改变函数内部this的指向
fun.bind(thisArg,arg1,arg2,----)
thisArg:在fun函数运行时指定的this指向
arg1,arg2:传递的其他参数
返回由指定的this值和初始化参数改造的原函数拷贝
例子:
// 3. bind() 绑定 捆绑的意思
var o = {
name: 'andy'
};
function fn(a, b) {
console.log(this);
console.log(a + b);
};
var f = fn.bind(o, 1, 2);
f();
// 1. 不会调用原来的函数 可以改变原来函数内部的this 指向
// 2. 返回的是原函数改变this之后产生的新函数
// 3. 如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向此时用bind
// 4. 我们有一个按钮,当我们点击了之后,就禁用这个按钮,3秒钟之后开启这个按钮
点击按钮禁用时间过后恢复案例
// var btn1 = document.querySelector('button');
// btn1.onclick = function() {
// this.disabled = true; // 这个this 指向的是 btn 这个按钮
// // var that = this;
// setTimeout(function() {
// // that.disabled = false; // 定时器函数里面的this 指向的是window
// this.disabled = false; // 此时定时器函数里面的this 指向的是btn
// }.bind(this), 3000); // 这个this 指向的是btn 这个对象
// }
call() apply() bind()总结
相同点:
都可以改变内部this的指向
区别点:
1、call和apply会调用函数,并且改变内部的this指向
2、call和apply传递参数不一样,call传递参数aru1,aru2形式apply必须数组形式[arg]
3、bind不会调用函数,可以改变函数内部this的指向
主要应用场景
1、call经常做继承
2、apply经常跟数组有关系,比如借助于数学对象实现数组最大值最小值
3、bind不调用函数,但是还想改变this的指向,比如改变定时器内部的this指向