Working code sample.角度的意外行爲。自執行的函數調用範圍功能
瑣碎的標記:
<!DOCTYPE html>
<html ng-app="APP">
<head></head>
<body ng-controller="myController">
<script src="angular.min.js"></script>
<script src="controller.js"></script>
</body>
</html>
瑣碎的代碼示例:
angular.module('APP', []).controller('myController', function($scope) {
$scope.test = function() {
console.log('Weird behaviour!')
}
(function() {}()); //if you comment self-executing function console will be empty
});
而且很奇怪的範圍的行爲。你能解釋爲什麼會發生這種情況嗎?
我相信你知道這個j avascript特定的代碼風格...這裏是一個例子:jsfiddle.net/prfy9eso。值得注意的是,對於該示例的解釋以不同的方式工作(空行在第二個示例中更改代碼行爲,但不會更改角示例中的行爲)。 – Spirit
'return'[已知例外](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/return#Automatic_Semicolon_Insertion)。請注意關於「返回語句後無法訪問的代碼」的註釋,它僅涉及Firefox,但使用正確配置的linter可能會阻止這兩種情況。 – estus
還'拋出','繼續','打破'。 [spec](http://ecma-international.org/ecma-262/5.1/#sec-7.9) –