javascript中Array.prototype.reverse和Array.reverse有什么区别? [英] What is the difference between Array.prototype.reverse and Array.reverse in Javascript?

查看:144
本文介绍了javascript中Array.prototype.reverse和Array.reverse有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题涉及一个向现有 String构造函数添加新方法的应用程序.在Stoyan Stefanov的Java面向对象程序中,有一个示例,该示例使用 Array构造函数的.reverse()方法为 String构造函数创建一个. :

The question I have deals with an application of adding a new method to the existing String constructor. In Object Oriented Program for Javascript by Stoyan Stefanov, there is an example of using the Array constructor's .reverse() method to create one for the String constructor. Here is the example:

String.prototype.reverse = function() {
     return Array.prototype.reverse.apply(this.split('')).join('');
}

我认为Array的 .reverse()方法直接属于Array的对象.实际上,当我尝试使用此语句执行第二部分代码时:

I thought the .reverse() method of Array belonged directly to the object of Array. In fact, when I try to do the second bit of code with this statement:,

String.prototype.reverse = function() {
     return Array.reverse.apply(this.split('')).join(''); //WITHOUT the .prototype
}

var rev = "karunesh".reverse(); //applying the code here

我在Firebug控制台中收到一条错误消息,指出:"TypeError:调用函数Array.reverse时缺少参数0" .这对我来说没有任何意义.

I get an error in the Firebug Console stating: "TypeError: missing argument 0 when calling function Array.reverse". That does not make any sense to me.

当然,如果我重新添加 .prototype ,它就可以很好地工作.

And of course, if I add back in the .prototype, it works perfectly fine.

此外,如果我必须调用原型从 Array对象访问 .reverse()方法的情况下,那么我是否需要这种方法呢?必须对Javascript中的任何内置对象执行此操作?

Also, if is the case that I have to call upon prototype to access the .reverse() method from the Array object, then is it the case that I have to do that for any built-in object in Javascript?

谢谢您的帮助!

推荐答案

是这种情况,我必须调用原型才能从Array对象访问.reverse()方法

Is it the case that I have to call upon prototype to access the .reverse() method from the Array object

不.要访问对象上的方法,只需使用点符号即可访问它.您想要做的就是

No. To access a method on an object, just access it with dot notation. What you want to do is simply

return this.split('').reverse().join('');

apply (或call)可以:

var arr = this.split('');
return arr.reverse.apply(arr).join('');

,最后是arr.reverse === Array.prototype.reverse,因为那是Array对象的继承来源.您不是在访问Array构造函数对象本身的reverse方法,而是要通过其原型访问所有Array 实例共享的属性.但是,您几乎不需要显式地使用原型对象,仅当您正在处理不是Array实例(不共享原型)的对象时,例如arguments对象或NodeList s.

and finally arr.reverse === Array.prototype.reverse since that's where Array objects do inherit from. You are not accessing the reverse method on the Array constructor function object itself, you are to access the property that all Array instances share - via their prototype. Yet you hardly will ever need to use the prototype object explicitly, that's only when you're dealing with objects that are not Array instances (do not share the prototype) like arguments objects or NodeLists.

TypeError: missing argument 0 when calling function Array.reverse.这对我来说毫无意义.

TypeError: missing argument 0 when calling function Array.reverse. That does not make any sense to me.

Array.reverse是非标准的 Array通用方法,仅在Firefox中可用.目的是简化将Array原型方法应用于其他对象的构造,并且确实将类似数组的对象作为其第一个参数.一个例子:

Array.reverse is a non-standard Array generic method which is only available in Firefox. It's purpose is to simplify the construct of applying Array prototype methods on other objects, and it does take the array-like object as it's first parameter. An example:

Array.reverse([0, 1]) // [1, 0]

等效于

Array.prototype.reverse.apply([0, 1]);

但是,你在做

Array.reverse.apply([…]/*, undefined*/)

,它使用(无关)Array.reverse函数rel ="nofollow"> this值,没有实际参数,等效于

which is calling the Array.reverse function with the array for the (irrelevant) this value and no actual argument, equivalent to

Array.prototype.reverse.apply(undefined)

这会引发合法的异常.

这篇关于javascript中Array.prototype.reverse和Array.reverse有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆