const name = 'qianyin';
const product = {
name: 'linheng',
};
function log(...args){
console.log(this.name, ...args);
}
log(1, 2, 3); // qianyin 1 2 3
log.call(null, 1, 2, 3); // qianyin 1 2 3
log.call(product, 1, 2, 3); // linheng 1 2 3
复制代码
const name = 'qianyin';
const product = {
name: 'linheng',
};
const log = (...args) => {
console.log(this.name, ...args);
}
log(1, 2, 3); // qianyin 1 2 3
log.call(null, 1, 2, 3); // qianyin 1 2 3
log.call(product, 1, 2, 3); // qianyin 1 2 3
复制代码
箭头函数做为函数的一种形式, 对于this的处理和普通函数有所区别, 其没有本身的 this 上下文,也就是说经过 bind/call/apply 函数方法设置 this 值时无效的,会被忽略;数组
由于箭头函数没有本身的 this 上下文, 因此箭头函数的 this 上下文等于定义函数处的this上下文,也就是最近的一个 this 上下文;app
你能够认为箭头函数的 this 和调用者无关,只和其定义时所在的 this 上下文相关;函数
以下代码: 在对象 obj 中使用箭头函数定义 log 函数, 那么由于箭头函数没有本身的 this 上下文, 因此 log 函数的 this 上下文等于定义箭头函数处的 this 上下文, 等于 对象 obj 所处的 this 上下文(window)ui
const name = 'linheng';
const obj = {
name: 'qianyin',
log: () => {
console.log(this.name);
}
};
obj.log(); // linheng
复制代码
const name = 'linheng';
const obj = {
name: 'qianyin',
log: function(){
console.log(this.name);
}
};
obj.log(); // qianyin
复制代码
const name = 'linheng';
const obj = {
name: 'qianyin',
log: function(){
(() => {
console.log(this.name);
})();
},
};
obj.log(); // qianyin
复制代码
const name = 'qianyin';
const product = {
name: 'linheng',
};
function log(...args){
console.log(this.name, ...args);
}
log([1, 2, 3]); // qianyin [1 2 3]
log.apply(null, [1, 2, 3]); // qianyin 1 2 3
log.apply(product, [1, 2, 3]); // linheng 1 2 3
复制代码
const name = 'qianyin';
const product = {
name: 'linheng',
};
const log = (...args) => {
console.log(this.name, ...args);
}
log([1, 2, 3]); // qianyin [1 2 3]
log.apply(null, [1, 2, 3]); // qianyin 1 2 3
log.apply(product, [1, 2, 3]); // qianyin 1 2 3
复制代码
// 例一
this.x = 9;
var module = {
x: 81,
getX: function() { return this.x; }
};
module.getX(); // 返回 81 (经过对象调用函数, 上下文为该对象)
var retrieveX = module.getX; // 获取对象中函数的引用地址
retrieveX(); // 返回 9, 在这种状况下, "this" 指向全局做用域(在全局对象下调用函数)
// 永久为函数 boundGetX 绑定 this 上下文
var boundGetX = retrieveX.bind(module);
boundGetX(); // 返回 81 (函数 this 上下文永久绑定为 module)
复制代码
// 例二为回调函数绑定 this 上下文
var x = 10;
var obj = {
x: 20,
get: ffunction(){
console.log(this.x);
}
};
// 将对象中方法取出(函数的引用地址),做为回调函数, 又由于 setTimeout 回调函数执行的上下文是 window
setTimeout(obj.get, 1000); // 打印 10
// 将对象中方法取出(函数的引用地址),做为回调函数并绑定 this 上下文
setTimeout(obj.get.bind(obj), 1000); // 打印 20
复制代码
function list() {
return Array.prototype.slice.call(arguments);
}
var list1 = list(1, 2, 3); // [1, 2, 3]
// 为拷贝 list 方法并绑定初始参数
var leadingThirtysevenList = list.bind(undefined, 37);
var list2 = leadingThirtysevenList(); // [37]
var list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]
复制代码
const obj = {0: 'q', 1: 'i', 2: 'q', 3: 'a', 4:'n', 5: 'y', 6:'i', 7:'n', length: 8};
const arr = [];
Array.prototype.push.apply(arr, obj);
console.log(arr); // ["q", "i", "q", "a", "n", "y", "i", "n"]
复制代码
const obj = {0: 'q', 1: 'i', length: 2};
const arr = Array.prototype.slice.call(obj); // [q, i]
复制代码
const obj = {0: 'q', length: 1};
Array.prototype.push.call(obj, 'i', 'a', 'n');
console.log(obj); // {0: 'q', 1: 'i', 2: 'a', 3: 'n'}
复制代码
const obj = {0: 'q', length: 1};
const push = Array.prototype.push.bind(obj);
push('i', 'a', 'n');
console.log(obj); // {0: 'q', 1: 'i', 2: 'a', 3: 'n'}
复制代码
const arr = [1,2,3,4,5,6];
const max = Math.max.apply(null, arr);
// 或 const max = Math.max.call(null, ...arr)
console.log(max); // 6
复制代码
const arr = [1, 2];
const brr = [3, 4];
Array.prototype.push.apply(arr, brr);
// 或者 Array.prototype.push.call(arr, ...brr);
// 固然还能够这样 arr.push(...brr);
console.log(arr);
复制代码
function log(...args){
// 在能够经过配置, 或者判断当前开发环境来控制是否须要在控制台打印输出
if(true){
console.log.apply(console, args);
}
}
复制代码