2

I'm trying to port this Javascript function (shortened for clarity):

Vertices.scale = function(vertices, scaleX, scaleY, point) {

    point = point || Vertices.centre(vertices);

};

point is a vector, i.e. an object of the form { x: 5, y: 10 }. Vertices.centre(vertices) returns a similar vector object.

As far as I can see, there is no overloading of the '||' operator in the source code. In fact, I don't think you can overload operators in Javascript.

What does this code mean in plain English then?

1 Answer 1

2

It is the same as:

Vertices.scale = function(vertices, scaleX, scaleY, point) {
    if (point) {
        point = point;
    } else {
        point = Vertices.centre(vertices);
    }
}

Convention using || (logical OR operator) is shorthand for using default values for function paramters. (but notice that it won't work for boolean parameters).

You can find more about logical operators on MDN

Sign up to request clarification or add additional context in comments.

2 Comments

It seems so. In javascript you can call function with any number of parameters.
Thank you madox2. Very different than the language I'm trying to port this framework too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.