Dakusan's Domain Forum

Main Site Discussion => Posts => Topic started by: Dakusan on September 28, 2009, 05:32:08 am

Title: More JavaScript Language Oddities
Post by: Dakusan on September 28, 2009, 05:32:08 am
Original post for More JavaScript Language Oddities can be found at https://www.castledragmire.com/Posts/More_JavaScript_Language_Oddities.
Originally posted on: 11/17/08

This is sort of a continuation of the parseInt in JavaScript post made a few months ago.


Another minor JavaScript oddity is that it has two very similar String functions in its base library that are so similar that they can cause confusion to programmers. If a programmer only uses one of the two, and then tries to work with someone else’s code that uses the other function, things could easily get messy. These two functions are substr and substring, which w3schools defines as follows:

Function NameParametersDescription
substrStartIndex, LengthExtracts a specified number of characters in a string, from a start index
substringStartIndex, EndIndexExtracts the characters in a string between two specified indices

It is KIND of nice to have substring as a function for some purposes... but is it really so hard to do a...
String.substr(StartIndex, EndIndex - StartIndex)
?

I actually did something like this myself in my super awesome string library (which I have not yet released and do not know when I will...). I do not consider this hypocritical though because in my string library, I have a “substr”, like the JavaScript one, but the function that acts like JavaScript’s substring is called “mid”, after the function used in Visual Basic. I did this because I wanted the library to have “matching function names for many languages (PHP, JavaScript, VB, etc).” to make it easier on other programmers already familiar with other libraries.