繁体   English   中英

即使使用空检查也未定义

[英]Undefined even with null check

listener 返回 undefined,我相信 start 属性。 这是一个观察者对象。

    var updateP = {
        cb: function (event, properties) {
            "listener" in window? listener.next(properties):null
        },
        start: function (listener) {
            dataset.on("update", this.cb)
        },
        stop: function () {
            dataset.off("update", this.cb)
        },
    }

listener变量是start函数的本地变量,因此您无法将其作为全局变量访问。

this.cb的定义移动到updateP.start ,然后它就可以访问词法变量了。

 var updateP = { start: function(listener) { function asdf(ab) { console.log(ab) } this.cb = function(event, properties) { listener.next(properties) }; dataset.on("update", this.cb) }, stop: function() { if (this.cb) { dataset.off("update", this.cb); } }, }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM