Clarifying some JavaScript coding style

Hello,

By analyzing the peaces of codes below I had come to a conclusion that few things aren’t clear to me which need clarification. They are excerpt from an Adobe’s PS script.

At the first code sample, statements inside the function append are separated by comma instead by semicolon, but, if you notice, only if they are in one line. If a statement begins on a new line, there is no comma separation.
Does this mean that the usage of comma instead of semicolon inside function is completely valid and no statement separator at all if placed on a new line?

stID = stringIDToTypeID; function append(par1, par2) {
	aR = new ActionReference(), aD = new ActionDescriptor()
	aR.putProperty(stID('property'), stID(par2 || 'pattern')), aR.putClass(stID 'application'))
	aD.putReference(stID('null'), aR), aD.putPath(stID('to'), File(dpt + ht + 'someFolder/' + par1))
	aD.putBoolean(stID('append'), true), executeAction(stID('set'), aD, DialogModes.NO)
}

At the second code sample the function expression is wrapped in parentheses followed by a JS property. Is this an IIFE or something else that I don’t know of?

(w = wh('grp1', 'Width')).active = true

At the third code sample, simply what are the + symbols placed in front of the w object?

if ((w = +w.text) && !isNaN(+w)) {...}

Thanks

That is a confusing comma operator being used. All statements get executed, but if an assignment is on the left then only the last statement gets assigned.

var a = b(), c(), d(); // all are executed, and a equals the result from d()

I don’t think that there’s any benefit of coding in that type of style;

[quote=“xede1, post:1, topic:351323, full:true”]
At the second code sample the function expression is wrapped in parentheses followed by a JS property. Is this an IIFE or something else that I don’t know of?

(w = wh('grp1', 'Width')).active = true

I don’t even want to try and understand what the hell that’s doing. That’s a very good example of bad and confusing code.

That is a shorthand way to convert a string into a number. It’s usually advised against, because it just ends up being confusing.

Why are all of those confusing things being done? Mostly it’s because programmers are showing off about saving a few characters. That comes at the cost of being able to easily understand what it’s doing.

1 Like

Ok, with regards the obfuscated code

(w = wh('grp1', 'Width')).active = true

(w is assigned a returned object first from a call to wh) the property active is then set.

a further example

let person1,
    person2;

function makeObj (name) {

    return {
        name : name,
        active : false,
        getName() {
            if ( this.active === true ) {
                
                console.log( `Name is ${this.name}` )
            } else {

                console.log( 'No can do!!' )
            }
        }
    }
}

person1 = makeObj('Jack')

(person2 = makeObj('Jill')).active = true

person1.getName() // 'No can do!!'

person2.getName() // 'Name is Jill!!'

It’s not great code is it? With 'ad’s, 'w’s, 'wh’s, stID’s (I think there’s a clinic for that). A nightmare to come back to and debug

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.