IE8支持function.bind()方法
这个 bind 方法仅仅有在 ie10 版本号的浏览器才得到原生支持,低于该版本号的浏览器下运行时会得到一个 undefined 的错误提示。于是仅仅好再次上网 google 解决方式,功夫不负有心人,我们在 firefox 的开发站找到了解决方式,那就是添加 property 原型使得全部浏览器都能支持 bind 方法,代码例如以下:
<script type="text/javascript">
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
</script>
主要解决“百度地图”官网上的样例的bug,摘取例如以下代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
#allmap{width:100%;height:500px;}
p{margin-left:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=39b92e64ae5622663ceceaccd8ab8eb1"></script>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<title>给多个点加入信息窗体</title>
<script type="text/javascript">
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
</script>
</head>
<body>
<div id="allmap"></div>
<p>点击标注点,可查看由纯文本构成的简单型信息窗体</p>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.417854,39.921988), 15);
var data_info = [[116.417854,39.921988,"地址:北京市东城区王府井大街88号乐天银泰百货八层"],
[116.406605,39.921585,"地址:北京市东城区东华门大街"],
[116.412222,39.912345,"地址:北京市东城区正义路甲5号"]
];
var opts = {
width : 250, // 信息窗体宽度
height: 80, // 信息窗体高度
title : "信息窗体" , // 信息窗体标题
enableMessage:true//设置同意信息窗发送短息
};
for(var i=0;i<data_info.length;i++){
var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1])); // 创建标注
var content = data_info[i][2];
map.addOverlay(marker); // 将标注加入到地图中
marker.addEventListener("click",openInfo.bind(null,content));
}
function openInfo(content,e){
var p = e.target;
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗体对象
map.openInfoWindow(infoWindow,point); //开启信息窗体
}
</script>
IE8支持function.bind()方法的更多相关文章
- ES6下的Function.bind方法
在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...
- 解决function.bind()方法
这个 bind 方法只有在 ie10 版本的浏览器才得到原生支持,低于该版本的浏览器下执行时会得到一个 undefined 的错误提示. 于是只好再次上网 google 解决方案,功夫不负有心人,我们 ...
- Function.bind 方法
this.num = 9; var mymodule = { num: 81, getNum: function() { return this.num; } }; module.getNum(); ...
- js函数中的apply()、call()、bind()方法
ECMAScript中的函数是对象,因此函数也有属性和方法.每个函数都包含两个属性:length和prototype,且每个函数包含两个非继承而来的方法apply()和call().这两个方法的用途都 ...
- 理解JS中的call、apply、bind方法(*****************************************************************)
在JavaScript中,call.apply和bind是Function对象自带的三个方法,这三个方法的主要作用是改变函数中的this指向. call.apply.bind方法的共同点和区别:app ...
- 浅析 JavaScript 中的 Function.prototype.bind() 方法
Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个对象,bind() 方法会创建一个函数,函数体内this对象的值会被绑定到传入bind() 函数 ...
- prototype.js中Function.prototype.bind方法浅解
prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...
- JavaScript 中的 Function.prototype.bind() 方法
转载自:https://www.cnblogs.com/zztt/p/4122352.html Function.prototype.bind()方法 bind() 方法的主要作用就是将函数绑定至某个 ...
- 【转载】JS中bind方法与函数柯里化
原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...
随机推荐
- JAVA一些基础概念
Java (计算机编程语言) Java是一门面向对象编程语言,不仅吸收了C++语言的各种长处,还摒弃了C++里难以理解的多继承.指针等概念.因此Java语言具有功能强大和简单易用两个特征. Java语 ...
- mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure
err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...
- 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏
转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...
- Cygwin 与 MinGW/MSYS/MSYS2,如何选择?甚至还有GNU utilities for Win32
Cygwin与MinGW/MSYS,如何选择? 2012-11-03更新:加入 MSYS 的内容. 2013-10-15更新:修改表格格式,加入介绍链接. 2014-12-17更新:加入 MSYS2 ...
- Seagate-保修验证(za25shrx)
保修验证 http://support.seagate.com/customer/zh-CN/warranty_validation.jsp Seagate 保修验证 End User ...
- [Angular] Router outlet events
For example, we have a component which just simply render router-outlet: import { Component } from ' ...
- 【14.06%】【hdu 5904】LCIS
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 批量解决 word/wps 中公式和文字不对齐的问题
完美解决Word或wps中中公式和文字对不齐的问题 在 word 的各个版本中,当公式和字符同时出现时,尤其是发生公式的拷贝粘贴时,公式往往会出现上飘或下移的情况,这里给出一个简单易行的解决方案: 全 ...
- 【42%】【hdu1166】排兵布阵(树状数组解法&&线段树解法)
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- Qt 模仿QQ截图 动态吸附直线
最近在学Qt.学东西怎么能不动手. 就写了些小程序.看QQ截图能够动态吸附直线的功能挺有意思,所以就模仿了一个. 先上效果图 界面很简单..呵呵 移动鼠标,会把鼠标所在最小矩形选中.把没有选中的地方给 ...