Javascript中的Array.prototype.reverse和Array.reverse有什么区别?
我提出的问题涉及向现有的字符串构造函数添加新方法的应用程序。在Stoyan Stefanov编写的面向对象的Javascript程序中,有一个使用数组构造函数的.reverse()方法为字符串构造函数创建一个的示例。以下是示例:Javascript中的Array.prototype.reverse和Array.reverse有什么区别?,javascript,oop,object,prototype,Javascript,Oop,Object,Prototype,我提出的问题涉及向现有的字符串构造函数添加新方法的应用程序。在Stoyan Stefanov编写的面向对象的Javascript程序中,有一个使用数组构造函数的.reverse()方法为字符串构造函数创建一个的示例。以下是示例: String.prototype.reverse = function() { return Array.prototype.reverse.apply(this.split('')).join(''); } 我认为数组的.reverse()方法直接属于数组
String.prototype.reverse = function() {
return Array.prototype.reverse.apply(this.split('')).join('');
}
我认为数组的.reverse()方法直接属于数组的对象。事实上,当我尝试使用以下语句执行第二位代码时:
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”。这对我来说毫无意义
当然,如果我在.prototype中加回去,它工作得非常好
另外,如果我必须调用prototype从数组对象访问.reverse()方法,,那么对于Javascript中的任何内置对象,我必须这样做吗
提前谢谢你的帮助
Array.reverse
未定义(至少在Chrome 29中)Array.prototype.reverse
是一个函数,它将反转调用它的“iterable”的顺序
这里需要注意的关键点是,Array
不像Java中那样是一个类,而是一个构造函数:
Array
的prototype
属性实际上是为Array
的任何特定实例提供行为的原因:
Object.getPrototypeOf([]) === Array.prototype;
// true
// Bad idea, just for an example
var guys = ['Tom', 'Harry', 'Richard'];
Array.prototype.exclaim = function() {
return this.join(", ") + "?!?!?!";
};
guys.exclaim();
// Tom, Harry, Richard?!?!?!
这里的关键是JavaScript使用基于原型的面向对象模式,而不是您可能更熟悉的经典模式。JavaScript没有包含所有行为但与实例不同的“类”,而是有对象,可以是其他对象的“原型”,为子对象提供数据和行为
// Totally licit OO pattern in JavaScript
var prototypeClass = {
method1: function() { console.log("Hello from method 1!"); },
method2: function() { console.log("Hello from method 2!"); },
classData: 42
};
var prototypeInstance = Object.create(prototypeClass);
prototypeInstance.method1() // Hello from method 1!
prototypeInstance.classData // 42
// And you can modify the class after
// instantiating instances and the changes
// will be picked up by the instances
prototypeClass.happyPrimes = "Don't they teach recreational mathematics anymore?";
prototypeInstance.happyPrimes // The quote from 42
在这种情况下,我必须调用prototype从数组对象访问.reverse()方法吗
不需要。要访问对象上的方法,只需使用点表示法即可。你想做的只是
return this.split('').reverse().join('');
这正是(或调用
)所做的:
最后是arr.reverse===Array.prototype.reverse
,因为这是Array
对象继承的地方。您不能访问数组
构造函数对象本身上的reverse
方法,而是要访问所有数组
实例通过其原型共享的属性。然而,您几乎不需要显式地使用prototype对象,这只是在处理非数组
实例(不共享原型)的对象时,如参数
对象或节点列表
s
TypeError:调用函数数组时缺少参数0。请反转。这对我来说毫无意义
Array.reverse
是一种非标准配置,仅在Firefox中提供。它的目的是简化在其他对象上应用数组原型方法的构造,并将类似数组的对象作为其第一个参数。例如:
Array.reverse([0, 1]) // [1, 0]
这相当于
Array.prototype.reverse.apply([0, 1]);
Array.prototype.reverse.apply(undefined)
然而,你在做什么
Array.reverse.apply([…]/*, undefined*/)
调用Array.reverse
函数,其中数组用于(不相关)且没有实际参数,相当于
Array.prototype.reverse.apply([0, 1]);
Array.prototype.reverse.apply(undefined)
这将抛出一个合理的异常。这可能与您如何设置阵列有关。这应该是可行的:返回这个.split(“”).reverse().join(“”)代码>为什么您希望这样做有效?您需要了解什么是原型
。非常有趣的是,Array.reverse===Array.prototype.reverse
在Firefox中是false
。我不太明白为什么Array.reverse存在于Firefox中。@takteek:当然是为了简化对数组函数的应用:而不是使用Array.prototypye.xxxx.apply(…)您可以执行Array.xxx.apply(…)。(电话也一样)。但由于它不是标准的,这是一个相当无用的努力。。。