As we know we can assign a function/method to another variable and execute it anytime with the assigned variable.
const logger = {
log: (text) => {
console.log(text);
},
};
const log = logger.log;
logger.log("Log test");
log("Log test 2");
But why we can't assign document.querySelector
to a variable and use it. querySelector
is also a method of the document
object.
console.log( typeof document.querySelector ) // function
const myDocument = document.querySelector;
myDocument("body"); // Uncaught TypeError: Illegal invocation