Заметил проблему, интересно с чем связано:
let App = App || {};
App.init = function() {
console.log('Inited');
}
App.init(); // Uncaught ReferenceError: App is not defined (с const то же самое)
var App = App || {};
App.init = function() {
console.log('Inited');
}
App.init(); // 'Inited'
Во втором случае App инициализирован и имеет undefined, а в первом случае у тебя ошибка в первой строке о неинициализированной переменной.
Берем и читаем стандарт.
Переменные Let и Const создаются в запущенном контексте и недоступны ровно до тех пор, пока не будут вычислены их значения при присваивании. Если же вычисления нет, то по умолчанию им присваивается undefined.
Переменные Var создаются в контексте и сразу же инициализируются значением undefined, только потом происходит их вычисление.
13.3 Declarations and the Variable Statement
13.3.1 Let and Const Declarations
NOTE let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.
13.3.2 Variable Statement
NOTE A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. Within the scope of any VariableEnvironment a common BindingIdentifier may appear in more than one VariableDeclaration but those declarations collective define only one variable. A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.
Продвижение своими сайтами как стратегия роста и независимости