JS中的bind方法
# bind的机制
```
var foo = function(){}
var bar = foo;
console.log(foo === bar) //true
/--------------------------------------/
var foo = function(){}
var bar = function(callback1,callback2){
console.log(callback1 === callback2) //true
}
bar(foo,foo)
/--------------------------------------/
var foo = function(){}
var bar = function(){}
console.log(foo === bar) //false //两个函数,不在同一内存地址中,所以返回了false
对象有属性和方法,函数也是对象的一种,我们也可以称之为函数对象,既然是对象那么就有熟悉和方法,bind就是函数对象下面的一个方法。
我们都知道对象是引用类型,引用的是内存中的一个地址,上面的callback1 === callback2这两个指针就指向了一个地址所以为true。
<br>
<br>
var foo = function(){}
var fooBind = foo.bind()
console.log(foo === fooBind) // false
/--------------------------------------/
var foo = function(){}
var fooBind = foo.bind()
var bar = function(callback1,callback2){
console.log(callback1 === callback2) //false
}
bar(foo,fooBind)
上面的代码中的 foo.bind()的返回值是一个新的函数,其实是将foo拷贝了一份,他们两个已经没有了任何关系,也就是说foo和fooBind已经不在同一个内存地址中了,所以返回了false。
<br>
<br>
var foo = function(){}
var fooBind1 = foo.bind()
var fooBind2 = foo.bind()
console.log(fooBind1 === fooBind2) //false
虽然上面的fooBind1和fooBind2都使用了foo.bind()进行了拷贝,但他们也并没有任何关系,完全两个独立的函数。
<br>
<br>
var obj = {key:"value"}
var foo = function(){
return this;
}
var fooBind1 = foo.bind(obj)
var fooBind2 = foo.bind(obj)
console.log( fooBind1() === fooBind2 () ) //true 他们都指向了同一个obj
console.log( fooBind1 === fooBind2 ) //false 他们分别是存在两个不同内存地址中的,与函数中的this无关,所以返回了false
有人就会这么想了,bind方法的主要目的是为了改变函数内的this指向,那如果我用bind方法拷贝了一个fooBind1和一个fooBind2让他们中的this都指向obj这个对象,那这fooBind1和fooBind2还在同一内存地址中吗
<br>
<br>
<br>
<br>
# bind的用法
var obj = {key:"value"}
var foo = function(){
console.log(this) //obj
}.bind(obj)
foo()
/--------------------------------------/
var obj = {key:"value"}
var foo = function(){
console.log(this) //obj
}
foo.bind(obj)() //也可以这样
让foo中的this指向obj。注意:foo已经并不是foo本身了,而是调用了bind之后返回的一个新的函数
<br>
<br>
var obj = {
method:function(){
setTimeout(function(){
console.log(this) //obj 注意:function(){console.log(this)}.bind(this) 返回值是一个函数
}.bind(this),1000)
}
}
obj.method()
/--------------------------------------/
var obj = {
method:function(){
var arg = function(){
console.log(this) //obj
}
var argBind = arg.bind(this) //返回来的argBind函数与arg函数完全没有任何关系。
setTimeout(argBind,1000) //与上面的写法完全相等
}
}
obj.method()
简单的使用
JS中的bind方法的更多相关文章
- JS中的bind方法学习
EcmaScript5给Function扩展了一个方法:bind 众所周知 在jQuery和prototype.js之类的框架里都有个bind jQuery里的用途是给元素绑定事件 $("# ...
- 分享一个js中的bind方法使用
来源:http://www.cnblogs.com/yuzhongwusan/archive/2012/02/13/2348782.html Js代码 复制代码 代码如下: var first_obj ...
- js中的bind方法的实现方法
js中目前我遇见的改变作用域的5中方法:call, apply, eval, with, bind. var obj = { color: 'green' } function demo () { c ...
- 秒味课堂Angular js笔记------Angular js中的工具方法
Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...
- js中的tostring()方法
http://blog.sina.com.cn/s/blog_85c1dc100101bxgg.html js中的tostring()方法 (2013-11-12 11:07:43) 转载▼ 标签: ...
- JS中通过call方法实现继承
原文:JS中通过call方法实现继承 讲解都写在注释里面了,有不对的地方请拍砖,谢谢! <html xmlns="http://www.w3.org/1999/xhtml"& ...
- jQuery与JS中的map()方法使用
1.jquery中的map()方法 首先看一个简单的实例: $("p").append( $("input").map(function(){ return $ ...
- JavaScript -- 时光流逝(二):js中数组的方法
JavaScript -- 知识点回顾篇(二):js中数组的方法 1. 数组 (1)定义数组,数组赋值 <script type="text/javascript"> ...
- ASP.NET#使用母版时,如果要使用js中的getElementById()方法取得某个内容页的元素时要注意的问题
当使用母版,要使用js中的getElementById()方法取得某个内容页的元素时,所选取的id并不是母版中内容页的id,而是在设计内容页时设定的id例子:母版页: ...... <head ...
随机推荐
- 数学--数论--HDU - 6124 Euler theorem (打表找规律)
HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the ...
- Servlet --启动时创建、配置url、ServlectContext、初始化参数、获取资源
servlet的版本的区别 2.5版本, Servlet的配置只支持在xml文件中的配置 3.0版本: Servlet的配置支持在xml文件中的配置, 也可以使用注解的方式, 默认使用注解 让服务器在 ...
- 2020牛客寒假算法基础集训营1 J题可以回顾回顾
2020牛客寒假算法基础集训营1 这套题整体来说还是很简单的. A.honoka和格点三角形 这个题目不是很难,不过要考虑周全,面积是1,那么底边的长度可以是1也可以是2, 注意底边1和2会有重复的, ...
- (二)Redis在Mac下的安装与SpringBoot中的配置
1 下载Redis 官网下载,下载 stable 版本,稳定版本. 2 本地安装 解压:tar zxvf redis-6.0.1.tar.gz 移动到: sudo mv redis-6.0.1 /us ...
- QTreeWidget更新后保存节点的展开状态
class Xx : public QWidget { Q_OBJECT struct ItemState{ ItemState(); int _id; bool _isExpend; }; publ ...
- failed parsing overlays.
clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:
- Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别
一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕 ...
- 设计模式之GOF23责任链模式
责任链模式chain of responsibility 将能够处理同一类请求的对象连成一条链,所提交的请求依次在链上传递,直到传递至有能力处理该请求的对象,不能则传给链上下一个 场景: -打牌时 - ...
- 手机网页,div内滚动条,以及div内部滚动条拉到底部之后触发事件
var gao = document.documentElement.clientHeight; var headHeight = parseInt($('.yhead').css('height') ...
- Spring全家桶之springMVC(四)
路径变量PathVariable PathVariable Controller除了可以接收表单提交的数据之外,还可以获取url中携带的变量,即路径变量,此时需要使用@PathVariable ...