Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript custom variable and custom array variable type

im trying to great a array of vector3's in JavaScript, im trying to create a Vertex, Edge, Face structure for a openGL cube, now im quite new to JavaScript but as HTML5 supports it, i feel JS should be a language i understand and now :),

Now I dont know how to declare a struct in JS and then how I would implement it into a array type?

I have something like this but i'm not sure if that's correct.

var vector3 = (x=0,y=0,z=0);

but then how would I use that for a array?

Cheers for the help.

like image 216
Canvas Avatar asked Oct 21 '12 16:10

Canvas


People also ask

What are the three types of variables in JavaScript?

In JavaScript, there are three different variable types: var , let , and const . Each of these variables have several rules around how they should be used, and have different characteristics. In this tutorial, we are going to explore the basics of variables in JavaScript.

What are the 4 ways to declare a JavaScript variable?

4 Ways to Declare a JavaScript Variable:Using var. Using let. Using const. Using nothing.

What is ${ var in JavaScript?

The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable: var carName; After the declaration, the variable is empty (it has no value).

Can you declare variable type in JavaScript?

You can declare variables to unpack values using the destructuring assignment syntax. For example, const { bar } = foo . This will create a variable named bar and assign to it the value corresponding to the key of the same name from our object foo . Variables should always be declared before they are used.


1 Answers

I would create an Object:

var vector3 = {
    x:0,
    y:0,
    z:0
};

You can access the individual fields with code such as:

var tmp = vector3.x;

To place points in a vector

var myPolygon = [
    {x: 3, y: 8, z: -8},
    {x: 3, y: 4, z: 10},
    {x: 9, y: 8, z: -8},
];

You could write a vector type with this too so you do not have to write x, y, and z every time:

var vec3 = {x:0,y:0,z:0};

var demoVec = vec3;
var demo2Vec = vec3;
demoVec.x+=demo2Vec.y;
like image 61
tobspr Avatar answered Sep 21 '22 18:09

tobspr