其实项目中还没有用到。

但自己还是想逐步了解一些高级的JS语法,不是为了炫技,也不像找前端的工作。

主要目的是:1.学习设计思想,提升解决问题的能力2.让自己的脑子动起来,别太笨。

简单的几句话总结一下call,apply和bind:

三者都是为了改变函数执行时的上下文环境。
We really do want to be able to ask deep_thought a question when we click the button, and more generally, we do want to be able to call object methods in their native context when responding to things like events and setTimeout calls. Two little-known JavaScript methods, apply and call, indirectly enable this functionality by allowing us to manually override the default value of this when we execute a function call. (手动重写默认的this值)
call方法的第一个参数定义了this关键字在被调用方法的执行上下文中指向和对象,call方法的剩余参数则是被调用方法的参数。

apply方法和 call方法基本一致,但是允许你以数组的形式向被调用的函数传递参数.
all是立即执行函数的,因此我们提供的 onclick handler是函数的执行结果而不是函数本身.我们需要JavaScript的另一个特性来解决这个问题:bind方法。
bind方法:对于给定函数,创建具有与原始函数相同的主体的绑定函数。 在绑定函数中,this 对象将解析为传入的对象。 绑定函数具有指定的初始参数。

两个参考网址:
JavaScript的this,call(),apply(),bind()http://blog.csdn.net/golden_chan/article/details/4030111
微软官方bind指南(MSDN大法好!!!)https://msdn.microsoft.com/zh-cn/library/ff841995
bind   function.bind(thisArg[,arg1[,arg2[,argN]]])
如果理解了bind,那么call和apply也就会轻松一些了,下面就直接上代码了,代码直接复制的MSDN,自己又修改了几句做了一点点测试。再说一遍:MSDN大法好!!!
第一个:bind填充this对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script language="JavaScript">
var checkNumeericRange = function(value){
if(typeof value !=='number')
return false;
else
return value>=this.minimum && value<=this.maxmum;
}
var checkNumeericRange1 = function(value,r){
if(typeof value !=='number')
return false;
else
return value>=r.minimum && value<=r.maxmum;
}
var range = {minimum:10,maxmum:20};
var boundCheckNumericRange = checkNumeericRange.bind(range);
var result = boundCheckNumericRange(12);
var result1=checkNumeericRange(12);
var result2=boundCheckNumericRange(21);
var result3=boundCheckNumericRange('adssad');
var result4=checkNumeericRange1(12,range);
document.writeln(result);//true
document.writeln(result1);//false
document.writeln(result2);//false
document.writeln(result3);//false
document.writeln(result4);//true
</script>
</body>
</html>

第二个:bind改变原有的this对象

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script language="JavaScript">
var originalObject={
minimum:50,
maxmum:100,
checkNumericRange:function(value){
if(typeof value !== 'number')
return false;
else
return value>=this.minimum && value<=this.maxmum;
}
} var result=originalObject.checkNumericRange(10);
document.writeln(result); var range={minimum:10,maxmum:20}; //MSDN的原版写法
// Create a new version of the checkNumericRange function that uses range.
var boundObjectWithRange = originalObject.checkNumericRange.bind(range);
// Check whether 10 is in the numeric range.
var result = boundObjectWithRange(10); // 这样写也可以:
// var boundObjectWithRange=originalObject.checkNumericRange.bind(range,39);
// var result=boundObjectWithRange()
document.write(result);
</script>
</body>
</html>

第三个:利用[,arg1[,arg2[,argN]]]传入参数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script language="JavaScript">
var displayArgs = function(val1,val2,val3,val4){
document.write(val1 + " " + val2 + " " + val3 + " " + val4);
}
var emptyObject = {};
var displayArgs2 = displayArgs.bind(emptyObject,12,"a");
displayArgs2("b","c");
</script>
</body>
</html>

最新文章

  1. iOS开发之Socket通信实战--Request请求数据包编码模块
  2. Iterator接口。集合输出
  3. Linux进程间通信(二):信号集函数 sigemptyset()、sigprocmask()、sigpending()、sigsuspend()
  4. 向tiny6410中移植中移植linux-4.5.1内核(最新)
  5. extjs实现多国语音切换
  6. 如何设计App登录模块?
  7. pattern目录
  8. gmake使用注意
  9. Linux中seq命令的用法
  10. linux 优化(一)
  11. UVA1401 Remember the Word
  12. BZOJ1295 [SCOI2009]最长距离 最短路 SPFA
  13. 洛谷 P1474 货币系统 Money Systems(经典)【完全背包】+【恰好装满的最大方案数量】
  14. 什么原因接触接触impala的
  15. JS判断当前是否是IE浏览器,并返回时IE几?
  16. Vue2.0 新手完全填坑攻略——从环境搭建到发布
  17. Oracle NVL与NVL2函数
  18. MySQL设置远程连接服务器
  19. day1 HTML - &lt;head&gt;
  20. 出现RST的几种情况

热门文章

  1. ztree框架使用问题汇总
  2. SQL中CASE 的用法 转载
  3. git本地分支关联远程分支
  4. C# 创建一个WCF服务
  5. Java集合篇三:Vector
  6. 正则表达式验证问题(用户名、密码、email、身份证
  7. centos7 版本防火墻操作和配置
  8. windows环境下wampserver配置https
  9. envi利用矢量数据对影像做多边形裁剪 (转)
  10. 初学jboss