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);
}
}
複製代碼