1.0.1 • Published 4 years ago
@ziki/handler
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