We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
var obj = { '2': 3, '3': 4, 'length': 2, 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1) obj.push(2) console.log(obj)
考点: Array-Like Object
答案:
题目来源
在执行 push 操作之前,先来看看 obj 是什么情况:
var obj = { '2': 3, '3': 4, 'length': 2, 'splice': Array.prototype.splice, 'push': Array.prototype.push } console.log(obj); /* * Object(2) [empty × 2, 2: 3, 3: 4, splice: ƒ, push: ƒ] * 2: 3 * 3: 4 * length: 2 * push: ƒ push() * splice: ƒ splice() * __proto__: Object */ console.log(obj.length); // 2 console.log(obj[0], obj[1], obj[2], obj[3]); // undefined undefined 3 4
push 方法根据 length 属性来决定从哪里开始插入给定的值,并且改变数组长度,当 length 不存在时,会自动创建 length。
类数组对象特点是有 length 属性且可以用索引访问,但没有数组方法,所以执行 push 操作之前的 obj 是个类数组,从 obj 的各种打印结果也能看出。obj 前两位都是空值,length 为 2,push 会从 obj[2] 开始插入给定值。
Q:为什么 push 可以被用在 Object 上? A:push 是特意设计为通用的,Array.prototype.push 可以用在对象上。当将该集合存储在对象本身上,如题这样,使用 Array.prototype.push.call(obj) ,obj 会被当成在数组上执行 push 操作。
Array.prototype.push
Array.prototype.push.call(obj)
题中
var obj = { ..., 'push': Array.prototype.push } obj.push(1)
也可以写成
var obj = { ... } Array.prototype.push.call(obj, 1); Array.prototype.push.call(obj, 2);
输出结果是一样的。
Q:为什么对象添加了splice属性后并没有调用就会变成类数组对象?这个问题比较无聊🙄️ A:据大佬分析,是 chrome 的 Devtools 做了优化,还扒出了代码:
/** * @param {?Object} obj * @return {boolean} */ function isArrayLike(obj) { if (!obj || typeof obj !== 'object') return false; try { if (typeof obj.splice === 'function') { const len = obj.length; return typeof len === 'number' && (len >>> 0 === len && (len > 0 || 1 / len > 0)); } } catch (e) { } return false; }
判断的过程:
var obj = { '2': 3, '3': 4, 'length': 6 } Array.prototype.push.call(obj, 1); console.log(obj);
答案:{2: 3, 3: 4, 6: 1, length: 7}
Array.prototype.forEach.call(arrayLikeObject, (item, index) => console.log(`arrayLikeObject[${index}]:`, item)); for (let i = 0; i < arrayLikeObject.length; i++) { console.log(`arrayLikeObject[${i}]:`, arrayLikeObject[i]); }
Array.from(arrayLikeObject)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
考点: Array-Like Object
答案:
题目来源
解析
在执行 push 操作之前,先来看看 obj 是什么情况:
push 方法根据 length 属性来决定从哪里开始插入给定的值,并且改变数组长度,当 length 不存在时,会自动创建 length。
类数组对象特点是有 length 属性且可以用索引访问,但没有数组方法,所以执行 push 操作之前的 obj 是个类数组,从 obj 的各种打印结果也能看出。obj 前两位都是空值,length 为 2,push 会从 obj[2] 开始插入给定值。
其他问题
Q:为什么 push 可以被用在 Object 上?
A:push 是特意设计为通用的,
Array.prototype.push
可以用在对象上。当将该集合存储在对象本身上,如题这样,使用Array.prototype.push.call(obj)
,obj 会被当成在数组上执行 push 操作。题中
也可以写成
输出结果是一样的。
Q:为什么对象添加了splice属性后并没有调用就会变成类数组对象?这个问题比较无聊🙄️
A:据大佬分析,是 chrome 的 Devtools 做了优化,还扒出了代码:
判断的过程:
延伸
答案:{2: 3, 3: 4, 6: 1, length: 7}
本篇参考
The text was updated successfully, but these errors were encountered: