class Test{
testfn(){
console.log("Class testfn")
}
}
let mixin = {
init(){
this.prop1 = this
},
prop2: this
}
let testInst = new Test()
extend(testInst, mixin)
testInst.init()
console.log(testInst)
This is the result
prop1 has been assigned in the init method, and is a reference to the Test instance itself.
prop2 has been assigned in the mixin, and is a reference to the window object. This is because the this
keyword is a reference to the instance when called from a method, because the method's scope is the class instance. However, that same this
keyword is a reference to the scope around the mixin when called from outside of a method. In that case, it's the window.
Welp, this was a short tutorial, hope you can all make good use of this 😄