`

How to bind a method to an object from angularjs source code

 
阅读更多
function bind(self, fn) {  
  var slice = [].slice;  
  var curryArgs = arguments.length > 2  
    ? slice.call(arguments, 2, arguments.length)  
    : [];  
  if (typeof fn == "function" && !(fn instanceof RegExp)) {  
    return curryArgs.length  
      ? function() {  
          return arguments.length  
            ? fn.apply(self, curryArgs.concat(slice.call(arguments, 0, arguments.length)))  
            : fn.apply(self, curryArgs);  
        }  
      : function() {  
          return arguments.length  
            ? fn.apply(self, arguments)  
            : fn.call(self);  
        };  
  } else {  
    // in IE, native methods are not functions and so they can not be bound (but they don't need to be)  
    return fn;  
  }  
}  
  
function Foo(name){  
this.name= name;  
}  
  
var print = function (prefix){  
console.log(prefix+this.name);  
}  
  
var cat = new Foo("cat");  
  
var printCat = bind(cat, print, "I am a ");  //printCat will always be bound to cat from now on.
printCat(); // I am a cat 

function zoo(printCat){
  printCat();
}

zoo(printCat);// where ever we use the function printCat, it is bound to cat
 
分享到:
评论

相关推荐

    Failed to bind to address(解决方案).md

    Failed to bind to address(解决方案).md

    How to bind data to different DataTemplate in Universal Windows Platform apps

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { if (item is Type1) return Template1; else if (item is Type2) return Template2; else return base....

    BIND9.12.0.x64 from windows

    BIND,全称Berkeley Internet Name Domain,是互联网上最广泛使用的域名系统(DNS)服务器软件之一,由Internet Systems Consortium(ISC)开发并维护。本文将详细介绍在Windows操作系统中使用BIND9.12.0.x64进行DNS...

    Mastering JavaScript Object-Oriented Programming

    From there, we'll move on to some advanced patterns for object creation and you'll get a strong idea of how to use interesting patterns to present data to users and to bind data. We'll use the famous...

    Unable to load class ‘javax.xml.bind.JAXBException‘(csdn).pdf

    在Android Studio中遇到"Unable to load class ‘javax.xml.bind.JAXBException‘"的错误,通常是由于Java运行时环境(JRE)或开发工具包(JDK)与项目的兼容性问题导致的。`javax.xml.bind.JAXBException`是Java的...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    How can we use a class Foo in a header file without access to its definition? We can declare data members of type Foo* or Foo&. We can declare (but not define) functions with arguments, and/or ...

    dns_howto

    ### DNS基础知识与BIND服务器配置详解 #### 一、DNS(Domain Name System)概述 DNS,即域名系统,是互联网的一项核心服务,它负责将人类可读的域名转换为计算机能够理解的IP地址,从而实现互联网上的资源定位和...

    ldap client C source code

    标题中的“ldap client C source code”指的是使用C语言编写的LDAP客户端程序的源代码。这个项目由中兴通讯(ZTE)开发,表明它可能是一个企业级的解决方案,旨在跨多个平台运行,包括Windows(通过Visual C++编译)...

    Pro DNS and BIND 10 英文版

    This book focuses on running DNS systems based on BIND 9.3.0the first stable release that includes support for the latest DNSSEC (DNSSEC...standards and a major functional upgrade from previous BIND 9 releases....

    sourcecode.zip

    在"sourcecode.zip"这个压缩包中,我们可能找到了有关于在Android 2.3版本中添加新进程并启动核心层服务的源代码示例。这里我们将详细探讨两种Binder通信方式:BpService和BnService的使用。 首先,让我们了解...

    计算机网络第六版答案

    Wifi (802.11) In a wireless LAN, wireless users transmit/receive packets to/from an base station (i.e., wireless access point) within a radius of few tens of meters. The base station is typically ...

    BIND-LINUX64位包

    Fixed a bug causing an insecure delegation from one "static-stub" zone to another to fail with a broken trust chain. [RT #35081] Fixed problem where iterative responses could be discarded when ...

    深入理解AngularJS中的ng-bind-html指令

    在AngularJS中,`ng-bind-html`指令是一个用于将数据绑定到HTML元素上的特殊方法,与`ng-bind`指令有着显著的区别。`ng-bind`通常用于将文本内容直接插入到DOM(文档对象模型)中,而`ng-bind-html`则允许你插入包含...

    最新AngularJS开发宝典视频教程 后盾网AngularJS培训视频教程 后盾网.txt

    ├最新AngularJS开发宝典—第006讲 表达式与ng-bind及ng-cloak解决闪屏问题.mp4 ├最新AngularJS开发宝典—第007讲 ng-model在表单中双向绑定实例分析讲解.mp4 ├最新AngularJS开发宝典—第008讲 ng-value的使用实例...

    angularjs中ng-bind-html的用法总结

    AngularJS是一个非常流行的前端JavaScript框架,它通过使用数据绑定、依赖注入等特性,大大简化了动态Web应用的开发。在AngularJS中,ng-bind-html指令允许我们将HTML字符串绑定到模型变量,并将其内容插入到DOM中。...

    Sortable前端框架

    scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling. scrollSpeed: 10, // px setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) { data...

    WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4

    The chapter provides examples of how to print documents and controls directly from a WPF application. #### Data Binding (Chapter 18 & Appendix J) Chapters 18 and Appendix J provide a comprehensive ...

Global site tag (gtag.js) - Google Analytics