首先需要了解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, arguments);
//Function.prototype.call.call(log, console, arguments);
//Function.prototype.call.apply(log, [console, arguments]); //传统方式
//var args=[].slice.call(arguments);
//log.apply(console,args);
}

执行结果:

console.log("测试","This is test");  
测试 This is test  

分析:

该怎么理解Function.prototype.apply.call(log,console,arguments);呢

首先可以将Function.prototype.apply看成一个整体-->FunctionApply

FunctionApply.call(log,console,arguments);

那么将此句翻译一下

log.FunctionApply(console,arguments);

然后再翻译一下,你就懂了吧,就是一个普通方法调用了

console.log(arguments);

发散思维:

Function.prototype.call.apply(log,[console.arguments]);

FunctionCall.apply(log,[console,arguments]);
log.FunctionCall(console,arguments);
console.log(arguments);

小tips:

 Function.prototype.apply.call  等同于Function.prototype.call.call
Function.prototype.call.apply 等同于 Function.prototype.apply.apply

免费外送个栗子:

function testA(a){
console.log('aaaa',a);
}
Function.prototype.apply.call(testA,window,['Mike']);
//Function.prototype.call.call(testA,window,['Mike']);
//testA.apply(window,['Mike']);
//window.testA('Mike');
//Function.prototype.apply.apply(testA,[window,['Mike']]);
//Function.prototype.call.apply(testA,[window,['Mike']]);

以上执行结果都一样

为:aaaa Mike

总结使用用法:

XXX可以是call或者是apply,child一定是parent可指向的对象

Function.prototype.XXX.call(child,parent,arguments||array);

Function.prototype.XXX.apply(child,[parent,arguments||array]);

--------------------------------------------------------------------------------------------------------------------------------------------------------------

终极方法

Function.prototype.apply.call(console.log,console,arguments);

这么一对比,第三种方案妥妥的胜出啊,不用考虑兼容,代码简短,虽然不是很好理解~~

说了这么多废话,Function.prototype.apply.call什么时候用,就是在这种应用场景。

如果还有其他的话,那就是那些奇葩面试题,比如:

var f1=function(){console.log(1)};
var f2=function(){console.log(2)};
Function.prototype.call.call(Function.prototype.call,f2)//
Function.prototype.call.call(f1,f2);//
昨天在网上看到一个很有意思的js面试题,就跟同事讨论了下,发现刚开始很绕最后豁然开朗,明白过来之后发现还是挺简单的,跟大家分享下!
题目如下:var a = Function.prototype.call.apply( function(a){return a;},  [0,4,3] );        alert(a);
分析步骤如下:
1、将Function.prototype.call当成整体,call方法是由浏览器实现的本地方法,是函数类型的内部方法
var a = (Function.prototype.call).apply(function(a){return a;}, [0,4,3]);
 
2、fun.apply(obj,args)等价于obj.fun(args),这一步是重点,必须理解!
(function(a){return a;}).apply(0,[4,3])
(function(a){return a;}).call(0,4,3)
 
3、 到这步结果就很明显了,a就是4,alert的结果就是4
 
这个题目迷惑的点就在于Function.prototype.call,理解好了,就清晰明了了!

Function.prototype.apply.call 理解分析的更多相关文章

  1. 关于Function.prototype.apply.call的一些补充

    宿主对象,在javascript中有三类对象,本地对象,内置对象和宿主对象.其他两类暂且不提,宿主对象是指什么呢(DOM BOM),控制台对象是文档对象模型的扩展,也被认为是宿主对象.那么,它们有什么 ...

  2. javascript中 Function.prototype.apply()与Function.prototype.call() 对比详解

    Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用 ...

  3. Function.prototype.apply.call

    我们先从一道简单的题目开始,前几天在git上看到的: 定义log方法,它可以代理console.log的方法.log(1,2,3)  =>  1 2 3 通常,你的答案会是这样的: functi ...

  4. 探索 Reflect.apply 与 Function.prototype.apply 的区别

    探索 Reflect.apply 与 Function.prototype.apply 的区别 众所周知, ES6 新增了一个全局.内建.不可构造的 Reflect 对象,并提供了其下一系列可被拦截的 ...

  5. Function.prototype.call 和 Function.prototype.apply 的区别

    call和apply函数是function函数的基本属性,都可以用于更改函数对象和传递参数,是前端工程师常用的函数.具体使用方法请参考以下案列: 例如: 申明函数: var fn = function ...

  6. Function.prototype.apply()

    文章地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ...

  7. Function.prototype.call.apply结合用法

     昨天在网上看到一个很有意思的js面试题,就跟同事讨论了下,发现刚开始很绕最后豁然开朗,明白过来之后发现还是挺简单的,跟大家分享下!  题目如下: var a = Function.prototype ...

  8. Function.prototype.call.apply作用详解

    关于call()和apply()基本用法可以参阅如下两篇文章: (1).call方法参阅JavaScript call()一章节. (2).apply方法参阅JavaScript apply()一章节 ...

  9. 箭头函数表达式和声名式函数表达式的区别以及 Function.prototype的bind, apply,call方法

    箭头函数不能用做构造函数 箭头函数没有arguments参数 箭头函数没有自己的this,是从作用域链上取this,是与箭头函数定义的位置有关的,与执行时谁调用无关,所以用call,apply,bin ...

随机推荐

  1. mysql-创建用户报错ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'

    创建用户: create user ‘test’@’%’ identified by ‘test’; 显示ERROR 1396 (HY000): Operation CREATE USER faile ...

  2. svn 清除用户名和密码

  3. pycharm 生成requirements.txt

    在命令行中输入 pip freeze>requirements.txt 1 安装requirements文件中的包 pip install -r requirements.txt

  4. 安装centos docker ce

    安装centos docker ce 移除旧的版本: $ sudo yum remove docker \ docker-client \ docker-client-latest \ docker- ...

  5. 更换yum源为阿里云源

    1.复制备份: cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载: wget -O ...

  6. LeetCode_476. Number Complement

    476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...

  7. (转) 解决django项目部署到nginx+uwsgi服务器后 admin页面样式消失的问题

    原贴地址:https://blog.csdn.net/qq_42571805/article/details/80862455 摘要 uwsgi为主要服务器,nginx为反向代理服务器部署完成之后发现 ...

  8. 领域模型/DDD领域驱动设计

    http://www.fanyilun.me/2018/04/08/%E8%B0%88%E8%B0%88%E9%A2%86%E5%9F%9F%E5%BB%BA%E6%A8%A1/ http://www ...

  9. ajax处理csrf的三种方式

    方式一: $.post({ url: '/get_result/', data: { value0: $('#v1').val(), value1: $('#v2').val(), csrfmiddl ...

  10. PHP不过过滤防止xss攻击的方法

    PHP不过过滤防止xss攻击的方法<pre> $content=htmlspecialchars($content); $pos=strpos($content,"\u" ...