After many hours looking for a explanation and failing, I am asking it here.
All code here are examples made from what I was working on.
First Approach
Simple object:
let obj = {
fun: window.getComputedStyle,
ele: $('#myDiv')[0]
};
When trying the following function call:
obj.fun(obj.ele);
Gives the following error:
Uncaught TypeError: 'getComputedStyle' called on an object that does not implement interface Window.
I am using jQuery to retrieve the JS element, but using document.getElementById
would produce the same result.
Second Approach
I did search for a while about different types of objects, and found this style:
let obj = {
fun: function() {return window.getComputedStyle},
ele: $('#myDiv')[0]
};
But this type needs to be called differently:
obj.fun()(obj.ele);
This approach works correctly.
Question
Obviously the expanded version of the code works as intended:
window.getComputedStyle($('#myDiv')[0]);
The above code returns the full object correctly.
So my questions are:
- Why is the first approach (which makes more sense to me) fails?
- Is there a way to fix the first approach, so I can use the first type of function call?
- Is this type of function wrapping not supposed to be done?
Thank you!