Help? Error "has no properties" for javascript array

P

PhxVyper

Hello,

I am getting the error: "Error: styleElementsArray.GLOBAL_STYLES has
no properties" with the following code.

The error is pointing me to the line:
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';

What am I doing wrong???

function someFunction()
{
var styleElementsArray = [];
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';
styleElementsArray['GLOBAL_STYLES'][1] =
'ff_container_background_color';
styleElementsArray['GLOBAL_STYLES'][2] = 'ff_container_font_family';
styleElementsArray['MAIN_HEADER'][0] = 'ff_mainHeader_color';
styleElementsArray['MAIN_HEADER'][1] =
'ff_mainHeader_background_color';
styleElementsArray['SUB_HEADER'][0] = 'ff_subHeader_color';
styleElementsArray['SUB_HEADER'][1] =
'ff_subHeader_background_color';
styleElementsArray['HEADER_BLOCK_CONTENT'][0] =
'ff_headerBlockContent_color';
styleElementsArray['HEADER_BLOCK_CONTENT'][1] =
'ff_headerBlockContent_background_color';
styleElementsArray['SEARCH_RESULTS_CONTENT'][0] =
'ff_blockContentSearchResult_color';
styleElementsArray['SEARCH_RESULTS_CONTENT'][1] =
'ff_blockContentSearchResult_background_color';
styleElementsArray['SEARCH_RESULTS_ITEM'][0] =
'ff_headerBlockSearchResultItem_color';
styleElementsArray['SEARCH_RESULTS_ITEM'][1] =
'ff_headerBlockSearchResultItem_background_color';
}

someFunction();

Thanks,
Chad
 
G

Gregor Kofler

PhxVyper meinte:
Hello,

I am getting the error: "Error: styleElementsArray.GLOBAL_STYLES has
no properties" with the following code.

The error is pointing me to the line:
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';

What am I doing wrong???

"0" is not a valid name for a property.

Gregor
 
T

Thomas 'PointedEars' Lahn

PhxVyper said:
I am getting the error: "Error: styleElementsArray.GLOBAL_STYLES has
no properties" with the following code.

The error is pointing me to the line:
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';

What am I doing wrong???

You do not create the (Array) object that you access later. And `undefined'
has no properties.
function someFunction()
{
var styleElementsArray = [];

Insert

styleElementsArray['GLOBAL_STYLES'] = [];

here.
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';
styleElementsArray['GLOBAL_STYLES'][1] =
'ff_container_background_color';
styleElementsArray['GLOBAL_STYLES'][2] = 'ff_container_font_family';

Insert

styleElementsArray['MAIN_HEADER'] = [];

here.
styleElementsArray['MAIN_HEADER'][0] = 'ff_mainHeader_color';
styleElementsArray['MAIN_HEADER'][1] =
'ff_mainHeader_background_color';

Insert

styleElementsArray['SUB_HEADER'] = [];

here.
styleElementsArray['SUB_HEADER'][0] = 'ff_subHeader_color';
styleElementsArray['SUB_HEADER'][1] =
'ff_subHeader_background_color';

Insert

styleElementsArray['HEADER_BLOCK_CONTENT'] = [];

here.
styleElementsArray['HEADER_BLOCK_CONTENT'][0] =
'ff_headerBlockContent_color';
styleElementsArray['HEADER_BLOCK_CONTENT'][1] =
'ff_headerBlockContent_background_color';

Insert

styleElementsArray['SEARCH_RESULTS_CONTENT'] = [];

here.
styleElementsArray['SEARCH_RESULTS_CONTENT'][0] =
'ff_blockContentSearchResult_color';
styleElementsArray['SEARCH_RESULTS_CONTENT'][1] =
'ff_blockContentSearchResult_background_color';

Insert

styleElementsArray['SEARCH_RESULTS_ITEM'] = [];

here.
styleElementsArray['SEARCH_RESULTS_ITEM'][0] =
'ff_headerBlockSearchResultItem_color';
styleElementsArray['SEARCH_RESULTS_ITEM'][1] =
'ff_headerBlockSearchResultItem_background_color';
}

The above code can be simplified and written easier maintainable as follows:

var styleElements = {
GLOBAL_STYLES: ["ff_container_color", "ff_container_background_color",
"ff_container_font_family"],

MAIN_HEADER: ["ff_mainHeader_color", "ff_mainHeader_background_color"],

SUB_HEADER: ["ff_subHeader_color", "ff_subHeader_background_color"],

HEADER_BLOCK_CONTENT: ["ff_headerBlockContent_color",
"ff_headerBlockContent_background_color"],

SEARCH_RESULTS_CONTENT: ["ff_blockContentSearchResult_color",
"ff_blockContentSearchResult_background_color"],

SEARCH_RESULTS_ITEM: ["ff_headerBlockSearchResultItem_color",
"ff_headerBlockSearchResultItem_background_color"]
};

As you can see, in J(ava)Script, styleElements(Array) is not an array at all
(hence the name change). Since no features of Array objects are used there,
it is better to create an Object object instead, here with the Object
literal/initializer.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Gregor said:
PhxVyper meinte:
I am getting the error: "Error: styleElementsArray.GLOBAL_STYLES has
no properties" with the following code.

The error is pointing me to the line:
styleElementsArray['GLOBAL_STYLES'][0] = 'ff_container_color';

What am I doing wrong???

"0" is not a valid name for a property.

Sorry, that is utter nonsense. `0' might not be an identifier (and so MUST
NOT appear in as operand of a dot property accessor), but it is of course a
valid name for a property, as especially Array objects prove. Note that all
property names are stored as strings, even array indexes.


PointedEars
 
P

PhxVyper

On Sep 13, 2:31 pm, Thomas 'PointedEars' Lahn <[email protected]>
wrote:

SNIP
var styleElements = {
GLOBAL_STYLES: ["ff_container_color", "ff_container_background_color",
"ff_container_font_family"],

MAIN_HEADER: ["ff_mainHeader_color", "ff_mainHeader_background_color"],

SUB_HEADER: ["ff_subHeader_color", "ff_subHeader_background_color"],

HEADER_BLOCK_CONTENT: ["ff_headerBlockContent_color",
"ff_headerBlockContent_background_color"],

SEARCH_RESULTS_CONTENT: ["ff_blockContentSearchResult_color",
"ff_blockContentSearchResult_background_color"],

SEARCH_RESULTS_ITEM: ["ff_headerBlockSearchResultItem_color",
"ff_headerBlockSearchResultItem_background_color"]
};

As you can see, in J(ava)Script, styleElements(Array) is not an array at all
(hence the name change). Since no features of Array objects are used there,
it is better to create an Object object instead, here with the Object
literal/initializer.

HTH

PointedEars

Fantastic! Well, I definitely learned something today :)

Thank you all so much for you assistance. I'm going to rewrite this
to use the object method as described above.

PhxVyper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top