Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I set properties of an array element (a string) in JavaScript?

var array = ['a', 'b', 'c'];
array[0].property = 'value';

alert(array[0].property);   
alert(array[0].property = 'value');
alert(array[0].property);   

The result? undefined, 'value', then undefined

Why isn't this code working as expected?

like image 703
ClosureCowboy Avatar asked May 28 '11 18:05

ClosureCowboy


People also ask

Can array index be a string?

Javascript arrays cannot have "string indexes". A Javascript Array is exclusively numerically indexed. When you set a "string index", you're setting a property of the object.

Can you have an array of strings in JavaScript?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

Can an array have properties?

arrays are normal objects and their elements are properties with names 0, 1, .. etc. arrays also have a special properties lenght and many function that can manipulate the elements.

How do you add properties to an array?

Arrays are objects and therefore you can add your own properties to them: var arr = [1, 2, 3]; arr.


2 Answers

The Array is irrelevant - you're trying to set a property on a primitive:

A data that is not an object and does not have any methods. JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined. With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.

If you absolutely must use a property to carry this additional information around in a string, an alternative would be to use the object equivalent:

>>> var array = [new String('a'), new String('b'), new String('c')];
>>> array[0].property
undefined
>>> array[0].property = 'value'
"value"
>>> array[0].property
"value"

A potential gotcha to look out for if you do this and later need to detect that the value is a string:

>>> var a = ['s', new String('s')];
>>> a.map(function(s) { return typeof s; });
["string", "object"]
>>> a.map(function(s) { return s instanceof String });
[false, true]
>>> a.map(function(s) { return Object.prototype.toString.call(s) == '[object String]' });
[true, true]
like image 148
Jonny Buchanan Avatar answered Oct 20 '22 01:10

Jonny Buchanan


Your array elements are string literals. JavaScript makes it seem like string literals are objects and have properties, but what's actually happening when you do array[0].property is that JavaScript is creating a temporary object for your string and assigning the property to that.

That's why the middle alert works correctly and the others don't. If you declared your array like:

var array = [new String('a'), new String('b'), new String('c')];

all three would work.

like image 27
John Flatness Avatar answered Oct 20 '22 01:10

John Flatness