Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用某个函数或方法.call()方法类似于apply(),不同之处仅仅是call()接受的参数是参数列表. 简而言之: apply()一个this,多个参数 call()   一个this,一个参数 语法 fun.apply(thisArg[, argsArray])|fun.call(thisArg[…
1. // Function.prototype.apply()的作用 // 1.Using apply to chain constructors Function.prototype.construct = function (aArgs) { var oNew = Object.create(this.prototype); this.apply(oNew, aArgs); return oNew; }; // 或者The Object.create() method used above…
宿主对象,在javascript中有三类对象,本地对象,内置对象和宿主对象.其他两类暂且不提,宿主对象是指什么呢(DOM BOM),控制台对象是文档对象模型的扩展,也被认为是宿主对象.那么,它们有什么缺陷呢?在IE9之前,宿主对象不是继承自Object,它们的方法也不继承自Function,IE9之后就大有改进了. 看下IE8与IE9的document.getElementById ie8: ie9: 我们可以看到,ie9的document.getElementById是有Function.pr…
探索 Reflect.apply 与 Function.prototype.apply 的区别 众所周知, ES6 新增了一个全局.内建.不可构造的 Reflect 对象,并提供了其下一系列可被拦截的操作方法.其中一个便是 Reflect.apply() 了.下面探究下它与传统 ES5 的 Function.prototype.apply() 之间有什么异同. 函数签名 MDN 上两者的函数签名分别如下: Reflect.apply(target, thisArgument, arguments…
我们先从一道简单的题目开始,前几天在git上看到的: 定义log方法,它可以代理console.log的方法.log(1,2,3)  =>  1 2 3 通常,你的答案会是这样的: function log(){ var args = Array.prototype.slice.call(arguments); console.log.apply(console, args); }; 直到有一天,你看到一个非常高大上的答案: function log(){ Function.prototype.…
首先需要了解apply,call的基本用法,其目的是改变调用方法中的this指向,将其指向为传入的对象,改变this的指向,两种方法接收参数的方式不同. 代码:console.log var console = window.console || {log: function () {}}; var log = console.log; console.log = function(tips,message){ Function.prototype.apply.call(log, console…
1. // Function.prototype.bind() 的作用 // 1.Creating a bound function this.x = 9; var module = { x: 81, getX: function() { return this.x; } }; console.log(module.getX()); var retrieveX = module.getX; console.log(retrieveX()); // 9, because in this case,…
1. // call的3种作用 // 1.Using call to chain constructors for an object function Product(name, price) { this.name = name; this.price = price; if (price < 0) { throw RangeError('Cannot create product ' + this.name + ' with a negative price'); } } function…
call和apply函数是function函数的基本属性,都可以用于更改函数对象和传递参数,是前端工程师常用的函数.具体使用方法请参考以下案列: 例如: 申明函数: var fn = function (msg, isalert) { if (isalert) alert(this + msg); }; 用法: call: fn.call(/*context,arg1,arg2,...*/); apply:fn.call(/*context,[arg1,arg2,...]*/); 讲述:第一个参…
文章地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply 利用Math.max.apply()方法获取数组最大值时,会有潜在的风险超过js解释引擎的长度限制,最大限制是:65536,如图: 推荐用如下方法:…