生下来,活下去,如此简单如此难!人,一生都在忙着,累着,奔波着,不论多苦,事,仍是没作完。人,一生都在省着,攒着,储蓄着,不论多抠,钱,仍是没存够。
数组
1、默认模式和严格模式下:bash
function fun(x, y) {
console.log(this,x,y);
}
fun(10,20);// 此时打印的this是window 复制代码
function fun1(x, y){
"use strict"
console.log(this, x, y);
}
fun1(39, 13); // 此时打印的this指向是undefined复制代码
var Obj = {
name: "张三"
}
function fun2(x, y) {
console.log(this, x, y);
}
fun2(20,30);// 若是这样直接调用的话,this指向就是window
fun2.call(Obj, 30, 50); //若是使用call改变其this指向的话, 这时候this就是指向Obj了复制代码
var Obj = {
name: "张三"
}
function fun3(x, y) {
console.log(this, x, y);
}
fun3(20,30);// 若是这样直接调用的话,this指向就是window
fun3.apply(Obj, [100,200]); //若是使用apply改变其this指向的话, 这时候this就是指向Obj了,后面的参数时用数组封装起来的复制代码
var Obj = {
name: "张三"
}
function fun4(x, y) {
console.log(this, x, y);
}
// 使用bind会复制一个函数,而后改变其this指向,并返回一个新的函数
var ss = fun4.bind(Obj);
// 这个时候再调用传入参数
ss(10, 20);复制代码