-1

Consider the following code (shortened for clarity):

Vertices.chamfer = function(vertices, radius, quality, qualityMin, qualityMax) {
    radius = radius || [8];

    if (!radius.length)
    radius = [radius];
};

I'm reading the first part as (pseudocode):

if (radius array passed to function) then
  radius = radius
else
  radius = [8] // new array, single element of value 8
end if

but I don't get the second expression (the if(!radius.length) radius = [radius] part).

Could someone explain it to me?

4 Answers 4

1
    Vertices.chamfer = function(vertices, radius, quality, qualityMin, qualityMax) {
            // Set the radius to the value passed in. 
            // If the value passed in is undefined, set the radius to an
            // array containing 8 (the "default")
            radius = radius || [8];

            // If radius isn't an array (i.e. length is 0 which evaluates to false), 
            // make it an array.
            if (!radius.length)
            radius = [radius];
        };
Sign up to request clarification or add additional context in comments.

1 Comment

...and keep in mind it will break if you pass the radius as a string, for example "7".length == 1 ;)
1

The concept used here is known as duck typing - i.e. checking the type of a variable by looking for the presence (or absence) of some characteristic properties/methods. Author of this code assumed that if something has a length property it is an Array, and if not it can be converted to an Array by wrapping it in [].

The second part, converting x to a single-item Array containing x using x = [x] is totally OK.

The problem is there are other types in JavaScript that have a length so this test is not really reliable.

For example: "7".length returns 1, so if you passed the variable as a string instead of a number (easy enough to make this mistake for example by reading values from <input> fields) something would break down the line expecting an Array but getting a String.

Another type that has a length is a Function: (function(a,b){}).length == 2.

So yeah, this is not really good test but the basic idea makes sense. Should have used either Array.isArray or some other property/method that is unique to Arrays.

EDIT: I'd also point out the radius = radius || [8] construct is only OK if 0 is not allowed as the radius argument.

1 Comment

Beaten to the post but upvoted as an equally thorough answer. Thank you.
0

I'm reading the first part as (pseudocode):

if (radius array passed to function) then

radius = radius || [8] checks to see if radius is falsy, which might be not passed in at all (undefined), or passed in as undefined, or passed in as null, 0, "", NaN, or of course false.

But yes, it's probably intended to use [8] if no radius was specified.

but I don't get the second expression (the if(!radius.length) radius = [radius] part).

If radius was given but is either A) An array with no entries (radius.length is 0 and therefore falsy), or B) Not an array (radius.length is undefined and therefore falsy), we create an array and make it the first entry in that array.

That's not at all robust validation, lots of things have length (strings, for instance), but that's probably what it's meant to do.

The robust way to check if something is an array is to use Array.isArray, which was added in ES5 and which can be reliably shimmed/polyfilled for obsolete engines (like the one in IE8).

Comments

0

if imported radius is not an array and it has value, then it returns an array with the radius value inside.

if imported radius is empty then it returns an array with the number 8 inside: [8]

However, inside the pseudocode, it is not gonna get in if statement, if the imported radius is not an array.

Comments

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.