Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a JavaScript object property with a hyphen in it?

I am using this script to make a style object of all the inherited, etc. styles.

var style = css($(this)); alert (style.width); alert (style.text-align); 

With the following, the first alert will work fine, but the second one doesn't... it's interpreting the - as a minus I assume. The debugger says 'uncaught reference error'. I can't put quotes around it, though, because it isn't a string. So how do I use this object property?

like image 411
Damon Avatar asked Aug 19 '11 13:08

Damon


People also ask

Can JavaScript object keys have dashes?

JavaScript object property names can be any string, including having hyphens in the name. However, object property names that are not valid JavaScript identifiers, can only be: Specified using quotes, and; Accessed using the bracket property accessor notation.

Does JavaScript support hyphens in property names?

CSS uses hyphens in property-names (font-size). Hyphens can be mistaken as subtraction attempts. Hyphens are not allowed in JavaScript names.

How do you add a hyphen in JavaScript?

To insert hyphens into a JavaScript string, we can use the JavaScript string's replace method. We call phone. replace with a regex that has 3 capturing groups for capturing 2 groups of 3 digits and the remaining digits respectively. Then we put dashes in between each group with '$1-$2-$3' .

How is an object property referenced in JavaScript?

Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.


2 Answers

Look at the comments. You will see that for CSS properties, the key notation is not compatible with a number of properties. Using the camel case key notation therefore is the current way:

obj.style-attr // would become  obj["styleAttr"] 

Use key notation rather than dot

style["text-align"] 

All arrays in JavaScript are objects and all objects are just associative arrays. This means you can refer to a place in an object just as you would refer to a key in an array.

arr[0] 

or the object

obj["method"] == obj.method 

A couple things to remember when accessing properties this way:

  1. they are evaluated so use strings unless you are doing something with a counter or using dynamic method names.

    This means obj[method] would give you an undefined error while obj["method"] would not

  2. You must use this notation if you are using characters that are not allowed in JavaScript variables.

This regex pretty much sums it up:

[a-zA-Z_$][0-9a-zA-Z_$]* 
like image 112
austinbv Avatar answered Sep 28 '22 16:09

austinbv


The answer to the original question is: place the property name in quotes and use array style indexing:

obj['property-with-hyphens']; 

Several have pointed out that the property you are interested in is a CSS property. CSS properties that have hyphens are automatically converted to camel casing. In that case you must use the camel cased name like:

style.textAlign; 

However this solution only works for CSS properties. For example,

obj['a-b'] = 2; alert(obj.aB);          // undefined alert(obj['a-b']);      // 2 
like image 33
Stoney Avatar answered Sep 28 '22 15:09

Stoney