function
or var declarations in nested blocks is not preferred JS-0016188 // Nueva clase de apoyo, reemplazar para ocupar bootstrap.
189 if ( attributes.className ){
190
191 function addnewClassName(clase){192193 if( clase.includes('is-style-nav-tabs') ){194 if( clase.includes('nav-pills') ){195 clase = clase.replaceAll('nav-pills', '')196 }197 clase = clase.replace('is-style-nav-tabs', 'nav-tabs')198 }199200 if( clase.includes('is-style-nav-pills') ){201 if( clase.includes('nav-tabs') ){202 clase = clase.replaceAll('nav-tabs', '')203 }204 clase = clase.replace('is-style-nav-pills', 'nav-pills')205 }206207 return clase;208 }209
210 setAttributes( { className: addnewClassName(attributes.className) } );
211 }
228
229 // Precargar nombre ID en hijos.
230 if( !attributes.anchor ){
231 function getRandomArbitrary(min, max) {232 return Math.floor(Math.random() * (max - min) + min);233 }234 setAttributes( { anchor: 'accordionChild' + getRandomArbitrary(10,150) } )
235 }
236
115
116 // Precargar nombre ID.
117 if( !attributes.anchor ){
118 function getRandomArbitrary(min, max) {119 return Math.floor(Math.random() * (max - min) + min);120 }121 setAttributes( { anchor: 'accordion' + getRandomArbitrary(10,150) } )
122 }
123
Function declarations (with the function
keyword) and variable declarations should preferably be in the root of a program or the body of a function.
Having nested function declarations inside blocks may have unexpected results at runtime due to hoisting.
As a rule of thumb, if you ever find yourself needing to use nested functions:
Prefer const f = () => ...
over function f() {...}
for functions inside block-statements.
When using function
or var
, do not have any declarations that can possibly be accessed outside the block in which they're declared.
Note: Block bindings (let
, const
) are not hoisted and therefore they are not affected by this rule.
function outer(test) {
if (test) {
// the declaration for "inner" can
// be accessed outside the if-statement
// only when `test` is truthy.
function inner() {
return "inner value";
}
inner();
}
// works only if `test` is true.
return inner();
}
outer(true); // "inner value"
outer(false); // TypeError: inner is not a function
inner
should be moved out of the if
block, or be declared with a const
keyword.
// When `inner` is needed outside the `if` block:
function outer(test) {
const inner = () => "inner value"
if (test) {
inner();
}
// always works.
return inner();
}
// When `inner` is not needed outside the `if` block:
function outer(test) {
if (test) {
const inner = () => "inner value"
inner();
}
return "outer value"
}