js 对call apply bind理解

请参考 http://www.cnblogs.com/xljzlw/p/3775162.htmlcall applyvar mtt = {
    name: "mtt",
    sayHello: function (age) {
         console.log("hello, i am ", this.name + " " + age " years old");
     }
};

var  xjs = {
    name: "xjs ",};

mtt.sayHello(24);// hello, i am zlw 24 years old

apply参数需要是数组形式,而call参数需要用逗号隔开24,25
mtt .sayHello.call(xjs , 24);// hello, i am xjs 24 years oldmtt .sayHello.apply(xjs , [24]);// hello, i am xjs 24 years old

bind方法传递给调用函数的参数可以逐个列出,也可以写在数组中。bind方法与call、apply最大的不同就是前者返回一个绑定上下文的函数
mtt .sayHello.bind(xjs)(24); //hello, i am xlj 24 years old
mtt .sayHello.bind(xjs)([24]); //hello, i am xlj 24 years old

bind
var bind = Function.prototype.call.bind(Function.prototype.bind);

var zlw = {
 name: "zlw"
};

function hello () {
  console.log("hello, I am ", this.name);
}

bind(hello, zlw)() // hello, I am zlw
时间: 2024-08-03 15:36:54

js 对call apply bind理解的相关文章

js 的 call apply bind 方法

js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) thisObj可选项.将被用作当前对象的对象.arg1, arg2, argN ..可选项.将被传递方法参数序列. 如果没设置严格模式 “use strict” 当thisObj 不存在或 为 undefined 或为 null 或为 this 时,则隐式地指向 全局对象(在浏览器中即为 wi

JS中call,apply,bind方法的总结

why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user: "小马扎", fn: function(){ console.log(this.user); } } var b = a.fn; b(); // undefined 我们是想打印对象a里面的user却打印出来undefined是怎么回事呢?如果我们直接执行a.fn()是可以的. var a = { user: "小马扎

js: this,call,apply,bind 总结

对js中的一些基本的很重要的概念做一些总结,对进一步学习js很重. 一.this JavaScript 中的 this 总是指向一个对象,而具体指向那个对象是在运行时基于函数的执行环境动态绑定的,而非函数声明时的环境     实际应用中 this 的指向大致可以分为以下 4 中:         1. 作为对象的方法调用         2. 作为普通函数掉用         3. 构造器调用         4. Function.prototype.call 或 Function.proto

原生JS实现call,apply,bind函数

1. 前言 使用原生JS实现call和apply函数,充分了解其内部原理.call和apply都是为了解决改变this的指向.作用都相同,只是传参的方式不同.除了第一个参数外,call可以接受一个参数列表,apply只接受一个参数数组. 2. call函数 2.1 描述 call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数. 2.2 语法 fun.call(thisArg, arg1, arg2, ...) 2.3 参数 thisArg:可选的.在 fun 函数

JS之call/apply/bind

测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 函数中this指向window对象 2.test.call(thisObj,param1,param2....); thisObj:test函数中this的指向 param1,param2...:传给test函数的参数 3.test.call(null,3)或者test.call(undefine

理解 JavaScript call()/apply()/bind()

理解 JavaScript this 文章中已经比较全面的分析了 this 在 JavaScript 中的指向问题,用一句话来总结就是:this 的指向一定是在执行时决定的,指向被调用函数的对象.当然,上篇文章也指出可以通过 call() / apply() / bind() 这些内置的函数方法来指定 this 的指向,以达到开发者的预期,而这篇文章将进一步来讨论这个问题. 先来回顾一下,举个简单的例子: var leo = { name: 'Leo', sayHi: function() {

js的call,apply,bind的使用与区别

在原生js中会有三个很常见的函数,call,apply,bind 他们的作用就是改变当前函数的this指针, 但是细微来说他们还是有不同的. 1)call,apply都是执行某一函数,发现this有变得时候才使用的(进行时) 2)bind是在函数进行调用之前,就强行给变了this的指向(进行前),它的效果是返回一个函数(只是给变了this指向) 说的很多了,不说了 demo : function Foo(name){ this.name=name; } Foo.prototype.getName

学些js call apply bind的新的收获,做个记录,希望大家多多指导

call: 改变当前执行上下文的this指针 function dog(color){ this.color = color; } dog.prototype.eat = function(){ return this.color+ " dog can eat food"; } var blackDog = new dog('black'); blackDog.eat();// black dog can eat food var redDog = { color: "red

前端JS面试题汇总 Part 3 (宿主对象与原生对象/函数调用方式/call与apply/bind/document.write)

原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questions.md 最近将持续翻译JavaScript面试题,希望对各位有所帮助. (文章中斜体字部分为译者添加) 目录: Part 1(事件委托/this关键字/原型链/AMD与CommonJS/自执行函数) Part 2 (null与undefined/闭包/foreach与map/匿名函数/代码组织)