This might sound like a weird question, considering variable and classes are completely different things, but I come from a Java background (Currently Associate degree level, 3rd year college student) and I've been reading up on javascript and watching videos.
A video about animation in js started with an intro to creating a vector
object. He defined a vector
in its own file called vector.js
and used it as the basis for particle motion in basic 2d animations.
Here is the code:
var vector = { _x: 1, _y: 0, create: function(x, y) { var obj = Object.create(this); obj.setX(x); obj.setY(y); return obj; }, setX: function(value) { this._x = value; }, getX: function() { return this._x; },
It continues with more getters and setters for angle, length, etc. Also defines methods for other vector operations like cross and dot product.
My questions are:
-How is this different from using a class with methods?
-Is this acceptable/standard code?
-Is the syntax foo: function(args)
as a header the same as function foo(args)
?
-Can you all point me to resources explaining the concept of having functions and parameters inside of seemingly a declared variale?
I have tried looking up information about, but I don't know if this syntax or usage has a name in js. I haven't seen anything like this in Java. I can't find any information.
Cheers.