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各项特性的支持情 ...
随机推荐
- Linux中IRC通讯工具Pidgin的基本用法
这是我发在南昌大学开源小组里的一篇教程 IRC IRC是个古老的产物,在图形界面流行之前就出现了.可是在现在非常多开源社区都是利用IRC来进行团队交流或对用户提供技术支持的,比方维基百科,所以还是有必 ...
- Oracle批量插入在C#中的应用
public void SetUserReportResult(int[] reportId, bool isReceive, string result) { if (reportId == nul ...
- Microsoft Bot Framework 链接至微信公共号
如何将 Microsoft Bot Framework 链接至微信公共号 说到 Microsoft Bot Framework 其实微软发布了已经有一段时间了,有很多朋友可能还不太了解,微软Bot ...
- UVA 11859 - Division Game
看题传送门 题目大意 有一个n * m的矩阵,每个元素均为2~10000之间的正整数,两个游戏者轮流操作.每次可选一行中的1个或者多个大于1的整数把它们中的每个数都变成它的某个真因子,比如12可以变成 ...
- 分析Net 内存对象
.Net 内存对象分析 在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的 ...
- 无法顺利删除问题处理一则-ORA-00604和ORA-00942错误
1.以sys/oracle登录 2. -- Create tablecreate table ARGUMENT$( OBJ# NUMBER not null, PROCEDURE ...
- bc-win32-power-echo-vim-not-work
http://gnuwin32.sourceforge.net/packages.html linux ok, but win32 not ok [root@130-255-8-100 ~]# ech ...
- JS生成一个种子随机数(伪随机数)
原文链接:https://geniuspeng.github.io/2016/09/12/js-random/ 最近有一个需求,需要生成一个随机数,但是又不能完全随机,就是说需要一个种子seed,se ...
- 【翻译自mos文章】Clusterware间歇性的hang,命令报CRS-184而且Network Socket Files in /tmp/.oracle or /var/tmp/.oracle被删
来源于: Clusterware Intermittently Hangs And Commands Fail With CRS-184 as Network Socker Files in /tmp ...
- IAdjustCountOption--动态设置recycleView的itemCount(不须要改动数据源)
概述 RecycleViewUtil是新增的一个主要针对RecycleView的一个工具类.该工具类中提供了部分RecycleView可能会使用到的方法,当中也包含了一些用来增强HeaderRecyc ...