- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过将对象的原型(prototype)方法设置为数组方法,对象的行为就像对象和数组的混合体。下面是一个简单的例子:
function Foo() {}
Foo.prototype.push = Array.prototype.push;
Foo.prototype.forEach = Array.prototype.forEach;
var foo = new Foo();
foo.push('abc');
foo.length; // = 1 as expected. But wait, why isn't foo.length undefined? How/when did this property get attached to foo?
foo[1] = 'def';
foo.length; // still = 1. But foo={0:'abc',1:'def'}, Why not =2?
foo.forEach(function(item) {
console.log(item)
}); //shows only'abc' and not 'def'
foo.push('ghi');
foo.length; // = 2, and now foo = {0:'abc', 1:'ghi'}. So it overwrote the key=1, which means its accessing the same location, but the first approach did not change the length ( didn't become a part of the array ) why ?
foo.forEach(function(item) {
console.log(item)
}); //now shows 'abc' and 'ghi'
为什么会发生所有这些奇怪的行为,为什么模仿这样的数组不好?
最佳答案
length
属性是如何设置的?当您调用 Array#push
时,或者在本例中,调用 Foo#push
方法。根据 ECMAScript 2015 Specification :
22.1.3.17 Array.prototype.push ( ...items )
[...]
- [...]
[...]
d. Let len be len+1
- Let setStatus be Set(O,
"length"
, len, true).
因此,当您调用该函数时,它会自动将 length 属性设置为值 len
(如果它不存在),该值会在您每次推送时递增。
现在,当您直接在数组中设置索引时,长度属性不会更新。那是因为 JavaScript 中的数组是 exotic objects当您直接设置属性时,它会在内部增加长度。再次按照规范:
9.4.2 Array Exotic Objects
[...]
whenever an own property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever an own property is added whose name is an array index, the value of the
length
property is changed, if necessary, to be one more than the numeric value of that array index;
根据定义,奇异对象是覆盖所有普通对象所具有的内部方法的任何对象。在这种情况下,数组外来对象会覆盖内部 [[DefineOwnProperty]]
方法,因此当您定义数组的属性(例如设置索引)时,它需要额外的步骤来确保诸如属性 length
已更新。您的 Foo
构造函数不会创建奇异对象,因此它不会像数组那样覆盖 [[DefineOwnProperty]]
内部方法——因此不会更新 length
当您直接在索引处定义一个值时。
push
会推送到上一个索引?由于 Foo
不是奇异对象,因此当使用 foo[1] = 'def 直接添加元素时,不会自动增加
,当您第二次尝试推送时,length
'length
仍为 1。如果我们再次查看 Array#push
:
22.1.3.17 Array.prototype.push ( ...items )
[...]
[...]
- Let items be a List whose elements are, in left to right order, the arguments that were passed to this function invocation.
[...]
因此,由于 foo[1] = 'def'
没有修改 length
属性,因此您的数组长度仍为 1,因此它将设置新的 to-be - 将索引 1 处的元素压入,因为长度为 1。
同样的原则适用于Array#forEach
. forEach
依赖于 length
遍历数组。由于当您执行 foo[1] = 'def'
时您的长度没有被修改并保持为 1,因此 forEach
仅从索引 [0, 1) 迭代导致它仅记录第一个元素。推送更新长度并使其从 [0, 2) 迭代并记录两个元素。
这是因为数组是奇特的对象。它们与常规对象相处不来,因为常规对象根本无法实现与奇异对象相同的行为。根据定义,奇异对象覆盖内部方法的默认行为以实现功能所需的某些行为。在这种情况下,数组必须特别处理设置索引和管理长度——使用内部方法 [[DefineOwnProperty]]
完成。对于常规对象,它不会覆盖 [[DefineOwnProperty]]
因此许多基本操作无法正常工作 - 因此您不应该这样做。
不过,您可以使用奇异对象,例如 Proxy
对象来为 [[DefineOwnProperty]]
实现您自己的代码,例如模拟行为的数组。另一种方式,如loganfsmyth提到,你可以使用 ES2015 类来 extend and subclass builtin exotic object constructors从而正确模仿数组行为。
关于javascript - 为什么这个类似数组的对象会有这样的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815940/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!