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 ( ...
随机推荐
- 1Z0-053 争议题目解析706
1Z0-053 争议题目解析706 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 706.You execute the following command to set the ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【Java】记录一次代码优化
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- JS实现-页面数据无限加载
在手机端浏览网页时,经常使用一个功能,当我们浏览京东或者淘宝时,页面滑动到底部,我们看到数据自动加载到列表.之前并不知道这些功能是怎么实现的,于是自己在PC浏览器上模拟实现这样的功能.先看看浏览效果: ...
- 记录visual Studio使用过程中的两个问题
Visual Studio是Windows平台下进行项目管理和开发的终极利器.除了微软自家的技术外,新版的VS不但支持Javascript, Python的开发调试,甚至还支持了Android, iO ...
- 修复DapperExtension做Insert对象主键为Guid时不能赋值的问题
最新的dapperExtension有个bug,就是当做Insert操作的时候,实体的主键类型为GUID的时候,会自动生产一个新的GUID替换原来的GUID,使得使用者在Insert的时候不能在外部指 ...
- 实际案例:在现有代码中通过async/await实现并行
一项新技术或者一个新特性,只有你用它解决实际问题后,才能真正体会到它的魅力,真正理解它.也期待大家能够多分享解一些解决实际问题的内容. 在我们遭遇“黑色30秒”问题的过程中,切身体会到了异步的巨大作用 ...
- globalization与全球化
本配置节是关于配置应用程序的全球化设置. <globalization <!--以下两个特性默认编码为 UTF-8,该编码在 Machine.config 文件(在安装 .NET Fram ...
- 模仿36。杀毒~button
<Style x:Key="360btn" TargetType="{x:Type Button}"> <Setter Property=&q ...
- Chrome立体动画代码
效果预览:http://hovertree.com/code/run/css/x8l6si70.html 请实用Chrome浏览器查看效果,手机上也可以. 代码如下: <!DOCTYPE htm ...