0

我无法在 canjs 网站上制作以下示例:http: //canjs.com/docs/can.Object.same.html#section_Examples

浏览器中加载的以下页面将出现错误:'Uncaught TypeError: Cannot read property 'same' of undefined'

<!DOCTYPE>
<html>
<head>
<title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <script src="http://canjs.com/release/latest/can.jquery.js"></script>
</head>
<body>
<script>

    can.Object.same({ name: "Justin" }, { name: "JUSTIN" }) //-> false
    // ignore the name property
    can.Object.same({ name: "Brian" }, { name: "JUSTIN" }, { name: null }) //-> true
    // ignore case
    can.Object.same({ name: "Justin" }, { name: "JUSTIN" }, { name: "i" }) //-> true
    // deep rule
    can.Object.same({ person: { name: "Justin"} },
    { person: { name: "JUSTIN"} },
    { person: { name: "i"} }) //-> true
    // supplied compare function
    can.Object.same({ age: "Thirty" },
    { age: 30 },
    { age: function (a, b) {
        if (a == "Thirty") {
            a = 30
        }
        if (b == "Thirty") {
            b = 30
        }
        return a === b;
    } 
    })      //-> true
</script>
</body>
</html>

而且我还尝试使用requirejs来使用canjs:

var can = requirejs("can");

但它是同一件事,can.Object 仍然是“未定义”。我想使用 can.Object.same 因为要检查两个对象在值级别是否相等。我没有使用underscorejs,否则我可以使用_.isEqual。

====除了Sebastian的回答,如果使用requirejs,在require([...])的列表中添加“can/util/object”。====

4

1 回答 1

1

您需要将 can.Object 作为额外脚本加载:

http://canjs.com/release/latest/can.object.js

需要在Can之后加载。

于 2014-06-20T05:42:36.913 回答