如何比较两个不同的javascript对象的相同属性(不比较实例函数)

发布于 2024-12-08 10:00:05 字数 1226 浏览 0 评论 0原文

如何将对象任务内的树结构与 obj_after 树进行比较,而不必实际比较树中的各个节点。 (一个是类的实例,即具有实例方法,另一个没有函数,只有数据)。

请阅读下面的内容以了解我的确切问题(上面的内容不太清楚)。

我正在编写一个基于 js 对象树构建 GUI 的脚本。我正在为此使用咖啡脚本。

我想创建一个接受 json 的类,并构建树。

window.GuiTree = class GuiTree
    constructor:(json_GUI_tree)->

但我遇到的问题是测试这个。我想向此类添加方法,例如“add_node”。这会向 GuiTree 添加一个新节点。

所以我在茉莉花中尝试这个。

obj_before =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   


obj_after =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   
        5:
          type:"text" 
          name:"subtask"
          label:"Subtask"

我已将任务 5 添加到 Gui 树中

,我想像这样测试它

task = new GuiTree(obj_before)
expect(task.add(3, node_5)).givesNewTree(obj_after)

匹配器“givesNewTree”是一个自定义匹配器。

问题就出在这里!!如何将对象任务内的树结构与 obj_after 树进行比较,而不必实际比较树中的各个节点。 (其中之一)

对我来说,看起来需要编写很多容易出错的代码来比较树。所以我可能需要编写测试来测试。有没有更聪明的方法。

How do I compare the tree structure inside an object task with the obj_after tree, without having to actually compare individual nodes in the tree. (one is an instance of a class i.e. with instance methods and the other has no functions just data).

Please read below to understand my exact problem (not so clear from the above).

I am writing a script that builds a GUI based on a js object tree. I am using coffeescript for this.

I would like to create a class that takes a json, and build the tree.

window.GuiTree = class GuiTree
    constructor:(json_GUI_tree)->

But the problem I am having is in testing this. I would like to add methods to this class like 'add_node'. That adds a new node to the GuiTree.

So I try this in Jasmine.

obj_before =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   


obj_after =
  1:
    type:"text" 
    name:"task"
    label:"Task"
  2:
    type:"subsection"
    sections:
      3:
        4:
          type:"text" 
          name:"subtask"
          label:"Subtask"   
        5:
          type:"text" 
          name:"subtask"
          label:"Subtask"

Where I have added the task 5 to the Gui tree

And I would like to test it like this

task = new GuiTree(obj_before)
expect(task.add(3, node_5)).givesNewTree(obj_after)

The matcher 'givesNewTree' is a custom matcher.

The problem is here !! how do I compare the tree structure inside the object task with the obj_after tree, without having to actually compare individual nodes in the tree. (one is )

To me it looks like lots of error prone code to write to compare the trees. So I may need to write tests to test. Is there a smarter way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-12-15 10:00:05

您正在寻找的是深度等效的实现。它们不一定容易出错,事实上有几种已知的实现,最著名的是 QUnit 的 deepEqual实施。 这是另一个。当然这是一个相当复杂的算法。

当然,您还可以在 GuiTree 类中定义一个 .equals (或类似名称的)方法,该方法根据您已经知道的重要内容以更简单的方式比较两个实例它们,但我会首先尝试现有的深度相等算法,因为您可能能够导入您已经知道是正确的以前编写的代码。

一种黑客的替代方法是对两个对象执行 JSON.stringify() 并期望生成的 JSON 编码字符串相等,但我怀疑这可能会导致循环的各种问题参考资料等,但它可能会帮助您入门。

What you're looking for is an implementation of deep equivalence. They're not necessarily error prone, in fact there are several known implementation, the most well known is QUnit's deepEqual implementation. Here's another. Granted it's a quite complicated algorithm.

Your could of course also define a .equals (or similarly named) method in your GuiTree class that compares two instances in a simpler way based on what you already know is significant about them, but I would try existing deep equal algorithms first since you might be able to import previously written code that you already know is correct.

A hackish alternative is to do JSON.stringify() on both objects and expect the resulting JSON encoded strings to be equal, but I suspect that could lead to various problems with circular references and the like, but it might get you started.

赏烟花じ飞满天 2024-12-15 10:00:05

就在今天,Underscore.js 1.2.0 发布了,带有新的 _。 isEqual 可以处理循环的函数。 Underscore 是一个健壮、成熟且相当小的库,可以在客户端和服务器端 JavaScript 中运行。

Just today, Underscore.js 1.2.0 came out, with a new _.isEqual function that can handle cycles. Underscore is a robust, mature, and reasonably small library that works in both client-side and server-side JavaScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文