How functions in vue methods are forced to bind to vue instances

question how are functions in: vue methods forced to bind to an vue instance?

var obj = {
    eventHandle: function (){
        // thiswindow
        console.log(this);
    }
};
window.addEventListener("resize", obj.eventHandle, false);

< hr >
var obj = {
    eventHandle: function (){
        // thiswindow
        console.log(this);
    }.bind(obj)
};
window.addEventListener("resize", obj.eventHandle, false);

< hr >
var vm = new Vue({
    el: "-sharpapp",
    methods: {
        eventHandle: function (){
            console.log(this);
        }
    },
    mounted(){
        /*
            // thisvue
            window.addEventListener("resize", this.eventHandle.bind(this), false);
         */
        
        /* thisvuewindow
        thisvue */
        window.addEventListener("resize", this.eventHandle, false);
        
    }
});

Mar.28,2021

if you post a piece of source code, you can see that all the methods in vue are forced to bind to the this context by bind. If you don't know anything about bind, you can search

yourself.
function nativeBind(fn, ctx) {
    return fn.bind(ctx)
}
var bind = Function.prototype.bind ? nativeBind : polyfillBind;
function initMethods(vm, methods) {
    //...
    for(var key in methods) {
        {
            //...
        }
        vm[key] = methods[key] == null ? noop : bind(methods[key], vm);
    }
}

so the this in this.eventHandle in window.addEventListener ("resize", this.eventHandle, false); ) points not to window, but to vm instance


this is contextual


you should note that your method is written in the vue instance, so how can this not point to the vue instance? if it is not pointing to the vue instance, it is impossible to access the eventhandle method under the menthods attribute at all

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb0e0f-230f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb0e0f-230f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?