Function.prototype.call.apply()方法
在看uncurrying化函数时候,碰到了Function.prototype.call.apply()的用法:
先说说uncurrying()函数:
Function.prototype.uncurrying = function(){
var self = this;
return function(){
var obj = Array.prototype.shift.call(arguments);
return self.apply(obj,arguments);
}
}
然后可以通过var call = Function.prototype.call.uncurrying()进行函数参数的调用:
var call = Function.prototype.call.uncurrying();
var fn = function(name){
console.log(name);
}
call(fn,window,'sven');
最后输出的是://sven
怎么解释这段代码呢?
Function.prototype.call.apply(fn,[window,'sven']);
Function.prototype.call.call(fn,window,'sven');
fn.functionCall(window,'sven');
window.fn('sven'); //'sven
可以先将apply的调用转化为call的调用方式,而后改变this指向,fn拥有call的方法,再window拥有fn的方法,参数'sven'输入进去,最后输出sven。
再看下Function.prototype.apply.apply()方法的调用:
var apply = Function.prototype.apply.uncurrying();
var fn = function(name){
console.log(this.name); //sven
console.log(arguments); //[1,2,3]
}
apply(fn,{name:'sven'},[1,2,3]); Function.prototype.apply.apply(fn,[{name:'sven'},[1,2,3]]);
Function.prototype.apply.call(fn,{name:'sven'},[1,2,3]);
fn.FunctionApply({name:'sven'},[1,2,3]);
最后转化时候,this对象指向{name:'sven'},arguments此时是[1,2,3]。
【完】
一切皆为虚幻
Function.prototype.call.apply()方法的更多相关文章
- Function.prototype.call.apply结合用法
昨天在网上看到一个很有意思的js面试题,就跟同事讨论了下,发现刚开始很绕最后豁然开朗,明白过来之后发现还是挺简单的,跟大家分享下! 题目如下: var a = Function.prototype ...
- Function.prototype.call.apply作用详解
关于call()和apply()基本用法可以参阅如下两篇文章: (1).call方法参阅JavaScript call()一章节. (2).apply方法参阅JavaScript apply()一章节 ...
- javascript中 Function.prototype.apply()与Function.prototype.call() 对比详解
Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用 ...
- 关于Function.prototype.apply.call的一些补充
宿主对象,在javascript中有三类对象,本地对象,内置对象和宿主对象.其他两类暂且不提,宿主对象是指什么呢(DOM BOM),控制台对象是文档对象模型的扩展,也被认为是宿主对象.那么,它们有什么 ...
- Function.prototype.apply.call 理解分析
首先需要了解apply,call的基本用法,其目的是改变调用方法中的this指向,将其指向为传入的对象,改变this的指向,两种方法接收参数的方式不同. 代码:console.log var cons ...
- js中的call和apply方法的区别
一.call和apply的说明 1.call,apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以 ...
- JavaScript之call()和apply()方法详解
简介:apply()和call()都是属于Function.prototype的一个方法属性,它是JavaScript引擎内在实现的方法,因为属于Function.prototype,所以每个Func ...
- JS中的call()和apply()方法区别
如 果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可 理喻.如果在学JavaScript这自由而变幻无穷 ...
- Function.prototype.toString
语法:fn.toString(indentation) 改方法返回当前函数源代码的字符串,而且还可对此字符串进行操作,比如: function num(){ }; var str = num.toSt ...
随机推荐
- Excel-DNA自定义函数的参数智能提示功能:ExcelDna.IntelliSense1.1.0.rar
解压缩后,可以看到如下3个文件. ExcelDna.IntelliSense.xll 以及 ExcelDna.IntelliSense64.xll 是两个函数参数智能提示加载项,分别用于32和64位E ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- 点分治练习——BZOJ 2152
做的第二道点分治的题目,比较裸,算是模板题吧(感觉比之前那题还简单点. 题目:BZOJ 2152 聪聪可可 题目大意:给出一棵树,求树上两点间长度为3的倍数(0也算)的路径数. 解题思路: 基本和PO ...
- 网页title滚动
); var leftstar=title.substring (1,title.length ); document.title =leftstar +firstch ; ...
- SQL语言:DDL,DML,DCL,DQL,TCL
DDL(Data Definition Language)数据库定义语言 statements are used to define the database structure or schema. ...
- node 环境下简单web服务器搭建代码
零.前置 已经安装 node 环境. 一.代码片段 var http = require('http'); var path = require('path'); var fs = require(' ...
- python 3新式类的多继承
因为我用的是python3,所以所用到的类都是新式类,这里我说的都是新式类,python2类的继承复杂一些,主要有新式类和老式类.python3类(新式类)的继承是是广度优先(BFS),实例如下: c ...
- SpringBoot自动配置的原理
Spring Boot的运行是由注解@EnableAutoConfiguration提供的它的关键功能是@Import注解. EnableAutoConfigurationImportS ...
- SpringSecurity 如何提示错误
1.可以通过authentication-failure-url="/login.html?error=1" 前端接收参数,根据参数提示 错误 2.前端vue this.myNam ...
- idea 内存溢出解决方法
在Run/Debug configuration 的 vm options里面输入 -server -XX:PermSize=128M -XX:MaxPermSize=256m 具体如下图: