Search code examples
canjscanjs-plugin

How to include/import can.Object of canjs, so that can.Object.same would be available to use?


I cannot make following example work at canjs website: http://canjs.com/docs/can.Object.same.html#section_Examples

The following page loaded in browser will get an error: '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>

And I also tried using requirejs to use canjs:

var can = requirejs("can");

But it's the same thing, can.Object is still 'undefined'. I want to use can.Object.same because of to check if two objects are equal at value level. I didn't use underscorejs, otherwise I can use _.isEqual.

====In addition to the answer by Sebastian, if using requirejs, add "can/util/object" into the list in require([...]).====


Solution

  • You need to load can.Object as an extra script:

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

    It needs to be loaded after Can.