`
61party
  • 浏览: 1057735 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Function::apply 方法

 
阅读更多

METHOD: Function::apply

--------------------------------------------------------------------------------
Function.apply(thisObj[, argArray])

apply 方法允许调用某一个对象的一个方法,并且用指定的一个对象替换当前的对象.

参数
thisObj
可选项。将被用作当前对象的对象。
argArray
可选项。将被传递给该函数的参数数组。

The apply method allows you to call a function and specify what the keyword this will refer to within the context of that function. The thisArg argument should be an object. Within the context of the function being called, this will refer to thisArg. The second argument to the apply method is an array. The elements of this array will be passed as the arguments to the function being called. The argArray parameter can be either an array literal or the deprecated arguments property of a function.

The apply method can be used to simulate object inheritance as in the following example. We first define the constructor for an object called Car which has three properties. Then the constructor for a second object called RentalCar is defined. RentalCar will inherit the properties of Car and add one additional property of its own - carNo. The RentalCar constructor uses the apply method to call the Car constructor, passing itself as thisArg. Therefore, inside the Car function, the keyword this actually refers to the RentalCar object being constructed, and not a new Car object. By this means,the RentalCar object inherits the properties from the Car object.

以下的例子用 apply 方法模拟一个对象的继承.第一先定义一个Car对象的构造方法,有三个属性.第二再定义一个RentalCar对象的构造方法,RentalCar将继承Car的属性并加上一个自己的属性carNo. RentalCar构造方法使用 apply 方法去调用Car的构造方法,把自身当作thisArg参数传递过去.因此,在Car函数的内部,关键字 this 实际上已经被 RentalCar 对象代替,并不是一个新的 Car 对象.通过这个方法,RentalCar对象从Car对象继承了它的属性.


Code:
function Car(make, model, year)
{
this.make = make;
this.model = model;
this.year = year;
}

function RentalCar(carNo, make, model, year)
{
this.carNo = carNo;
Car.apply(this, new Array(make, model, year));
}

myCar = new RentalCar(2134,"Ford","Mustang",1998);
document.write("Your car is a " + myCar.year + " " + myCar.make + " " + myCar.model + ".") ;

Output:
Your car is a 1998 Ford Mustang.

NOTE: The apply method is very similar to the call method and only differs in that, up until now, you could use the deprecated arguments array as one of its parameters.

NOTE: apply 方法非常像 call 的方法,唯一的区别是 apply 方法传递的参数是 arguments 或 Array 对象

ps:网上找的一篇介绍关于apply的文章,顺便学习一下英语,呵呵,就翻译出来,第一次翻译文章,如果有什么不对的地方,请多指教

原帖地址:http://blog.csdn.net/cinnxu/archive/2004/12/17/219503.aspx

分享到:
评论

相关推荐

    玩转方法:call和apply

    在ECMAScript v3中,给Function原型定义了这两个方法,这两个方法的作用都是一样的:使用这两个方法可以像调用其他对象方法一样调用函数,这句话是从书上抄的,至少我是没读明白这是什么意思。 下面说简单易懂的,先...

    Function.prototype.apply()与Function.prototype.call()小结

    apply接受两个参数,第一个制定了函数体内this对象的指向,第二个参数为一个带下标的集合(可遍历对象),apply方法把这个集合中的元素作为参数传递给被调用的函数: var func = function(a, c, c){ alert([a,...

    js中apply方法的使用详细解析

    1、对象的继承,一般的做法是复制:Object.extendprototype.js的... }除此之外,还有种方法,就是:Function.apply(当然使用Function.call也是可以的) apply方法能劫持另外一个对象的方法,继承另外一个对象的属性

    javascript中apply和call方法的作用及区别说明

    1、call,apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例(就是每个方法)都有call,apply属性。既然作为方法的属性,那它们的使用...

    fasttext文本分类.zip

    fasttext文本分类 import re ... if isinstance(apply, FunctionType) or isinstance(apply, MethodType): sentence = apply(sentence) return ' '.join([i for i in jieba.cut(sentence) if i.str

    js中apply与call简单用法详解

    call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就是每个方法都有call, apply属性.既然作为方法的属性,那它们的使用就当然是...

    全面了解构造函数继承关键apply call

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args–>...

    apply和call方法定义及apply和call方法的区别

    如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而...call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实

    JavaScript中函数(Function)的apply与call理解

    主要介绍了JavaScript中函数(Function)的apply与call理解,本文讲解了JavaScript函数调用分为4中模式以及通过apply和call实现扩展和继承两方面,需要的朋友可以参考下

    小议Function.apply() 之一——(函数的劫持与对象的复制)

    关于对象的继承,一般的做法是用复制法: Object.extend 见protpotype.js 的实现方法... } 除此以外,还有一种不太常见的方法: Function.apply. apply 方法能劫持(<<Ajax>> 书中用到”劫持”一语,很生动啊)

    Javascript中call,apply,bind方法的详解与总结

    1.call/apply/bind方法的来源 2.Function.prototype.call() 3.Function.prototype.apply()  3.1:找出数组中的最大数  3.2:将数组的空元素变为undefined  3.3:转换类似数组的对象 4.Function.prototype.bind() ...

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

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

    Function.prototype.call.apply结合用法分析示例

    分析步骤如下: 1、将Function.prototype.call当成整体,call方法是由浏览器实现的本地方法,是函数类型的内部方法 var a = (Function.prototype.call).apply(function(a){return a;}, [0,4,3]); 2、fun

    js中apply和Math.max()函数的问题及区别介绍

    下面给大家介绍js中apply和Math.max()函数的问题,具体内容如下所示: ...XXX.apply是一个调用函数的方法,其参数为:apply(Function, Args), Function为要调用的方法,Args是参数列表,当Functio

    使用Function.apply()的参数数组化来提高 JavaScript程序性能的技巧

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧。 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值。 比如 alert(Math.max(5,8)) //8 alert(...

    完美解决IE低版本不支持call与apply的问题

    Function.prototype的apply和call是在1999年发布的ECMA262 Edition3中才加入的(1998年发布ECMA262 Edition2)。在此前的的浏览器如IE5.01(JScript 5.0)中是没有apply和call的。因此会带来一些兼容性问题,以下是...

    JavaScript中apply与call的用法意义及区别说明

    apply和call,它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数的方式有所区别: Function.prototype.apply(thisArg,argArray); Function.prototype.call(thisArg[,arg1[,arg2…]]); 从函数原型...

    javascript中call,apply,callee,caller用法实例分析

    实践一:call,apply 用来让一个对象去调用本不属于自己的方法,两者都可以传递参数,call的参数是列表形式,apply的参数是数组形式 var person = { "name":"Tom", "say":function(){ console.log("person say");...

    JS中使用apply方法通过不同数量的参数调用函数的方法

    apply()方法定义 函数的apply()方法和call方法作用相同,区别在于接收的参数的方式不同。 apply()方法接收两个参数,一个是对象,一个是参数数组。 apply()作用 1、用于延长函数的作用域 示例: var color='...

Global site tag (gtag.js) - Google Analytics