首页 > 其他 > 详细

How to use the function of bind

时间:2014-06-07 21:28:26      阅读:465      评论:0      收藏:0      [点我收藏+]

The usage of  bind  is to define a specified scope for called function. Because the key this is easy to refer another objet, experically window, instead of itself. Please pay attention to look at the following example:

bubuko.com,布布扣
 1 //Global variables
 2 window.name="Chromium";
 3 
 4 var person={
 5   name:"Pin"
 6 }
 7 
 8 function say()
 9 {
10    alert("My name is "+this.name+"!");
11 }
12 
13 say(); //the result is : My name is Chromium!
14 
15 var sayfunction=say.bind(person);
16 
17 sayfunction(); //the result is : My name is Pin!
bubuko.com,布布扣

When the function say assign to a variable sayfunction, the scope of this function has been changed. However, if you use the function of bind  to refer a specified object person, the scope will own the refer object person.

 

 

How to use the function of bind,布布扣,bubuko.com

How to use the function of bind

原文:http://www.cnblogs.com/pinxiong/p/3774910.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!