A basic problem of functional digital quantity!

var obj = {
    f1: function() {
        var p = new this.f2()
        Object.assign(this, p)
        console.log(1, this)
    },
    f2: function() {
        console.log(2, this)
        this.f3 = function() {
            console.log(3, this)
        }
    }
}

(above is the sample code, and the actual code is Mini Program"s)
now I get this in f2 is a {imageLoaded: examples ()} , why not this pointing to obj , what can I do to add f3 to obj , and also in f2

Page({
    f1: function() {
        console.log(this) //{}
        
        this.imageLoaded = function() {}
    },
    f2: function() {
        const p = new this.f1()
        Object.assign(this, p)
    },
})

<image src="{{item}}" hidden bindload="imageLoaded"></image>

you want Page to receive options as context, but Page has specified context. Can you directly register f3 to implement code logic


sample code is fine.
the this in the actual code can only depend on fate. You specify a this of {} . If you want to have a relationship with Page , that's the problem of the concrete implementation of Page . Generally speaking, all the this of F1 , f2 are incorrect.

Menu