在Javascript中,Array.join真的是Array.prototype.join吗? [英] In Javascript, is Array.join really Array.prototype.join?

查看:88
本文介绍了在Javascript中,Array.join真的是Array.prototype.join吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么即使在Javascript权威指南第6版,O'Reilly, p。 149 - 150 ,它继续使用 Array.join() Array.concat()。它应该是 Array.prototype.join()吗?



但是,虽然Chrome没有定义 Array.join ,但Firefox确实可以,并且可以通过 Array.join(array,string)。差异可能很重要,因为一个是类方法,另一个是实例方法。我不知道这是为什么?



即使在核心Javascript参考文档中,本书也继续使用 Array.join ,但这可能意味着 Array.prototype.join ,而且,它似乎暗示数组的长度 属性,但它实际上也应该是 Array.prototype 的属性,并且是 Array.prototype.length



顺便说一下,似乎Firefox的 Array.prototype.join 的实现可以

  Array.prototype.join = function(s){
return Array.join(this,s);
}

但我不认为通常会这样做(定义一个类方法,可以在实例上调用。

解决方案

FireFox将原型方法的版本放在相关的构造函数。



这不是ECMAScript标准的一部分,而是ECMAScript的特定JavaScript超集的一部分。



<这本书应该有所区别,除非本书专门讨论JavaScript扩展。



请记住JavaScript!== ECMAScript。 ECMAScript是语言标准,JavaScript是该标准第3版的实现,包括标准未指定的功能超集。


I wonder why even in the Javascript Definitive Guide 6th ed, O'Reilly, p. 149 - 150, it keeps on using Array.join() or Array.concat(). Should it be Array.prototype.join() instead?

But while Chrome doesn't have Array.join defined, Firefox actually does, and it can be invoked by Array.join(array, string). The difference might be important as one is a class method and the other is an instance method. I wonder why that is?

The book kept on using Array.join even in the Core Javascript Reference docs, but maybe it meant Array.prototype.join, and also, it seems to suggest that Array has a length property, but it really should also be a property of Array.prototype, and is Array.prototype.length?

By the way, it seems that Firefox's implementation of Array.prototype.join can be

Array.prototype.join = function(s) {
    return Array.join(this, s);
}

but I don't see that usually being done (defining a class method that can be invoked on an instance).

解决方案

FireFox puts a version of prototyped methods on the related constructor.

This isn't part of the ECMAScript standard, but rather is part of the specific JavaScript superset of ECMAScript.

The book should make that distinction, unless the book is talking specifically about the JavaScript extensions.

Keep in mind that JavaScript !== ECMAScript. ECMAScript is the language standard, and JavaScript is an implementation of the 3rd edition of that standard, and includes a superset of functionality not specified by the standard.

这篇关于在Javascript中,Array.join真的是Array.prototype.join吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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