轉載請註明出處
bind()
bind()
方法建立一個新的函數,在bind()
被調用時,這個新函數的this
被bind的第一個參數指定,其他的參數將做爲新函數的參數供調用時使用。數組
function.bind(thisArg[,arg1[,arg2[, ...]]])
var module = { x: 42, getX: function() { return this.x; } } var unboundGetX = module.getX; console.log(unboundGetX()); // The function gets invoked at the global scope // expected output: undefined var boundGetX = unboundGetX.bind(module); console.log(boundGetX()); // expected output: 42
bind()
的一個實現:Function.prototype.bind = function(context) { if (typeof this !== 'function') { throw new TypeError('Error') } let _this = this let args = [...arguments].slice(1) return function F() { // 判斷是否被當作構造函數使用 if (this instanceof F) { return _this.apply(this, args.concat([...arguments])) } return _this.apply(context, args.concat([...arguments])) } }
apply()
apply()
方法調用一個具備給定this
值的函數,以及做爲一個數組(或相似數組對象)提供的參數。app
func.apply(thisArg, [argsArray])
var numbers = [5, 6, 2, 3, 7]; var max = Math.max.apply(null, numbers); console.log(max); // expected output: 7 var min = Math.min.apply(null, numbers); console.log(min); // expected output: 2
apply()
的一個實現:Function.prototype.apply = function(context) { // context就是apply的第一個參數,在這裏也就是obj // 判斷是不是undefined和null if (typeof context === 'undefined' || context === null) { context = window } // this也就是調用apply的函數 把函數賦值給context下面的一個鍵,這裏是fn context.fn = this // arguments就是傳進來的參數 let args = arguments[1] let result if (args) { result = context.fn(...args) } else { result = context.fn() } delete context.fn return result } let test = function (sex) { console.log(this.name + sex) } let obj = { name: '我是' } test.apply(obj, ['test'])
call()
call()
方法使用一個指定的 this
值和單獨給出的一個或多個參數來調用一個函數。函數
fun.call(thisArg, arg1, arg2, ...)
function Product(name, price) { this.name = name; this.price = price; } function Food(name, price) { Product.call(this, name, price); this.category = 'food'; } console.log(new Food('cheese', 5).name); // expected output: "cheese"
call()
的一個實現:Function.prototype.call = function(context) { // context就是call的第一個參數,在這裏也就是obj // 判斷是不是undefined和null if (typeof context === 'undefined' || context === null) { context = window } // this也就是調用call的函數 把函數賦值給context下面的一個鍵,這裏是fn context.fn = this // arguments就是傳進來的參數 let args = [...arguments].slice(1) let result = context.fn(...args) delete context.fn return result } let test = function (sex) { console.log(this.name + sex) } let obj = { name: '我是' } test.call(obj, 'test')
歡迎瀏覽個人 我的網站