我根据这个 article 创建了一个集合。我发现它可以在 chrome、firefox 和 ie9+ 上运行。 我认为有些事情不会起作用,或者 Windows 10 的 ie9 模拟器无法给我真实的图片。
此扩展可能会出现什么问题?
测试代码:
<script>
function Collection() {
var collection = Object.create( Array.prototype );
collection = (Array.apply( collection, arguments ) || collection);
return collection;
}
Collection.prototype = Object.create(Array.prototype);
var collection = new Collection();
collection[0] = 1;
console.log(collection[0]);
</script>
最佳答案
看看http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ .
特别是,
var collection = Object.create(Array.prototype);
创建一个继承自Array.prototype
而不是Collection.prototype
的对象。它创建一个对象,而不是一个数组。
collection = (Array.apply( collection, arguments ) …
Array
忽略其 this
值。并创建一个 Array
实例,而不是 Collection
实例。
… || collection)
这是毫无意义的,因为 Array
永远不会返回虚假值,因此 collection
总是被忽略。
What could go wrong with this extension?
它创建数组
,而不是集合
。根本没有扩展。尝试将一些方法放在 Collection.prototype
中并调用它们。
关于javascript - 创建扩展 native 数组的集合有哪些危险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43140746/