码迷,mamicode.com
首页 > 其他好文 > 详细

How to use the function of bind

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

标签:c   style   class   blog   code   java   

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

标签:c   style   class   blog   code   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!