callback demo code in javascript

<html>
<head>
    <title>test_cb</title>
    <script language="javascript">

        var sp_logic = {
            mediator_: null,

            bind: function (mediator) {
                this.mediator_ = mediator;
            },
            notify_dev_online: function (dev_id) {
                var cb = this.mediator_.notify_dev_online;
                this.apply(cb, { id :dev_id });

            },
            apply: function (fun, param, defaultValue) {
                if ((typeof fun) == "function" && null != fun) {
                    return fun.apply(this.mediator_, param ? [param] : []);
                }
                return defaultValue;
            }
        };

        var mediator = {
            sp_logic_: null,
            init: function (sp_logic) {
                sp_logic.bind(this);
                this.sp_logic_ = sp_logic;
            },
            notify_dev_online: function (dev_id) {
                alert(dev_id);
            }
        };

        function test_onclick() {
            mediator.init(sp_logic);
            sp_logic.notify_dev_online(1);
        }
    </script>
</head>
<body>
    <input id="test" type="button" value="test" name="test" οnclick="javascript:test_onclick()" />
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值