This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Can arrays in Javascript contain functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Can arrays in Javascript contain functions" Watch "Can arrays in Javascript contain functions" New topic
Author

Can arrays in Javascript contain functions

Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
Hi all
I want to know can arrays in java script contain functions. And if yes then can that array have normal variables as their members.
Actually I am developing an application where I am using a third party webservice. Using the key I have got , I am calling a function which needs a js array as parameter. This array should contain normal variables as well as functions. When I try to this I am getting javascript error array[i][0] not a function. Actually my third array element is a function.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Originally posted by Aniket Kedari:
I want to know can arrays in java script contain functions.
Yes. In JavaScript functions are first class objects and can be referenced like any other type -- including within arrays.

And if yes then can that array have normal variables as their members.
Yes. Arrays in JavaScript can be heterogenous. Each element can be any valid JavaScript type.

When I try to this I am getting javascript error array[i][0] not a function. Actually my third array element is a function.
Then would not the reference be array[2]? Why are you using a doubly-subscripted array reference?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
hen would not the reference be array[2]? Why are you using a doubly-subscripted array reference?

-> Actually I am using a two dimensional array. I am populating it in a for loop. Each row has 4 columns. first two and last are normal values and third one is function.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Well, then we'll obviously need more info to try and diagnose the problem. If the reference at array[i][0] is actually a function, there's no reason it can't be called or otherwise referenced.
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
One more thing to add the function I am adding is like a nested one.
ex array[i][2] = function(abc){
stat1...
};
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
javaranch.com is not accepting my code because of < tag.
I rtied to replace manuallly it with & lt; but doesn`t work.
Can anybody help me please
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

First pare your code down to the smallest snippets that demonstrate the problem, Pasting in whole pages is usually not helpful. Angle brackets are usually not the problem. Phrases such as onclick and eval() are usually what blocks submission.

The FAQ for this forum also has a link to a handy tool that could help you post the code.
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
Hi Bear Bibeault
Above two posts contain my entire program.
Please try to append and run it in IE.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people will simply not look at it. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
Above two posts contain my entire code.
Make spelling correction for OLoad and OnUnLoad in second post
Run it in tomcat and mozila.
It gives me error altitude is null
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Do you find the posted code to be readable?
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
Really very sorry for not using UBB code in last posts. Noe I have remoced them . Here is my new code with UBB tags.
I know I should give only a code sniplet instead of entire code but I am so naive that I dont know where the potential error lies.
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
Aniket Kedari
Ranch Hand

Joined: Jan 29, 2008
Posts: 30
I think now I have posted it correctly.
Please help me to sort out problem in above code.
please change single line containing "body oload" containing correctly and run using tomcat and mozila.
 
Consider Paul's rocket mass heater.
 
subject: Can arrays in Javascript contain functions
 
Similar Threads
An object to hold several arrays
function overloading problem
How to write retrieved data into javascript Array?
is prototype required?
JavaScript return string