Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In javascript, can I override the brackets to access characters in a string?

Is there some way I can define String[int] to avoid using String.CharAt(int)?

like image 270
Jimmy Avatar asked Oct 31 '08 21:10

Jimmy


People also ask

Under what circumstances would you need to use bracket notation to access a value in an object?

We must use bracket notation whenever we are accessing an object's property using a variable or when the property's key is a number or includes a symbol or is two words with a space.

What do square brackets mean in JavaScript?

The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.

What is dot notation in JavaScript?

JavaScript provides two notations for accessing object properties. The first, and most common, is known as dot notation. Under dot notation, a property is accessed by giving the host object's name, followed by a period (or dot), followed by the property name.


1 Answers

No, there isn't a way to do this.

This is a common question from developers who are coming to JavaScript from another language, where operators can be defined or overridden for a certain type.

In C++, it's not entirely out of the question to overload operator* on MyType, ending up with a unique asterisk operator for operations involving objects of type MyType. The readability of this practice might still be called into question, but the language affords for it, nevertheless.

In JavaScript, this is simply not possible. You will not be able to define a method which allows you to index chars from a String using brackets.

@Lee Kowalkowski brings up a good point, namely that it is, in a way, possible to access characters using the brackets, because the brackets can be used to access members of a JavaScript Array. This would involve creating a new Array, using each of the characters of the string as its members, and then accessing the Array.

This is probably a confusing approach. Some implementations of JavaScript will provide access to a string via the brackets and some will not, so it's not standard practice. The object may be confused for a string, and as JavaScript is a loosely typed language, there is already a risk of misrepresenting a type. Defining an array solely for the purposes of using a different syntax from what the language already affords is only gong to promote this type of confusion. This gives rise to @Andrew Hedges's question: "Why fight the language?"..

There are useful patterns in JavaScript for legitimate function overloading and polymorphic inheritance. This isn't an example of either.

All semantics aside, the operators still haven't been overridden.

Side note: Developers who are accustomed to the conventions of strong type checking and classical inheritance are sometimes confused by JavaScript's C-family syntax. Under the hood, it is working in an unfamiliar way. It's best to write JavaScript in clean and unambiguous ways, in order to prevent confusion.

like image 190
12 revs, 2 users 81% Avatar answered Sep 28 '22 04:09

12 revs, 2 users 81%