Ext.bind函数说明
bind( fn, [scope], [args], [appendArgs] ) : Function
Create a new function from the provided fn, change this to the provided scope, optionally overrides arguments for the call. (Defaults to the arguments passed by the caller)
从提供的fn中创建一个新的函数,改变this到提供的scope,可选的重写调用参数。(默认为由调用方传递的参数)。
Ext.bind is alias for Ext.Function.bind
Ext.bind是Ext.Function.bind的别名
Parameters
fn : Function
The function to delegate.
去代理的函数
scope : Object (optional)
The scope (this reference) in which the function is executed.
If omitted, defaults to the default global environment object (usually the browser window).
函数执行的scope(this引用)
如果忽略了,默认是默认的全局环境变量(通常浏览器window)
args : Array (optional)
Overrides arguments for the call. (Defaults to the arguments passed by the caller)
重写调用参数(默认为由调用者传递的参数)
appendArgs : Boolean/Number (optional)
if True args are appended to call args instead of overriding, if a number the args are inserted at the specified position
如果true,args被追加到调用参数而不是重写;如果一个数字,args被插入在指定的位置。
appendArgs:
不指定或者false:重写参数
true:追加参数
数字:指定位置插入参数
下面是使用例子:
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta charset="UTF-8"> 6 <title>定义</title> 7 <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /> 8 <link rel="stylesheet" type="text/css" href="js/ext4/resources/css/ext-all.css"> 9 <link rel="stylesheet" type="text/css" href="css/icon.css"> 10 <!-- <script type="text/javascript" src="js/ext4/bootstrap.js"></script> --> 11 <script type="text/javascript" src="js/ext4/ext-all-dev.js"></script> 12 13 <script type="text/javascript" src="js/ext4/locale/ext-lang-zh_CN.js"></script> 14 <script type="text/javascript"> 15 //设置命名空间路径 16 Ext.Loader.setPath('org', './js/org'); 17 Ext.onReady(initFn); 18 19 /** 20 * 初始化函数 21 */ 22 function initFn(){ 23 console.info('Ext准备完毕~~~'); 24 var doAdd = Ext.bind(function(x,y){ 25 var self = this; 26 console.info('self:',self); 27 console.info('self==window:',self==window); 28 return x+y; 29 }); 30 31 var result0 = doAdd(5,6); 32 console.info('result0:',result0); 33 34 var doSubtract = Ext.bind(function(x,y){ 35 var self = this; 36 console.info('self:',self); 37 console.info('self==window:',self==window); 38 return x-y; 39 },{fullName:'张泰松',age:28,address:'杭州市西湖区'}); 40 41 var result1 = doSubtract(15,9); 42 console.info('result1:',result1); 43 44 var doMultiply = Ext.bind(function(x,y){ 45 var self = this; 46 console.info('self:',self); 47 console.info('self==window:',self==window); 48 console.info("arguments:",arguments); 49 return x*y; 50 },{fullName:'李超军',age:30,email:'1032160369@qq.com'},[18,8],false); 51 52 var result2 = doMultiply(16,6); 53 console.info('result2:',result2); 54 55 var doDivide = Ext.bind(function(x,y){ 56 var self = this; 57 console.info('self:',self); 58 console.info('self==window:',self==window); 59 console.info("arguments:",arguments); 60 return x/y; 61 },{fullName:'武利丹',age:29,email:'1175173151@qq.com'},[4,8],1); 62 63 var result3 = doDivide(16,2); 64 console.info('result3:',result3); 65 /* 66 appendArgs: 67 不指定或者false:重写参数 68 true:追加参数 69 数字:指定位置插入参数 70 */ 71 } 72 </script> 73 </head> 74 <body> 75 76 </body> 77 </html>