Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:05:32+00:00 2026-05-23T13:05:32+00:00

I think I have a problem with closures and execution contexts, although I’m not

  • 0

I think I have a problem with closures and execution contexts, although I’m not really sure.

Let me explain exactly what I’m trying to do and why I’ve coded it in the way I have:

1) I’m building a javascript powered interface, the interface contains different apps (I’m calling them widgets).

2) All the widgets are moveable/dragable/closeable/resizeable etc.. I’m trying to achieve something like a desktop interface although has to be said that what I’ve got is far less than a web based opperating system. But that is what I’m aiming for.

3) So I needed a way for the JS code to load each of these widgets without knowing which widgets exist and/or anything else about them. I decided that when the interface is loading it should call a php script to find out the names of the widgets.

4) The JS code has the name of each widget, it now loops through them all calling another php script, this php script will feed back the code for loading that particular widget and the html code to display it. The JS appends elements to the screen for the display and then it loads the JS code for the widget.

5) This is the tricky part, actually loading the code for each widget, I did it like this:

var func=new Function(ajax.responseText);
func();

And it works fine, or appears to atleast.

6) Now I have a problem with objects and arrays and I think it has something to do with execution contexts.

Array Example:

var myArray=new Array('foo', 'bar', 'fooBar', 'barFoo');

Looks OK to me, no problem with the actual array (unless I typed something wrong in there 🙂 can’t work without my Net Beans editor) I tried running this as a seperate script and it works fine, ie; with: alert(myArray[0]); resulting in foo

Array Problem:

My JS code can’t do anything usefull with this array because actually calling alert(myArray[0]); will result in foo, bar, fooBar, barFoo.. No idea why this happens really but it’s really messing everything up now.

Object Example:

var myObject=new Object();
myObject.a='foo';
myObject.b='bar';
myObject.c='fooBar';
myObject.d='barFoo';

Again, object works fine when run by it’s self.

Object Problem:

Very similar problem as with arrays, calling alert(myObject['a']); will actually result in [object Object],[object Object],[object Object], [object Object] …

…so that’s the same problem as with arrays, but it gets even worse with objects, if I call alert(myObject.a); (notice that i didn’t use [‘a’] this time) it will just result in undefined.

So to backtrack a little and further explain the way that everything is tied in together:

1) when the JS loads it will call a php script which returns a JSON encoded object containing 3 keys(‘functions’, ‘JSscript’, ‘Other’). Each key contains the source code of another javacript file. These are all loaded using a dynamically created function like: var func=new Function(ajax.responseText).

2) Then it’s these new javascript codes that call another php script for further directions.

3) Php responds with JSON, which is evaluated inside a dynamically created function and stored in a new variable.

4) The new variable is now an object containing maybe 3 keys (html, javascript, anotherObject) The html is appended into the document, The javscript is executed in a new function as above.

5) The problems come with objects/arrays that exist in the JS executed in my last statement.

So if anyone can help me out on this I’d be greatfull, and if you need further explaination or code examples then obviously I’ll be happy to provide.

Thanks,

Richard

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:05:32+00:00Added an answer on May 23, 2026 at 1:05 pm

    Ohhhh.. Turns out this was nothing to do with closures or any other javascript for that matter, sorry to excite you all.

    It was caused by a php script, and not even the php script that handles ajax requests, it was actually the script that reads the javascript files and then JSONs them. That script also has a find and replace function like a simple templating system where it will find all the words between ‘[‘ & ‘]’ which unfortunately means any arrays in the javascript will get bungled up.

    I used the find and replace with square braces because it was originally meant for html templating, I just didn’t consider it properly when I started using the system in my JS code aswell.

    So to clarify, my php script would change JS of obj[var].description into obj.description which is valid JS so didn’t flag any errors for me to notice it.

    Ohhhhh. What a loser 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have a problem in understanding the proper way of using MVC.
I think I have a synchronization problem...It may be too basic..Please help.. I have
I have a problem which I think is related to forward declarations, but perhaps
I have a problem with a template and pointers ( I think ). Below
I have a problem drawing something quickly in .NET. I don't think that any
i have a problem that i can't solve ! (sqlite3, but i think it
I have a small problem with how should i think a... problem. I want
I have a peculiar problem, which I think that a lot of other people
I have been having the following problem, i think it's probably due to the
I actually have two questions regarding the same problem but I think it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.