函数执行 看方法前面是否有“点” 没有“点” this是window【严格模式下是undefined】 有“点” “点”前面是谁this就是谁
const fn = function(){
console.log(this)
}
let obj = {
name:'gh',
fn:fn
}
fn()
obj.fn()
给当前元素的某个事件行为绑定方法 当事件行为触发 方法中的this是当前元素本身【排除attachEvent】
document.body.addEventListener("click", function () {
console.log(this);
});
function Factory() {
this.name = "gh";
this.age = 24;
console.log(this);
}
let f = new Factory();
箭头函数中没有执行主体 所用到的this都是其所处上下文中的this
let demo = {
name: "DEMO",
fn() {
console.log(this);
setTimeout(function () {
console.log(this);
}, 1000);
setTimeout(() => {
console.log(this);
}, 1000);
},
};
demo.fn();
可以基于Function.prototype上的call/apply/bind去改变this指向
func函数基于__proto__找到Function.prototype.call 把call方法执行
在call方法内部 【call执行的时候】call(context->obj,…params->10,20)
func函数基于__proto__找到Function.prototype.bind 把bind方法执行
在bind方法内部
function func(x, y) {
console.log(this, x, y);
}
let obj = {
name: "OBJ",
};
func.call(obj, 10, 20);
func.apply(obj, [10, 20]);
document.body.addEventListener("click", func.bind(obj, 10, 20));
//原理:利用“点”定this机制 context.xxx = self "obj.xxx = func" => obj.xxx()
Function.prototype.call = function call(context, ...params) {
// this/self->func context->obj params->[10,20]
let self = this,
key = Symbol("KEY"),
result;
context == null ? (context = window) : null;
/^(object|function)$/i.test(typeof context) ? (context = Object(context)) : null;
context[key] = self;
result = context[key](...params);
delete context[key];
return result;
};
实现bind
Function.prototype.bind = function bind(context, ...params) {
//this/self -> func context -> obj params -> [10,20]
let self = this;
return function proxy(...args) {
//把func执行并且改变this args->是执行proxy的时候可能传的值
self.apply(context, params.concat(args));
};
};
克隆arr返回一个新数组 可以使用arr.slice()前拷贝
实现一个slice
Array.prototype.slice = function () {
result = [];
for (let i = 0; i < this.length; i++) {
result.push(this[i]);
}
return result;
};
将arguments变成数组
Array.from(arguments)、[…arguments]、Array.prototype.slice.call(arguments)…
function func() {
[].forEach.call(arguments, (item) => {
console.log(item);
});
}
func(1, 2, 3);