简体   繁体   English

为什么我需要从另一个来源指向Array构造函数,为什么我不能直接在数组对象上调用toString()?

[英]Why do I ever need to point the Array constructor from another source and why can't I call toString() directly on array objects?

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);

var _Array = window.frames[window.frames.length-1].Array;

var arr = new _Array(1,2,3);

typeof arr; //'object'
arr instanceof Array; //false

I have 3 question about this code: 我对此代码有3个问题:

  1. Object.prototype appears on the proto chain of both _Array and Array prototypes but Array and _Array are not in the same chain and because of that (arr instanceof Array) returns false? Object.prototype出现在_Array和Array原型的原型链上,但是Array和_Array不在同一链中,因此(Array的instance较旧)返回false?

  2. I want to understand what's exactly going here, these are my thoughts: _Array points to the same constructor as where "windows.iframe[...].Array" points, then we create an instance of _Array, its proto points to Array.prototype, and Array.prototype.proto points to Object.prototype. 我想了解这里到底发生了什么,这是我的想法:_Array指向与“ windows.iframe [...]。Array”所指向的构造函数相同的构造函数,然后我们创建_Array的实例,其原型指向Array。原型,并且Array.prototype.proto指向Object.prototype。 So, both _Array and Array are constructors of array objects, which are inherited from Object.prototype. 因此,_Array和Array都是数组对象的构造函数,它们是从Object.prototype继承的。 So, why do I need to call toString() directly from Object.prototype in order to get "[object Array]" on _Array and Array objects? 因此,为什么我需要直接从Object.prototype调用toString()才能在_Array和Array对象上获取“ [object Array]”? When I call it like this: arr.toString() I get an empty string and when I call Object.prototype.toString.call(arr) I get "[object Array]". 当我这样调用时:arr.toString()我得到一个空字符串,当我调用Object.prototype.toString.call(arr)时得到“ [object Array]”。 Why is that? 这是为什么?

  3. Can someone explain please why do I ever need to point to the Array constructor from another source? 有人可以解释一下为什么我需要从另一个来源指向Array构造函数吗? in this code _Array points to the same constructor as where "windows.iframe[...].Array" points to. 在此代码中,_Array指向与“ windows.iframe [...]。Array”所指向的构造函数相同的构造函数。 Why is it useful? 为什么有用?

An Array is just a data structure with behavior; Array 只是具有行为的数据结构。 there are many array implementations, and you can implement your own as well. 有很多数组实现,您也可以实现自己的数组。

  1. I want to understand what's exactly going here... 我想了解这里到底发生了什么...
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);

// Accessing a new type of Array
var _Array =
  window.frames[window.frames.length-1].Array;

// Instantiates new array of type "_Array" (declared in iframe)
var arr = new _Array(1,2,3); // [1,2,3]

// Shows the type of arr variable
typeof arr; //-> 'object'

// Asserting that the "arr" instance is not in an instance of type "Array" (native Javascript type), rather it is of the type declared in the iframe
arr instanceof Array; //-> false
  1. (...) Why is it useful? (...)为什么有用?

Please note that this code pretty useless on its own (it is an example snippet), its probable goal is to show 请注意,此代码本身无用(这是一个示例片段),其可能的目标是显示

  • basic type-related operations in Javascript JavaScript中与类型相关的基本操作
  • possibility for iframes to hold data, such as new types ( Array ) iframe保留数据的可能性,例如新类型( Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM