basiljs / basil.js

An attempt to port the spirit of the Processing visualization language to Adobe Indesign.
http://basiljs.ch/
Other
246 stars 30 forks source link

Why do we have some Vector methods twice? (static method) #271

Open trych opened 6 years ago

trych commented 6 years ago

We have the methods Vector.dist(), Vector.cross() and Vector.dot() twice in our code.

I think this is intentional, however, can someone help me understand how this works?

Example dist:

We once have it as a method of the Vector prototype:

Vector.prototype = {

// ...

dist: function(v) {
      var dx = this.x - v.x,
        dy = this.y - v.y,
        dz = this.z - v.z;
      return Math.sqrt(dx * dx + dy * dy + dz * dz);
    },

// ...

}

and then we have it as a Vector method, outside the prototype thing:

  Vector.dist = function(v1, v2) {
    return v1.dist(v2);
  };

@basiljs For the second version it says, it is a "static function" (quote: "Is meant to be called "static" i.e. Vector.dist(v1, v2);"). I don't know what that means. What is a static method?

Also, I guess the second version does not overwrite the first version, right? I guess it extends it somehow? Is the first version supposed to be private?

Also, I think this turns into a problem that the inline docs of one version are overwriting the other one. How should we handle that? Should we rename one of them?

b-g commented 6 years ago

This is correct and IMO from the API design point identical to p5 and Processing.

var a = new Vector(1,1,1); // or use createVector() var b = new Vector(2,2,2);

var d = a.dist(b); // or written in a different style var d = Vector.dist(a, b);

We simply give the users the option to write the same operation in two different ways. Both are fine and is a matter of how you think about vectors. I would vote to leave it as it is.

RE: what is a static method? Please google something like "what is a static method in java” … basically: static = every instance of the class share a single identical “static” method

trych commented 6 years ago

Ah, okay, I get it now. Then I am just confused about the documentation. We should probably have that only in one place and then give examples for both syntax variants, right? @fabianmoronzirfas