• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Can arrays in Javascript contain functions

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear Bibeault
Above two posts contain my entire program.
Please try to append and run it in IE.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you find the posted code to be readable?
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Aniket Kedari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic