JavaScript DHTML/Mochkit/instanceof

Материал из Web эксперт
Версия от 07:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

instanceof operator

 
<!--
MochiKit is dual-licensed software.  It is available under the terms of the
MIT License, or the Academic Free License version 2.1.  The full text of
each license is included below.
-->
<!-- Code revised from MochiKit demo code -->
<html>
<head>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Base.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Iter.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/DOM.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Style.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Logging.js"></script>
    <script type="text/javascript" src="MochiKit-1.4.2/lib/MochiKit/Iter.js"></script>
</head>
<body>
<pre id="test">
<script type="text/javascript">
    var O = function (value) {
        bindMethods(this);
        this.value = value;
    };
    O.prototype.func = function () {
        return this.value;
    };
    var o = new O("boring");
    var p = {};
    p.func = o.func;
    var func = o.func;
    alert( o.func(), "boring", "bindMethods doesn"t break shit" );
    alert( p.func(), "boring", "bindMethods works on other objects" );
    alert( func(), "boring", "bindMethods works on functions" );
    var p = clone(o);
    alert( p instanceof O, "cloned correct inheritance" );
    var q = clone(p);
    alert( q instanceof O, "clone-cloned correct inheritance" );
    q.foo = "bar";
    alert( p.foo, undefined, "clone-clone is copy-on-write" );
    p.bar = "foo";
    alert( o.bar, undefined, "clone is copy-on-write" );
    alert( q.bar, "foo", "clone-clone has proper delegation" );
    // unbind
    p.func = bind(p.func, null);
    alert( p.func(), "boring", "clone function calls correct" );
    q.value = "awesome";
    alert( q.func(), "awesome", "clone really does work" );
</script>
</pre>
</body>
</html>