1.0.1 • Published 4 years ago

@ziki/handler v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago
@ziki/handler
描述
js 回调时 this 指向改变的解决方案
describe
The solution of this change when JS callback

not use @ziki/handler
不使用 @ziki/handler
function A(func) {
    this.name = 'A'
    this.hi = function() {
        func()
    }
}

function Main() {
    this.name = 'Main'
    this.hi = function() {
        console.log('main->', this.name)
    }

    let a = new A(this.hi)
    a.hi()
}

new Main() 
// output: Cannot read property 'name' of undefined
use @ziki/handler
使用 @ziki/handler
import Handler from './../dist/ziki.es6.handler.js'

function A(handler) {
    this.name = 'A'
    this.hi = function() {
        handler.run()
    }
}

function Main() {
    this.name = 'Main'
    this.hi = function() {
        console.log('main->', this.name)
    }

    let a = new A(new Handler(this, this.hi))
    a.hi()
}

new Main()
// output: main- Main