Modernizr.prefixed(str,obj) to use Function.prototype.bind? #478
Comments
a possibility for scoping for the last example from alex... use a 3rd param. var myelem = evt.target;
var ms = Modernizr.prefixed( 'matchesSelector', HTMLElement.prototype, myelem)
if (ms('.item')) ... |
That is a tough one. I would argue that this about detecting and not creating a helper method, however being both would be advantageous. I could play around with a 3rd arg and see how it feels? |
I think i wanna do the autobind. questions:
.
|
Sorry was mobile earlier and couldn't chime in. I think we can do this pretty easily with something like the following: Normal Case // Since 'requestAnimationFrame' exists on window as a function,
// return the function bound to window
Modernizr.prefixed('requestAnimationFrame', window) Override to string: // Passing false as a third param disables the possibility
// of a function return, always a string
Modernizr.prefixed('requestAnimationFrame', window, false) // window.webkitRequestAnimationFrame Override to other binding: // force a binding by passing the bound object.
// This could be avoided by just passing in the element as the second param (as long as we look into prototypes).
Modernizr.prefixed( 'matchesSelector', HTMLElement.prototype, document.getElementById('theid')) Downsides I can see:
|
Perhaps that means you should have to opt in for it. Always returns a string, unless you pass a third param. pass Further if you expect a function back, the result should be falsey if it doesn't exist (instead of the raw string like in the string version of the function). A noop function could potentially work, but I haven't thought into that much. |
It should be falsy return if the test fails.. which would allow for a nice rAF polyfill window.rAF = Modernizr.prefixed('requestAnimationFrame', window) || function(){ ... The rest sounds good to me. I would prefer to return the function / object / prop value when possible instead of its name. Trying to think of any prefixed property names that are just string values or numbers... |
Check out some early code in the prefixed branch 16a056d I just grabbed the window.rAF = Modernizr.prefixed('requestAnimationFrame', window) || function(){ ... This will now work as expected either returning a reference to rAF of false and falling back to function // Passing in the 3rd arg will bind to that
// e.g. ms("div") -> true
var ms = Modernizr.prefixed("matchesSelector", HTMLElement.prototype, document.querySelector("div")) So the three possible results that this change can get:
|
Also it doesn't do any hand holding so if someone tries to pass in something silly for the 3rd arg it will throw. |
ryan you got a patch for this alls et? |
I just merged in Ryan's branch. Considering this ticket closed code-wise but we'll have to add docs. |
Okay. code and tests updated. lots of tests! i think this is basically what @SlexAxton proposed here I have some extra checks so we're not calling
|
Done except for docs. |
I approve this commit, good stuff! |
docs added. |
or just
The reason why
Some recent examples from user tests..
The double
window
/nav
references kinda suck.. wondering if we just automatically bind the scope of it so we can pass back a function.Hmmm
But would we only do this if the resulting thing is a function? And still just pass back a string otherwise?
how about
Modernizr.prefixed('performance', window);
.. pass back object or string?Also to consider..
so we should make a call..what do we return?
matchesSelector
example disqualifies this)The text was updated successfully, but these errors were encountered: