I save the reference of document method:
let qs = document.querySelector;
Then try to get the element:
btnSort = qs('button');
Why does this method not work as for references to simple functions?
I save the reference of document method:
let qs = document.querySelector;
Then try to get the element:
btnSort = qs('button');
Why does this method not work as for references to simple functions?
Because this
in JavaScript is determined in runtime.
document.querySelector(...) // this -> document
let qs = document.querySelector
qs(...) // In this case `this` refer to the global object, which is window in a browser
You need to bind this
when you created a function reference.
let qs = document.querySelector.bind(document)
Or giving a this binding when you call it.
qs.call(document, 'button')