1

Javascript do not have structures.

So can i create a GLOBAL object like struct abc in javascript .

Struct abc contains some variables & array of another structure struct xyz.

struct xyz {

  var age;
  var name;

};

struct abc {

  var start;
  var end;
  var length;

  struct xyz  xyz_array[100];


};

If structures is not possible in javascript how can i save data in this format ?

============================= Edit some solution i found =================

http://www.w3schools.com/js/js_object_definition.asp
Javascript: How to create an array of object literals in a loop

=============================================

var details = {
    Start: 0,
    End: 0,
    length: 0
};



var arr = [];
var len = 100;

for (var i = 0; i < len; i++) {
    arr.push({
        direction: "out",
        message  : ""
    });
}

=====================================

Array inside a Javascript Object?

var details = {
    Start: 0,
    End: 0,
    length: 0
    info_text : []
};



var len = 100;

for (var i = 0; i < len; i++) {
    details.info_text.push({
        direction: "out",
        message  : ""
    });
}

=========================================

Community
  • 1
  • 1
Katoch
  • 2,709
  • 9
  • 51
  • 84

5 Answers5

3

TLDR: No. Use objects instead.

JavaScript does not have a struct concept in the language. You can instead use objects:

var xyz = {
    something: "hello world"
}
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
3

The closest equivalent in Javascript is an 'object'. There are two ways you can create and use them, demonstrated below.

Simple example, using an object literal

var xyz = {
    age: 42,
    name: 'fred'
};

More complex example, using a Javascript 'class' (really function)

function xyz(age, name) {
    this.age = age;
    this.name = name;
    this.getAge = function() { return age; }
}

var myXyz = new xyz(42, 'fred');
myXyz.getAge(); // Returns 42
2

Javascript is an object-based dynamic language that doesn't use the struct concept from C.

For your requirement you can use -

/* definition of an Object */
var xyz = function(age,name){
          this.age =  age;
          this.name = name;
    return this;
};

/* composition */
var abc = {
          start:'',
          end:'',
          length:'',
          xyz_pseudo_array: []
};

//push data inside the **xyz_pseudo_array** as and when needed, without having to worry about size-
abc.xyz_pseudo_array.push(new xyz(25,"Bill Gates"));
Subhranshu
  • 435
  • 2
  • 12
0

In JavaScript you can use function(){ ... }, in which can act more similar to a class in other languages, in which can do the same thing a struct can do but more powerful.

function xyz() {
   this.age = ...;
   this.name = ...;
}

You can also just use var for an object:

var xyz = { age: ... , name: ... };
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

Cited from JavaScript: The Good Parts

The simple types of JavaScript are numbers, strings, booleans (trueandfalse),null, and undefined. All other values are objects. Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections. In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects.

So you can use object, example :

function xyz(age, name){
    this.age = age;
    this.name = name;
}

var abc = {
    start : null,
    end : null,
    length: null

    xyz_array: []
}

abc.xyz_array.push( new xyz(33, 'johnDoe') );
abc.xyz_array.push( new xyz(32, 'jeanDoe') );

console.log(abc);
sam
  • 1