What Is This Practice Called in JavaScript

What is this practice called in JavaScript?

The pattern is called self-invocation, a self-invoking function. It can create a closure, but that is an effect of the pattern (perhaps the intended effect), not the pattern itself.

What is this practice called? `objectName && objectName.thing`

This is sometimes called a "guard," since the truthiness of the left operand guards access to the right operand. Of course, it's just a logical AND, but the use of AND in this specific context is occasionally called a "guard".

From Douglas Crockford's A Survey of the JavaScript Programming Language:

The && operator is commonly called logical and. It can also be called guard.

From Guard and Default Operators of JavaScript by Sean McArthur:

In Javascript, the way the languages determines logical operations and the values Javascript treats as true or false lead to people using the AND and OR operators for guard and default situations

What does this javascript syntax mean?

The

(function() { dosth; })();

syntax declares an anonymous function, and then executes it immediately. It's equivalent to doing this:

 var myFun = (function() { dosth; });
myFun();

but without the temporary variable.

Broadly speaking this is similar to just executing whatever dosth is; but creating a function object introduces a new scope for variables (due to the closure), and thus this is often used to work around issues with scoping.

In the specific case you've quoted, I don't see any reason why this would be particularly necessary. However it could be done like this for two reasons - either the final Javascript itself is generated by some automatic process that can't tell whether the closure is needed; or it was written by a human who decided to always wrap things in functions to be safe.

What is a best practice for ensuring this context in Javascript?

Using closure. Basically any variable declared in function, remains available to functions inside that function :

var Example = (function() {
function Example() {
var self = this; // variable in function Example
function privateFunction() {
// The variable self is available to this function even after Example returns.
console.log(self);
}

self.publicFunction = function() {
privateFunction();
}
}

return Example;
})();

ex = new Example;
ex.publicFunction();

Is the following JavaScript construct called a Closure?

It is an anonymous function (or more accurately a scoped anonymous function) that gets executed immediately.

The use of one is that any variables and functions that are declared in it are scoped to that function and are therefore hidden from any global context (so you gain encapsulation and information hiding).

Is it bad practice to use a function that changes stuff inside a condition, making the condition order-dependent?

Conditions are order-dependent whether you change the variables used in the condition or not. The two if statements that you used as an example are different and will be different whether you use myFunction() or not. They are equivalent to:

if (myFunction()) {
if (a === 2) {
alert("Hello, world!")
}
}

// Alert does not pop up.
if (a === 3) {
if (myFunction()) {
alert("Hello, universe!")
}
}

In my opinion, the bad practice in your code is not the fact that you change the condition's operands value inside the condition, but the fact that your application state is exposed and manipulated inside a function that does not even accept this state changing variable as a parameter. We usually try to isolate the functions from the code outside their scope and use their return value to affect the rest of the code. Global variables are 90% of the time a bad idea and as your code base gets larger and larger they tend to create problems that are difficult to trace, debug and solve.



Related Topics



Leave a reply



Submit