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

  • Home
  • SEARCH
  • 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 7777189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:04:30+00:00 2026-06-01T18:04:30+00:00

Okay, so this is complicated so try to bear with me, i will try

  • 0

Okay, so this is complicated so try to bear with me, i will try to keep it as simple as possible:

I have a “class” Struct, that creates a “struct like” skeleton to apply to ArrayBuffers within Javascript. The problem is when I am attempting to mimic the ‘c’ like behavior of allowing a struct to contain other structs.

The problem is that it clobbers the iterator of the calling method (obviously a closure problem), that i cant seem to figure out.

Here is an example of the code that gets clobbered (hopefully this is enough code to get the answer, if not I will add more as necessary, just trying to keep extraneous code out of here):

function StructObject(){
this.applyBuf = function(buf, start){
   var struct = {};
   for (obj in this){
       //problem is here:
       console.log(obj); //prints "c" on the way in
       struct[obj] = this[obj].__createFromBuf();
       console.log(obj); //prints "foo" (see the structs below)
   }
   return struct;
 }
}


function struct(strctObj, name){
var structObject = new StructObject();
...
//create the skeleton
for (item in strctObj){
//the specific code that fails me
structObject[item].__createFromBuf = function(buf, pos){
            return structs[this.name].applyBuf(buf, pos);
}
...
//store the skeleton for later application
structs[name] = structObject;
}


//Creating structs looks like this:
new struct({ foo: type.INT }, "bar");
new struct({
   a: type.INT, //defines size of memory (like c's sizeof)
   b: type.LONG,
   c: {type: type.STRUCT, name: "bar"},
   d: type.SHORT}, "myStruct");

structs.myStruct.applyBuf(new ArrayBuffer(35));

When I iterate through the first struct on the class method applyBuf, it calls __createFromBuf on each item within the struct skeleton, if the item is another struct __createFromBuf calls applyBuf on the other “struct skeleton” object and returns an instance of that struct back to the calling struct, which works as intended.

JSFIDDLE — Here is the link to a working example 🙂

  • 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-06-01T18:04:31+00:00Added an answer on June 1, 2026 at 6:04 pm

    clean your for statements:

    if not declared properly, the for loop index becomes a global member.

    demonstration:

    for (item in strctObj) // item becomes a global member
    
    for (var item in strctObj) // item is scoped within the loop's owner function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this scenario is a little complicated, but I will try to explain it
Okay this may be a simple question but I have yet to come with
I'm using SQL Server 2008. I have a database table that looks like this
Okay this is what I have so far. I have a form that needs
Okay, this is getting complicated... Given situation: I have a canvas with the dimensions
Okay this is werid, i keep getting the error, randomly. ValueError: matrix must be
Okay - this is the dumbest glitch I have seen in a while: <!DOCTYPE
Okay guess this question looks a lot like: What is the best way to
Okay this is a real headscratcher. I have an application which calls a web
Okay this is a homework question, and I just don't have a clue how

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.