最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

solution to create structure object in javascript - Stack Overflow

programmeradmin2浏览0评论

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 =================

.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  : ""
    });
}

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

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./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  : ""
    });
}

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

Share Improve this question edited May 23, 2017 at 12:05 CommunityBot 11 silver badge asked Oct 31, 2014 at 7:09 KatochKatoch 2,77710 gold badges57 silver badges88 bronze badges 4
  • there is no concept of struct in javascript – Prabhu Murthy Commented Oct 31, 2014 at 7:11
  • so how can i achieve this ... ? – Katoch Commented Oct 31, 2014 at 7:11
  • What is the "this" that you wish to achieve? In JS, the executing engine handles memory management -- you do not, generally, have access to low level concepts like this. But if you merely want to group a set of values, you just use an object. – Semicolon Commented Oct 31, 2014 at 7:18
  • why do you think you need a struct. a simple object is suffice. – Prabhu Murthy Commented Oct 31, 2014 at 7:19
Add a ment  | 

5 Answers 5

Reset to default 4

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 plex 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

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"
}

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;
};

/* position */
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"));

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: ... };

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);
发布评论

评论列表(0)

  1. 暂无评论