码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • object property order


    起因

    使用 prosemirror 时,想要取 schema 实例设置的 nodes

    const mySchema = new Schema({
      nodes: {
        doc: {},
        customBlock: {},
        text: {},
      },
      marks: {},
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在浏览器控制台查看 mySchema,发现两个地方有nodes:

    • mySchema.nodes 是普通对象,有三个属性,就是我想要获取的 nodes。但是我设置时顺序是 doc - customBlock - text,而控制台显示的顺序是 customBlock - doc -text,不一致
    • mySchema.spec.nodes 是一个 OrderedMap 对象

    好奇用意所以研究了一下

    object property order

    es5: 不规定属性顺序

    es2015: 规定对象属性顺序,但不要求老操作符遵守

    While ES6 / ES2015 adds property order, it does not require for-in, Object.keys, or JSON.stringify to follow that order, due to legacy compatibility concerns.

    Other operations, like Object.getOwnPropertyNames, Object.getOwnPropertySymbols, Object.defineProperties, and Reflect.ownKeys do follow the following order for ordinary objects:

    • Integer indices (if applicable), in ascending order.
    • Other string keys (if applicable), in property creation order.
    • Symbol keys (if applicable), in property creation order.

    es2020: 要求老操作符也要遵守这个顺序

    As of ES2020, even older operations like for-in and Object.keys are required to follow property order. That doesn’t change the fact that using property order for fundamental program logic probably isn’t a good idea, since the order for non-integer-index properties depends on when the properties were created.

    按照最新规定,对象属性是有顺序的,但并不保证是插入顺序!而且你无法得知用户使用的浏览器是否遵守最新规定。
    所以当顺序很重要的时候,就不要用 object!可以用数组或者 Map(Map 会记住 key 的插入顺序)


    在浏览器控制台中查看对象

    var obj = {
        b:'',
        a:''
    }
    
    • 1
    • 2
    • 3
    • 4

    打印obj,显示的顺序正确:

    在这里插入图片描述

    但是点击箭头查看详情时,会看到详情里展示顺序变了:

    在这里插入图片描述
    应该是浏览器为了开发者查看详情方便,展示的时候重新排序了。

    多加几个属性测试一下,也是如此:

    在这里插入图片描述

    结论:在浏览器控制台中,点击按钮查看对象详情时,显示的并不是对象属性的真正顺序!
    (以前没想过这个问题,觉得控制台里展现的就是数据最真实的、原始的样子。发现了个盲区,好玩,但也要警惕)


    说回 prosemirror

    在 prosemirror scheam 中,nodes 的先后顺序很重要(顺序控制节点的优先级)

    不知道出于什么原因,没有用 array 或者 Map 存储数据,而用了 object。这时为了保证顺序,就用 orderedMap了


    参考:stackoverflow - Does ES6 introduce a well-defined order of enumeration for object properties?

  • 相关阅读:
    100行代码教你写个卡牌翻翻乐小游戏
    debian设置允许ssh连接
    基于京东micro-app微前端框架的项目实践
    第二章 第二十四节:文件操作:写
    MYSQL全文索引及Match() against()踩坑记录-超详细超实用
    如何用WinRAR给压缩包设置和取消密码
    java-net-php-python-ssm巴音学院本科部校园网站计算机毕业设计程序
    国内项目管理中级证书CSPM-3正在报名!
    服务熔断 Hystrix
    我要写整个中文互联网界最牛逼的JVM系列教程 | 「类加载子系统」章节:ClassLoader的常用方法及其获取方法
  • 原文地址:https://blog.csdn.net/tangran0526/article/details/133772755
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号