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>

关于call和apply函数的区别及用法的更多相关文章

  1. PHP易混淆函数的区别及用法汇总

    本文实例分析了PHP易混淆函数的区别及用法.分享给大家供大家参考.具体分析如下: 1.echo和print的区别PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.ech ...

  2. pandas的map函数与apply函数的区别

    import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns=list("ABC ...

  3. call、apply/bind的区别和用法(简单粗暴的解释)

    var obj1={ name:"bob", age:20 } var obj2={ name:"coco", age:22 } function getAge ...

  4. js中 call() 和 apply() 方法的区别和用法详解

    1.定义 每个函数都包含俩个非继承而来的方法:call() 和 apply()   call 和 apply 可以用来重新定义函数的的执行环境,也就是 this 的指向:call 和 apply 都是 ...

  5. php 中session_set_cookie_params 和 setcookie 函数的区别与用法

    session_set_cookie_params() 函数不管刷不刷新页面,都不会改变cookie的过期时间, 但setcookie() 函数页面每刷新一次,cookie 的过期时间就会刷新一次. ...

  6. pandas DataFrame apply()函数(2)

    上一篇pandas DataFrame apply()函数(1)说了如何通过apply函数对DataFrame进行转换,得到一个新的DataFrame. 这篇介绍DataFrame apply()函数 ...

  7. JavaScript中的bind,call和apply函数的用法和区别

    一直没怎么使用过JavaScript中的bind,call和apply, 今天看到一篇比较好的文章,觉得讲的比较透彻,所以记录和总结如下 首先要理解的第一个概念,JavaScript中函数调用的方式, ...

  8. js中bind、call、apply函数的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

  9. Javascript中bind、call、apply函数用法

    js 里函数调用有 4 种模式:方法调用.正常函数调用.构造器函数调用.apply/call 调用. 同时,无论哪种函数调用除了你声明时定义的形参外,还会自动添加 2 个形参,分别是 this 和ar ...

随机推荐

  1. R语言进阶

    一.初学入门:<R in Action><The Art of_R Programming>入门者可首选两本,前者从统计角度入手,分高中低三部分由浅入深的讲解了如何用R来实现统 ...

  2. 额。。万恶之源就是c

    http://blog.csdn.net/feeltouch/article/details/45155529

  3. .NetChajian

    Code generation(代码自动生成) NVelocity CodeSmith X-Code .NET XGoF - NMatrix / DEVerest Compilation(编译工具) ...

  4. 为什么使用Junit Test而不用普通java main方法来完成测试?

    因为在程序里边,一个接口对应一个实现方法,而在接口中常常会定义相关的很多方法,所以在测试的时候,如果都在main方法里边进行测试,main方法就会显得臃肿,而且不便于以后其他人测试以及查看测试数据,用 ...

  5. cornerstone知识点

    CornerStone使用教程(配置SVN,HTTP及svn简单使用) 发布时间:2015-01-02 19:54   作者:芳仔小脚印    来源:开源中国 CornerStone是Mac OS X ...

  6. heightcharts

    title:标题 subtitle:子标题 data:eval(dataList)//数据格式转换

  7. vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法

    ---恢复内容开始--- 最近在安装了vsftpd后 添加了虚拟账户后 新建用户 为新用户创立独立的工作目录 因为虚拟用户在工作目录需要上传文件 所以必须拥有此目录的W权限,但每当给此目录加上W权限后 ...

  8. 使用 jsoup 对 HTML 文档进行解析和操作

    jsoup 简介 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HT ...

  9. ExtJS客户端代理

    代理(proxy)分为两大类:客户端代理和服务器端代理.客户端代理主要完成与浏览器本地存取数据相关的工作,服务器端代理则是通过发送请求,从服务器端获取数据.根据各自获取数据的方式,客户端代理和服务器端 ...

  10. 会议通js

    js逻辑: /** * Created by wanglijuan on 2016/12/2. */ $(function () { //登陆后请求数据 // $.ajax({ // url:&quo ...