这些物体相等吗?

发布于 2024-12-09 10:45:15 字数 900 浏览 0 评论 0原文

var Widget = new Class({
    Implements: Options,
    options: {
        color: '#fff',
        size: {
            width: 100,
            height: 100
        }
    },
    initialize: function(options){
        this.setOptions(options);
    }
});

var myWidget = new Widget({
    color: '#f00',
    size: {
        width: 200
    }
});

//myWidget.options is now: {color: #f00, size: {width: 200, height: 100}}

// Deep copy example
var mySize = {
    width: 50,
    height: 50
};

var myWidget = new Widget({
    size: mySize
});

(mySize == myWidget.options.size) // false! mySize was copied in the setOptions call.

来自此处

myWidget.options.size 也应该是

 {
        width: 50,
        height: 50
    };

为什么 (mySize == myWidget .options.size) // 错误! ?

var Widget = new Class({
    Implements: Options,
    options: {
        color: '#fff',
        size: {
            width: 100,
            height: 100
        }
    },
    initialize: function(options){
        this.setOptions(options);
    }
});

var myWidget = new Widget({
    color: '#f00',
    size: {
        width: 200
    }
});

//myWidget.options is now: {color: #f00, size: {width: 200, height: 100}}

// Deep copy example
var mySize = {
    width: 50,
    height: 50
};

var myWidget = new Widget({
    size: mySize
});

(mySize == myWidget.options.size) // false! mySize was copied in the setOptions call.

from here

myWidget.options.size should be also

 {
        width: 50,
        height: 50
    };

Why is (mySize == myWidget.options.size) // false! ?

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

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

发布评论

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

评论(4

别闹i 2024-12-16 10:45:15

不是很明显,但您从示例中复制的注释告诉您原因。

// false! mySize was copied in the setOptions call.

对象上的等于是实例上的。 setOptions 复制 mySize。它在初始化时运行。

initialize: function(options){
    this.setOptions(options);
}

除非你问的是我错过的更复杂的问题。

Not to be obvious, but the comment that you copied from the example tells you why.

// false! mySize was copied in the setOptions call.

Equals on an object is to the instance. setOptions copies the mySize. It runs when it is initialized.

initialize: function(options){
    this.setOptions(options);
}

Unless you are asking a more complex question that I missed.

渔村楼浪 2024-12-16 10:45:15

类似的东西是行不通的,因为它不检查对象的值,而是检查对象的身份:

({width:1, height:1}) == ({width:1, height:1})

所以如果你想比较两个对象,你必须编写类似的东西

obj1.width == obj2.width && obj1.height == obj2.height;

Something like that won't work because it doesn't checks the values of the objects, but the identity of the objects:

({width:1, height:1}) == ({width:1, height:1})

So if you want to compare two object you have to write something like that

obj1.width == obj2.width && obj1.height == obj2.height;
╰ゝ天使的微笑 2024-12-16 10:45:15

正如 @styrr 所说,比较 2 个对象是没有意义的,但您可以比较对象的序列化表示:

console.log(JSON.encode(mySize) === JSON.encode(myWidget.options.size)); // true 

虽然您最好比较单个值,但它更便宜。

另外,请查看Object.clone()以取消引用。默认情况下,对象通过引用传递,但在选项的情况下,它们被取消引用。 IE 如果您更改 mySize.width,它不会更改您通常会得到的 instance.options.size.width

comparing 2 objects is meaningless as stated by @styrr but you can compare the serialised representation of the objects instead:

console.log(JSON.encode(mySize) === JSON.encode(myWidget.options.size)); // true 

though you are better off comparing individual values, it's cheaper.

Also, look into Object.clone() to dereference. By default, objects are passed by reference but in the case of options, they are dereferenced. I.E. if you change mySize.width, it won't change instance.options.size.width, which you would get normally.

泡沫很甜 2024-12-16 10:45:15

使用 underscorejs.js isEqual 比较 JSON 对象 http://underscorejs.org/#isEqual

Use underscorejs.js isEqual to compare JSON objects http://underscorejs.org/#isEqual

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