Function.prototype.call 和 Function.prototype.apply 的区别
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,...]*/);
讲述:第一个参数(context)将成为 fn 函数的 this 对象,参数 arg1 对应fn函数的参数 msg,参数 arg2 对应fn函数的参数 isalert;
注:apply函数的第二个参数是数组!!!
模型:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>call和apply函数</title>
<style type="text/css">
*{float:left;width:100%;margin-left:20px;}
*{max-height:100%;max-width:100%}
*,:after,:before{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-ms-box-sizing:border-box;-o-box-sizing:border-box}
html{font-size:10px;font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}
body{margin:0 auto;width:80%;background-color:#fff;color:#333;font-size:10pt;font-family:"Helvetica Neue Light","Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;line-height:1.42857143}
div{margin:0;}
span{font-size:16px;font-weight:600;margin-top:10px;}
code{line-height:30px;padding:5px;margin:10px 20px;border:1px solid #fcc;}
.button
{
font-size: 16px;
font-weight: 300;
line-height: 32px; display: inline-block; width:auto;
height: 32px;
padding: 0 20px; -webkit-transition: .3s all;
-moz-transition: .3s all;
-ms-transition: .3s all;
-o-transition: .3s all;
transition: .3s all;
text-align: center;
text-decoration: none; color: #fff;
border: none;
border-radius: 4px; appearance: none;
-webkit-box-orient: vertical;
}
.button:hover,
.button:focus,
.button:active,
{
-webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
-moz-box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
text-shadow: 0 1px 0 rgba(255, 255, 255, .3); -ms-box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
-o-box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
}
.button:hover
{
text-decoration: none; color: #fff;
outline: none;
}
.button:focus
{
color: #fee;
}
.button:visited
{
color: #fff;
}
.button:active
{
text-decoration: none; color: #fff;
}
.button.gold
{
border-color: #feae1b;
background-color: #feae1b;
}
.button.gold:hover,
.button.gold:focus
{
border-color: #fec04e;
background-color: #fec04e;
}
.button.gold:active
{
color: #e59501;
border-color: #f3ab26;
background-color: #f3ab26;
}
</style>
<script type="text/javascript">
var fn = function (msg, isalert) {
if (isalert) alert(this + msg);
};
function call() {
fn.call("我是:", "工具包(cntooltik)", true);
}
function apply() {
fn.apply("我是:", ["工具包(cntooltik)", true]);
}
</script>
</head>
<body>
<span>申明函数:</span>
<div>
<code>
var fn = function (msg, isalert) {
if (isalert) alert(this + msg);
};
</code>
</div>
<span>函数调用:</span>
<div>
<code>
function call() {
fn.call("我是:", "工具包(cntooltik)", true);
}
</code>
</div>
<button class="button gold" onclick="javascript:call()">call函数测试</button>
<div>
<code>
function apply() {
fn.apply("我是:", ["工具包(cntooltik)", true]);
}
</code>
</div>
<button class="button gold" onclick="javascript:apply()">apply函数测试</button>
</body>
</html>
Function.prototype.call 和 Function.prototype.apply 的区别的更多相关文章
- Function.prototype.bind、call与apply方法简介
前言 前段时间面试遇见一题,题目内容大概是 function Parent() { this.prop = 'parent'; } Parent.prototype.get = function() ...
- 探索 Reflect.apply 与 Function.prototype.apply 的区别
探索 Reflect.apply 与 Function.prototype.apply 的区别 众所周知, ES6 新增了一个全局.内建.不可构造的 Reflect 对象,并提供了其下一系列可被拦截的 ...
- Function.prototype.bind、call与apply
学习Function.prototype.bind.call与apply时,看到一篇博客,学到一些内容,但由于博客时间太久,根据官方文档对内容进行一些修正:下文为修正过内容 前言 前段时间面试遇见一题 ...
- error: function declaration isn’t a prototype [-Werror=strict-prototypes]
"warning: function declaration isn't a prototype" was caused by the function like that: ...
- JS 究竟是先有鸡还是有蛋,Object与Function究竟谁出现的更早,Function算不算Function的实例等问题杂谈
壹 ❀ 引 我在JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链一文中介绍了JavaScript原型与原型链,以及衍生的__proto__.constructor等一系列属性.在解答了 ...
- 原型对象prototype和原型属性[[Prototype]]
构造器:可以被 new 运算符调用, Boolean,Number,String,Date,RegExp,Error,Function,Array,Object 都是构造器,他们有各自的实现方式. 比 ...
- prototype/constructor/__proto__之prototype
1任何对象都有__proto__属性 属性值Object2并不是所有对象都有prototype属性.只有方法对象(构造函数)以及基本数据类型还有Array,有prototype属性;并且所有方法(对象 ...
- js函数使用prototype和不适用prototype的区别
js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class 首先来看一个实例: function Li ...
- javascript工厂函数(factory function)vs构造函数(constructor function)
如果你从其他语言转到javascript语言的开发,你会发现有很多让你晕掉的术语,其中工厂函数(factory function)和构造函数(constructor function)就是其中的一个. ...
- $(window).load(function() {})和$(document).ready(function(){})的区别
JavaScript 中的以下代码 : Window.onload = function (){// 代码 } 等价于 Jquery 代码如下: $(window).load(function ( ...
随机推荐
- 【JUC】JDK1.8源码分析之ConcurrentLinkedQueue(五)
一.前言 接着前面的分析,接下来分析ConcurrentLinkedQueue,ConcurerntLinkedQueue一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素 ...
- IO多路复用之epoll总结
1.基本知识 epoll是在2.6内核中提出的,是之前的select和poll的增强版本.相对于select和poll来说,epoll更加灵活,没有描述符限制.epoll使用一个文件描述符管理多个描述 ...
- 3.Struts2配置文件标签介绍
Struts2的很多核心功能都是由拦截器实现的. struts-default.xml中定义了这些拦截器与Result类型. 所以,不继承struts-default包,Struts2提供的很多核心功 ...
- 安装Entity Framework【Setup Entity Framework Environment】(EF基础系列篇4)
Entity Framework 5.0 API是分布在两个地方:NuGet和.NET Framework中,这个.NET framework 4.0/4.5包含EF核心的API,然而通过NuGet包 ...
- C# Socket 模拟http服务器帮助类
0x01 写在前面 0x02 Http协议 0x03 TCP/IP 0x04 看代码 0x05 总结 0x01 写在前面 由于工作中,经常需要在服务器之间,或者进程之间进行通信,分配任务等.用Sock ...
- Python 3.4 Library setup
matplotlib: https://pypi.python.org/pypi/matplotlib/1.4.3 Unofficial Windows Binaries for Python Ex ...
- 【Java每日一题】20161214
package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1214 { public s ...
- python征程1.1(初识python)
在学习python前必须要掌握的一些基本知识 1.编程语言 2.python . C# JAVA 3.python: pypy cpython jpython 4.执行方 ...
- webstorm+react+webpack-demo
序言:通过这个小例子你也许.大概.可能会掌握以下几点 1.webstorm如何使用命令行 2.如何使用webpack的loaders把json格式的文件转化为javascript文件 3.如何使用不同 ...
- C语言中把数字转换为字符串 【转】
在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出 ...