惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
量子位
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
J
Java Code Geeks
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
A
About on SuperTechFans
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
博客园 - Franky
H
Help Net Security
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog

博客园 - PC167

caiquan0 sheeeeee loggggg ato ctrl const loginnnnn pauto Shenmi llllll controller ctypes helper python test 1 Index was outside the bounds of the array. (Microsoft.SqlServer.Smo) contains 方法 mysql connector 和 sqlserver ado.net 的区别 javascript slice 转,SelectNodes + XPath jsgen 搭建 【转】基于第一个PhoneGap(cordova)的应用详解 回车和换行 html中空格转义字符 输出乘法表 sql server 相似度对比 sql server 2008 r2 报错 razor 拼接字符串 转,CV和resume的区别 b/s开发者的困境 sql 下,float和numeric
javascript bind
PC167 · 2014-10-29 · via 博客园 - PC167

bind的作用和apply,call类似都是改变函数的execute context,也就是runtime时this关键字的指向。但是使用方法略有不同。一个函数进行bind后可稍后执行。

定义:

function.bind(thisArg[,arg1[,arg2[,argN]]])

返回值 依然是function

这里与apply和call是不同的,apply和call是直接执行function,bind仅是将参数和thisArg缓存给function,却不会去触发

function testFun(name1 , name2, name3, name4){ 
console.log(name1+" ; " +name2+" ; " +name3+" ; " +name4 ); 
}

var test1 = testFun.bind(null,1,2);
var test2 = test1.bind(null,3);
test2(4);

结果:
1 ; 2 ; 3 ; 4 

可以看到bind是把参数一个个压进堆栈中去保存起来的

另外,bind也可以使用内置函数或者说可以用原型链调用

Array.prototype.slice.bind

换句话说,就是可以在不实例化类的情况下,直接调用共有方法(即 原型链上的方法,Person.prototype.display.bind)。