PROBLEM: There is a coding imperative (S. McConnel, Code Complete) that one shouldn't code on language, but by means of it, e.g. doing right style things even if language doesn't have some possibilities. Javascript is a script language in its heart, but it pretends being OOP very much. I want my Javascript and Java coding styles be better agreed on that basis.
QUESTION ONE: In Javascript methods I split code for simplicity and do encapsulation using closures. One large method turns into multiple inside its scope. Is it a bad practice?
QUESTION TWO: In Javascript method I declare variables first, then declare a method-constructor with a name of its parent-method, then go other methods-resources - like in Java classes. No script is floating directly in the body, except for one-line constructor call in the end of parent-method (void or return). When you open a closure method its main operations are atop. Any reasons why shouldn't I do that?
function parentFunction() {
var myVariable;
var scope = this; // Keep scope stable inside enclosed methods.
function parentFunction() {
// A pointless action to demonstrate an approach:
return (
sonFunction() ||
daughterFunction());
}
function sonFunction() {
return scope.name;
}
function daughterFunction() {
return scope.name;
}
return parentFunction();
}