Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid javascript object property names

Tags:

javascript

I'm trying to work out what is considered valid for the property name of a javascript object. For example

var b = {} b['-^colour'] = "blue";     // Works fine in Firefox, Chrome, Safari b['colour'] = "green";      // Ditto alert(b['-^colour']);       // Ditto alert(b.colour);            // Ditto for(prop in b) alert(prop); // Ditto //alert(b.-^colour);     // Fails (expected) 

This post details valid javascript variable names, and '-^colour' is clearly not valid (as a variable name). Does the same apply to object property names? Looking at the above I'm trying to work out if

  1. b['-^colour'] is invalid, but works in all browsers by quirk, and I shouldn't trust it to work going forward

  2. b['-^colour'] is completely valid, but it's just of a form that can only be accessed in this manner - (it's supported so Objects can be used as maps perhaps?)

  3. Something else

As an aside, a global variable in javascript might be declared at the top level as

var abc = 0; 

but could also be created (as I understand it) with

window['abc'] = 0; 

the following works in all the above browsers

window['@£$%'] = "bling!"; alert(window['@£$%']); 

Is this valid? It seems to contradict the variable naming rules - or am I not declaring a variable there? What's the difference between a variable and an object property name?

like image 683
hawkett Avatar asked May 30 '10 21:05

hawkett


People also ask

Which object name are valid in JavaScript?

JavaScript object key names must adhere to some restrictions to be valid. Key names must either be strings or valid identifier or variable names (i.e. special characters such as - are not allowed in key names that are not strings). platform num: 10, // Invalid because of the space between words.

What are the properties of JS objects?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

What are property names in JavaScript?

The function name property of the javascript object is used to return the name of the function. This name property of the function is only readable and cannot be altered. The name of the function which was given when the function was created is returned by Function.name.

How many types of object properties are there in JavaScript?

Properties are identified using key values. A key value is either a String value or a Symbol value. There are two types of object properties: The data property and the accessor property.


1 Answers

Yes, objects can be used as maps, and any string can be a property name. As you've discovered, some properties can only be accessed using the bracket syntax.

window['abc'] 

is accessing a property. It is not a variable, even though it refers to the same value (at the global level) as:

abc 
like image 167
Matthew Flaschen Avatar answered Sep 18 '22 01:09

Matthew Flaschen