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 ( ...
随机推荐
- 多个Activity相互调用和Intent
MainActivity.java和OtherActivity.java的相互调用 首先MainActivity.java是Android程序自带的,新建一个类OtherActiviy extends ...
- ajax+php+js实现异步刷新表单验证
创建ajax对象 //创建对象 function createAjax(){ var request =false; //IE浏览器,window对象存在ActiveXObject属性 if(wind ...
- X240 Win10企业版 14279版本 电池标尺白底问题
win10系统更新到14279版本: 电池标尺显示白底,而且右键也不可打开"启动电池管理器-" (1)首先安装lenovo settings 下载地址:http://think.l ...
- Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
Segregate operations that read data from operations that update data by using separate interfaces. T ...
- Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications 云设计模式:云应用的规范架构指导
1.Cache-aside Pattern 缓存模式 Load data on demand into a cache from a data store. This pattern can impr ...
- eclipse maven update error 解决方法
eclipse maven update error 解决方法 本来真不想写这篇博文的,但是eclipse和maven真的是太操蛋了,动不动就出了一些乱七八糟的问题,记录一下.希望公司能早 ...
- Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】
原文链接: http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part-2-singleton/ De ...
- Winform混合式开发框架访问Web API接口的处理
在我的混合式开发框架里面,集成了WebAPI的访问,这种访问方式不仅可以实现简便的数据交换,而且可以在多种平台上进行接入,如Winform程序.Web网站.移动端APP等多种接入方式,Web API的 ...
- jQuery:年月日三级联动
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...
- 事件流之事件冒泡与事件捕获<JavaScript高级程序设计>学习笔记
1.事件流 浏览器开发团队遇到一个很有意思问题:页面的那一部分会拥有特定的事件? 对于理解这个问题您可以想象画在一张纸上的一组同心圆,如果你把手指放在圆心上,那么你的手指指向的其实不是一个圆,而是纸上 ...