Why does Modernizr do the following:
toString = {}.toString,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s grabbing a local copy of the
Object.prototype.toStringmethod which would allow it to make small speed improvements in the script. This also allows it to test that thetoStringmethod exists.Regards to comments:
Every name resolution there is a cost, in lookup-time (locals, globals, prototype-chaining) and creation (closure-scoped variable), so imaging the following code:
For each iteration of the look we are having to resolve the
valuesvariable, and walk the prototype chain to identify the membertoString, and then execute that.Taking that above example, we could do the following:
Or even further:
Light applications won’t really benefit too much from this, but larger frameworks, such as jQuery, etc, can improve the script performance quite significantly. IE I believe is one such browser where these small improvements can help quite a lot.