Wednesday, January 26, 2011

JavaScript structures

Here's a brief example of how to create structures in JavaScript. We'll use this function a lot from now on in our code.

// Make Struct
function MakeStruct(names) {
var names = names.split(' ');
var count = names.length;
function constructor() {
for (var i = 0; i < count; i++) {
this[names[i]] = arguments[i];
}
}
return constructor;
}

// Use
// Declaring the struct
var Person = MakeStruct("ID FirstName LastName Email");
// Instance
var person1 = new Person(21, "Cornel", "Croitoriu", "cscmediadesign@yahoo.com");

Hope you'll find this useful ;)

0 comentarii: