您必须将我链接到 Papa 为我提出此建议的位置,但我只看到Papa 建议为“可绑定成员”执行此操作。他在谈论函数——而不是标量值。您正在尝试执行后者,这样做会遇到问题。
你看,提升将变量的声明移动到作用域的顶部,而不是它们的定义(又名assignments)。如果您在一个语句中声明和定义,则提升将其分成两个语句并提升前者。
所以这段代码:
var MainCtrl = function ($scope,$http) {
$scope.dumbValue = dumbValue;
var dumbValue = 'dumb';
}
与编写此代码相同(因为提升将其变为此):
var MainCtrl = function ($scope,$http) {
var dumbValue; // declared, not defined (hence the value `undefined`)
$scope.dumbValue = dumbValue; // assignment of `undefined` to a property of $scope
dumbValue = 'dumb'; // definition of your variable with a string value
}
然而,正如我所说,Papa 正在谈论将绑定到$scope
. 他说而不是这样做:
var MainCtrl = function ($scope,$http) {
function foo () {};
$scope.foo = foo;
}
做这个:
var MainCtrl = function ($scope,$http) {
$scope.foo = foo;
function foo () {};
}
这是有效的,因为提升将命名函数语句转换为将新变量定义为函数的变量声明,然后将它们拆分为两个单独的语句并将它们放在顶部。因此,Papa 推荐的版本(最后一个代码块)通过提升转换为:
var MainCtrl = function ($scope,$http) {
var foo;
foo = function () {};
$scope.foo = foo;
}
如您所见,这种对命名函数语句的不同处理允许foo
在绑定到$scope
发生之前将其定义为函数。
Personally, I don't like Papa's recommendation here. Although I personally understand hoisting and don't get bit by it, I have seen many developers create issues by writing code which gets changed in hoisting. This being the case, I am very particular to write my code exactly as the hoist would have converted it in order to avoid having my code misunderstood by the majority of developers. This is the way JSLint's default settings would have you write it as well.