JSFiddle - React, Tailwind, and code Playground

by KendoDev

HTML

<div id="test" > click me</div>

JavaScript

var globalFunction = function () {
  alert("this belongs to  "+ this.constructor.name); // if no one is associated to the function then it belongs to window
};
// calling global function
globalFunction();

var myObject = { ownFunction:globalFunction };
myObject.ownFunction();

function Employee(name, funcArg) {
    this.Name = name;
    this.ShowName = funcArg;   
    if(typeof funcArg ==='function')
    {
        this.ShowName(); // call to the globalFunction
    }
}
var employee = new Employee("velu",globalFunction);
employee.ShowName(); // belongs to employee object and hence to Employee
Employee("velu",globalFunction); // belongs to Window, because it is a direct call to the function


var test = document.getElementById("test");

// when clicked, `this` will become the element
test.addEventListener('click', globalFunction, false); // belongs to div