At work, we use jQuery. Shortly after its use began, I saw that two developers were adding jobs to a file jquery-extensions.js. Inside, I found a whole bunch of methods added in the $
which was essentially the number of static methods of jQuery. Here's something:
$ Format string (str, args) {...} $ .objectToArray (obj) {...}
etc. None of them really uses anything to do with jQuery This saw me as weirdly.
After all, we needed to have a function in our library to localize dates to my solution:
Date.prototype.toLocaleDate = function () {...} Date.parseLocalDate = function () {...}
After some time after doing this, I get a senior developer who is asking me what I think That's what I am doing. He tells me that here, where I work, we do not make prototypes because they are bad. The only reason given to them was that they are basically due to poor language features because they can be "abused" and it is misleading in seeing the prototype (for example, how do I know the new date). ToLocaleDate () is a prototype and does not have native ECMAScript). $ By using Format code (...)
instead of "blah blah" .formatString (...)
, we're making it clear that anything with $$ is part of the original javascript No.
It looks a bit silly in those reasons, but I have offered to make a compromise so that it is not necessary to remember that any method is a prototype & mdash; Prefix of prototype function with $
:
string.prototype $ Format = function () {...} "blah blah" $ Format (...);
It was sacked quickly and now I have to add these $ .myPrototypeAsAFauxStaticMethodOnJQuery () functions everywhere.
Am I thinking that this practice is stupid?
The original prototype debate is a little tired, there are valid points on both sides, I hope to summarize them Try that it can be useful to see the big picture.
contra There is no need to expand the native prototype. You have everything you need.
Supporters The JS Standard library is rare at the peak. Tables and strings have many important features
use per functions or your own "Namespace". Supporter methods such as foo.trim ()
are more than in the sense of language compared to functions such as org.blah.trim (foo)
. Everything in javascript is an object and keep it like this.
Resistance The original JS objects are "reserved" for language designers, our methods can override newly added built-ins by mistake. Supporters < / Strong> Open Objects is a great feature and it will not be silly to use it. A new JavaScript version does not happen every day and is well known in advance in addition to the standard.
Dispute The expansion of the native prototype is confusing because there is no difference between us and the basic methods.
Supporters JS Standards Library is small and well-documented. We need JavaScript developers to know the names of the basic methods.
contra extension prototype namespace can cause conflicts. Supporter Yes, but it can also happen with global works or famous global objects (like $
).
Contract Custom methods are enumerable
Supporters Yes, but for the rescue, isOpenProperty
it's your own calculator Wrap the ceremony and close the use of loops with the raw for..in
object. (There is no real answer, therefore, CW)
Comments
Post a Comment