当前位置 : 主页 > 网页制作 > Dojo >

dojo框架用hitch实现函数与上下文的绑定

来源:互联网 收集:自由互联 发布时间:2021-06-15
dojo框架:http://dojotoolkit.org/ !DOCTYPEhtmlhtmlheadmetacharset="UTF-8"titledojo框架绑定函数与上下文/titlescriptsrc="dojo/dojo/dojo.js"/script/headbodyscriptrequire(["dojo/_base/lang"],function(lang){vartheAccumulator={total:

dojo框架:http://dojotoolkit.org/


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>dojo框架绑定函数与上下文</title>
    <script  src = "dojo/dojo/dojo.js"  ></script>
</head>
<body>

<script>

    require(["dojo/_base/lang"], function(lang){
        var theAccumulator = {
            total: 0,
            clear: function () {
            },
            clear: function () {
                this.total = 0;
            },
            add: function (x) {
                this.total += x;
            },
            getResult: function () {
                return this.total;
            }
        };
        theAccumulator.clear();
        theAccumulator.add(1);
        theAccumulator.add(2);

        function printResult(func) {
            console.log(func());
        }
        printResult(lang.hitch(theAccumulator , "getResult")) ;//输出3
    } );


</script>
</body>
</html>
网友评论