bind() 函数兼容

为了搞清这个陌生又熟悉的bind,google一下,发现javascript1.8.5版本中原生实现了此方法,目前IE9+,ff4+,chrome7+支持此方法,opera和safari不支持(MDN上的说明)。 
bind的作用和apply,call类似都是改变函数的execute context,也就是 runtime 时 this 关键字的指向。但是使用方法略有不同。一个函数进行bind后可稍后执行。

bind 方法实现:绑定this  和 “科里化”:

function getConfig(colors,size,otherOptions){

  console.log(colors,size,otherOptions)

}

var defaultConfig = getConfig.bind(null,"#f00","1024*768");

defaultConfig("123");  //#f00 1024*768 123

defaultConfig("345");  //#f00 1024*768 345

因为bind 方法是EMS5 以后提出的方法,对于老的浏览器,可以使用手写一个bind 方法

if(!Function.prototype.bind){

  Function.prototype.bind = function(oThis){

    if(typeOf this !== ‘function‘){

      throw new TypeError(‘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?this:oThis,

           aArgs.concat(Array.prototype.slice.call(arguments)))

      }

    fNOP.prototype = this.prototype;

    fBound.prototype = new fNOP();

    return fBound;

  }

}

时间: 2024-12-14 21:19:05

bind() 函数兼容的相关文章

Function.prototype.bind函数兼容处理

今天敲代码的时候,想了想bind函数 Function.prototype.bind = Function.prototype.bind || function (target) { var self = this; return function (args) { if (!(args instanceof Array)) { args = [args]; } self.apply(target, args); }; }; /*example 1*/ function f1(y, z){ re

模拟实现兼容低版本IE浏览器的原生bind()函数功能

模拟实现兼容低版本IE浏览器的原生bind()函数功能: 代码如下: if(!Function.prototype.bind){   Function.prototype.bind=function(oThis){     if (typeof this !== 'function'){       throw new TypeError('调用者不是当前函数对象');     }       var aArgs = Array.prototype.slice.call(arguments, 1

《Javascript高级程序设计》读书笔记之bind函数详解

为什么需要bind var name = "The Window"; var object = { name: "My Object", getNameFunc: function () { return function () { return this.name; } } }; alert(object.getNameFunc()()); //"The Window" object.getNameFunc()返回一个匿名函数,在全局环境调用该

(十一)socket、connect、bind函数详解

一.socket函数 1.头文件: #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> 2.函数原型: int socket(int domain, int type, int protocol); socket函数类似于open,用来打开一个网络连接,如果成功则返回一个网络文件描述符(int类型),之后我们操作这个网络连接都通过这个网络文件描述符. dimain:域,网络域,网络地址范围(IPV4或IPV6

socket bind函数

1 #include<stdio.h> 2 #include<sys/types.h> 3 #include<sys/socket.h> 4 #include<netinet/in.h> 5 #include<arpa/inet.h> 6 #include<unistd.h> 7 #define PORT 2345 8 #include<stdlib.h> 9 #include<string.h> 10 int

C++ bind函数适配器

在我之前的博客让类成员函数指针成为可调用对象里有提到bind函数适配器,现在在这里介绍一下. 适配器可以让某一个看上去像另一个行为,比如栈.队列等,底层使用链表去完成功能,我们通过操作底层链表去实现栈.队列等的行为.bind是函数适配器,通过bind返回的可调用对象去完成指定函数的功能. bind的头文件是<functional>,可使用命名空间std::placeholders的_n形式引用外部参数,属于C++11标准. 使用例子: // main.cpp #include <func

C++拾遗--bind函数绑定

C++拾遗--bind函数绑定 前言 函数绑定bind函数用于把固定的参数与已知的函数进行绑定,形成新的函数,从而减少参数的个数,降低函数的调用难度.需要指出:bind就是函数适配器. bind函数 实例 #include <iostream> #include <functional> using namespace std; using namespace std::placeholders; int main() { auto fun = [](int *array, int

angular.bind() 函数

angular.bind bind 函数有三个参数, 参一:是一个对象 参二:是一个 function 参三:是用来给参二传参数的,可写可不写,看你心情 参数也可以在调用函数的时候传,也可以当做第三个参数传 在函数的体内可以访问参数一的所有属性值 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document<

2.cocos2dx 3.2中语法的不同之处,lambada表达式的使用和function和bind函数的使用

1        打开建好的T32  Cocos2dx-3.2的一个项目 2        设置Cocos显示窗口的位置是在AppDelegate.cpp中: 3  设置自适应窗口大小的代码是在上面的代码后面紧接着就添加: glview->setDesignResolutionSize(480,320, ResolutionPolicy::EXACT_FIT); 3        cocos2d-x-3.2项目案例(3.2版本之后都去掉了CC前缀) 4        项目目录结构如下: 编写公共