`
biyeah
  • 浏览: 201422 次
  • 来自: ...
社区版块
存档分类
最新评论

javascript中的bind

 
阅读更多
http://www.prototypejs.org/api/function/bind

In JavaScript, functions are executed in a specific context (often referred to as “scope”). Inside the function the this keyword becomes a reference to that scope. Since every function is in fact a property of some object—global functions are properties of the window object—the execution scope is the object from which the function was called, or (more precisely) the object that holds a reference to the function:


window.name = "the window object"

function scopeTest() {
  return this.name
}

// calling the function in global scope:
scopeTest()
// -> "the window object"

var foo = {
  name: "the foo object!",
  otherScopeTest: function() { return this.name }
}

foo.otherScopeTest()
// -> "the foo object!"

Because of the dynamic nature of the language, we can’t be sure that, for instance, otherScopeTest() will always be called on our foo object. The reference to it can be copied somewhere else, like on the window object:


// ... continuing from the last example

// note that we aren't calling the function, we're simply referencing it
window.test = foo.otherScopeTest
// now we are actually calling it:
test()
// -> "the window object"

The last call demonstrates how the same function can behave differently depending on its execution scope.

When you begin passing around function references in your code, you often want them to become fixated on a specific scope. Prototype can guarantee that your function will execute with the object you want under the this keyword just by invoking bind on it. You can also save the returned function and use it multiple times if you need so.

Examples
The code below is simply proof-of-concept:

var obj = {
  name: 'A nice demo',
  fx: function() {
    alert(this.name);
  }
};

window.name = 'I am such a beautiful window!';

function runFx(f) {
  f();
}

var fx2 = obj.fx.bind(obj);

runFx(obj.fx);
runFx(fx2);
分享到:
评论

相关推荐

    浅析Javascript中bind()方法的用法与实现_.docx

    JavaScript中的`bind()`方法是一个非常重要的函数,它主要用于改变函数内部`this`的指向,以及创建带有预定义参数的新函数。下面将详细讲解`bind()`的用法和实现原理。 首先,`bind()`方法的基本用法是接收一个或多...

    javascript中bind函数的作用实例介绍

    bind()的方法在ie,6,7,8中不适用,需要扩展通过扩展Function prototype可以实现此方法,下面为大家介绍下javascript中bind函数的作用

    浅析Javascript中bind()方法的使用与实现

    下面小编就为大家带来一篇浅析Javascript中bind()方法的使用与实现。小编觉得挺不错的,现在分享给大家,一起跟随小编过来看看吧

    javascript中利用柯里化函数实现bind方法_.docx

    柯里化在JavaScript中通常用于创建具有特定上下文(`this`关键字指向)的函数,这正是`bind`方法的核心功能。 `bind`方法是JavaScript中的一个内置函数,它的主要作用是改变函数内部`this`的指向,以及可以预先设置...

    浅谈javascript中的call、apply、bind_.docx

    JavaScript 中的 call、apply、bind 方法详解 JavaScript 中的 call、apply、bind 方法是 Function 对象自带的三个方法,这三个方法的主要作用是转变函数中的 this 指向,从而可以达到“接花移木”的效果。下面将对...

    javascript bind绑定函数代码

    JavaScript中的`bind`函数是JavaScript语言的一个重要特性,主要用于改变函数内部`this`的指向。在JavaScript中,`this`的值取决于函数是如何被调用的,而不是定义时的位置。因此,`bind`函数的存在解决了`this`上...

    javascript中apply、call和bind的用法区分_.docx

    javascript中apply、call和bind的用法区分_.docx

    javascript之bind使用介绍

    前几天看到一个面试题,题目是这样的: 请你说说对javascript中apply,call,bind的理解? 首先apply和call是老生常谈的东西,但是对于bind,我愣了下,因为这个词是jquery中使用频率很高的一个方法,用来给DOM元素...

    前端开源库-react-autobind

    React Autobind 是一个在前端开发中常用的工具库,尤其在使用React框架时。这个库主要解决的问题是JavaScript中的上下文(this)丢失问题。在React组件中,当我们定义了一个方法并在事件处理或者某些需要调用组件...

    开启Javascript中apply、call、bind的用法之旅模式

    在Javascript中,Function是一种对象。Function对象中的this指向决定于函数被调用的方式,使用apply,call 与 bind 均可以改变函数对象中this的指向。

    js代码-JavaScript bind方法

    JavaScript中的`bind`方法是函数式编程中一个非常重要的特性,它允许我们固定函数的上下文(即`this`关键字的值)以及预设函数的参数,从而创建一个新的函数。这个新函数在调用时会保持`bind`时设置的上下文和参数,...

    跟我学习javascript的call(),apply(),bind()与回调

    JavaScript 中通过call或者apply用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。简单的说就是改变函数执行的上下文,这是最基本的用法。两个方法基本区别...

    JavaScript中this关键字使用方法详解

    在JavaScript编程语言中,`this`关键字是一个至关重要的概念,它常常引发初学者的困惑,因为它的值在不同的上下文中可能会有所不同。`this`关键字主要用来引用对象的上下文,或者说是当前执行环境中的对象。在本文中...

    jQuery中bind(),live(),delegate

    今天我们将深入探讨jQuery中的事件绑定方法,包括`bind()`, `live()`, `delegate()`,以及后来推出的`on()`方法。这四个方法都是为了帮助开发者更方便地管理页面上的事件,特别是对于动态生成的元素。 1. **bind()*...

    JavaScript程序设计javascript中的thi

    JavaScript是Web开发中不可或缺的一部分,尤其在前端领域更是发挥着核心作用。`this`是JavaScript中的一个关键概念,它在不同上下文中具有不同的含义,理解并掌握`this`的用法对于编写高质量的JavaScript代码至关...

    JavaScript程序设计javascript中this

    JavaScript中的`this`关键字是程序设计中的一个核心概念,它在不同上下文环境中有着不同的指向,这使得理解和掌握`this`的用法至关重要。在JavaScript中,`this`的值取决于函数调用的方式,而不是定义的方式,这为...

Global site tag (gtag.js) - Google Analytics