ap = Array.prototype,
aps = ap.slice,
apsp = ap.splice,
I often see code like above in different frameworks. What are the benefits of this?
Why not use it directly?
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.
Several benefits:
One should note that I think it makes the code less readable to people not already familiar with the code or its conventions because the code isn’t as self documenting. Everyone knows what
Array.prototype.spliceis but strangers don’t know whatapsis until they study the code, track down its definition and remember what it is.I was asked in a comment to explain the fewer lookups issue:
For the interpreter to resolve
Array.prototype.splice, it has to do the following:Arrayname. It doesn’t find it.Arrayname. It doesn’t find it.Arrayname. It finds it.Arrayobject, look for theprototypeproperty.prototypeproperty, look for thespliceproperty and now it finally has the function it needs.For the interpreter to resolve
aps, it has to do this:apsname. It finds it and not it has the function it needs.The pre-assignment to
apshas essentially pre-done the lookups so that they are already resolved at runtime. This removes some flexibility because if the value of the splice or prototype properties are changed, the variableapswon’t reflect that change, but if you know they aren’t supposed to change (something the run-time interpreter doesn’t know), then you can take advantage of this shortcut.