3 ms·
Extending a browser provided prototype shouldn't be done lightly. You certainly couldn't do something like that if you were writing a library, as you might con
by ben336 12y ago
Extending a browser provided prototype shouldn't be done lightly. You certainly couldn't do something like that if you were writing a library, as you might conflict with somebody elses implementation of the same add on.
There's also value in having things like this standardized. If its a common idiom, its worth including in the standard to allow people to use it without having to worry about what utility libraries they're including.
- lomnakkus 12y agoI actually tend to think that this kind of shows the folly of single-dispatch OO in general (prototypical or not), in particular its focus on privileging the first parameter syntactically. The problem here is essentially that a freestanding function looks and thus (subjectively) "feels" different from a method. The syntax is obviously slightly different: "myArray.last()" vs. "last(myArray)". The latter stands out as slightly "alien" when compared to the other methods on Array. Why should the first parameter be privileged? Why isn't it possible to add a new method to, say, Array which is scoped to your lexical context? Even classic procedural programming didn't have this problem (as long as you don't need access to private data): Just add a function that does what you want and it'll look and behave just as the other functions on Array. EDIT: Maybe the focus shouldn't be on adding a few convenience methods here and there, but rather on making the language itself more "pliable" or "growable" as Steele would probably say.
- jkrems 12y agoNothing prevents you from using a function (`last(myArray)`). In a dynamically typed language what you are proposing makes only very limited sense.
- lomnakkus 12y agoI'm not saying functions are bad or anything. They just "feel" wrong since they don't look like the built-ins. Regarding "dynamically typed": CLOS[1] got this right. Common Lisp is dynamically typed. In short: Dynamic/static typing has very little to do with this. [1] http://en.wikipedia.org/wiki/Common_Lisp_Object_System http://en.wikipedia.org/wiki/Common_Lisp_Object_System
- aikah 12y agoscoped monkey would be quite usefull? One could mess with prototypes yet keep them clean in the global scope. last(array) feels to much like C style OOP.
- underwater 12y agoI agree. C# has this feature. They call them extension methods. They alive the scoping issue quite nicely: http://msdn.microsoft.com/en-us/library/bb383977.aspx http://msdn.microsoft.com/en-us/library/bb383977.aspx
- esailija 12y agoThey should just provide an operator like ::, then features that can be implemented without special language support don't need to bloat the spec. array::last() would mean same as last.call(array) that means `last` can just be a regular variable in scope
- thathonkey 12y agoAll valid points but there are ways that you can implement prototypes safely (even in the presence of other utility frameworks that might implement their own version), or use one of the many other ways JavaScript provides you for.