关于JS中的Plain Object
文章目录
Plain Object:纯粹的对象(通过 "{}" 或者 "new Object" 创建的)
将简单对象转为数组?
比如转类数组的对象arguments可以用Array.prototype.slice.call(arguments);
可以转数组的对象,必须符合两个条件:
1,对象的元素索引使用数字。
2,对象必须有length属性。
JS代码
var obj = {};
obj[0] = 1;
obj[1] = 2;
obj.length = 2;
alert(Array.prototype.slice.call(obj));
关于React中Redux里面的 isPlainObject 的判断方法:
export default function isPlainObject(obj) {
if (typeof obj !== 'object' || obj === null) return false
let proto = obj
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
return Object.getPrototypeOf(obj) === proto }
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论