Home Page
RABiD BUNNY FEVER
K.T.K

  • January 17, 2021, 03:27:40 AM *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: More JavaScript Language Oddities  (Read 4060 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 527
    • View Profile
    • Dakusan's Domain
More JavaScript Language Oddities
« on: September 28, 2009, 05:32:08 AM »


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.

Logged