+function ($) { "use strict"; }(window.jQuery); 怎么理解?
匿名函數閉包
我們先來理一理函數表達式和函數聲明的區別
函數表達式: 函數可以是匿名函數,也可以有函數名,但是這種函數無法直接使用,只有通過表達式左側的變量來調用。
var a = function(){ alert('Function expression'); } var b = new a();
函數聲明:必需要有函數名
function a(){ alert('Function declaration'); } a();
下面是一個匿名函數
function () {
}
一元操作符+和函數表達式
也可以使用如-,~,!這種其它的一元操作符或者括號,目的是告訴解析器在這些特定操作符附近的是一個表達式(注意,JS將function當作一個函數聲明的開始,而函數聲明后面不能跟圓括號直接進行調用,所以有的時候想辦法將函數聲明轉化為函數表達式)
The following are three classical methods 三種典型方式進行上述轉換
+function () { }; (function () { }); void function() { };
The efficiency of these three kinds of
Operators addition and subtraction counter, operator "~" (bit reverse) the definition in the EMCAScript5: according to operator binding statement; the old value into a 32 bit integer; execution operator after the statement; conversion of the line for 32 bit and returns the result.
"()"Group operators: returns the expression results in the execution of the.
void: According to operator binding statement execution, return undefined.
The group operator also needs to perform the statement and returns the value returned by the contrast statement block, void multiple access block operation, thus the performance of void in this case is better than the group of operators.
吧拉粑粑拉粑粑拉吧拉吧拉吧沒看明白不好意思!
Through the end of the () to call and run 通過在表達式末尾加()運行
+function () {
}();
(funtion () {
})();
Parameter passing, in order to avoid the $and other library or template that conflict, window.jQuery passed as parameters.
參數傳遞,為了避免$和其他庫或模板發生沖突,jquery用這種方式起作用
+function (x) {
console.log(x);
}(3);
+function ($) {
}(window.jQuery);
Another advantage is that, within the scope of the JS interpreter, can quickly find $object than this method.
其它優勢在於,在JS解釋器中,這種方式下的效率更高
$(function(){
});
There is a way, to avoid naming conflicts.
避免沖突的另一種方式
var j = jQuery.noConflict();
j('#someDiv').hide();
$('someDiv').style.display = 'none';// Other libraries$
A look of writing something like the code. This code initializes the jQuery code in the DOM loading is complete.
這種寫法,代碼將在DOM加載完畢時初始化Jquery代碼
$(function(){
});
This is equivalent to the
和下面這種寫法作用相同
$(document).ready(function(){
// Initialize the jQuery code in the DOM loading is complete.
});
Different from 和這種不同
$(window).load(function(){
// In the picture, the media file is loaded, the initialization of jQuery code.
});
In strict mode 嚴格模式下的情況我暫時先不研究了,等日后水平上來再進一步學習
The establishment of "strict mode", mainly in the following: norms, efficiency, safety, facing the future
Remove the Javascript grammar some unreasonable, not rigorous, reduce some strange behavior;
- remove some safety code operation code, ensure operation safety,
- to improve the compiler efficiency, increase running speed,
- in order to pave the way for future versions of Javascript.
Enter "sign in strict mode", is the following statement:
"use strict";
The old version of the browser will send it as an ordinary string, ignore them.
The "use strict" in the first line of the script file, the script will be "strictly" mode of operation.
If this statement is not in the first row, is invalid, the script is run in "normal mode".
If the code files of different patterns was merged into one document, this requires special attention.
(strictly speaking, as long as the front is not to produce practical results of the sentence, "use strict" may not be in the first line, such as direct as in an empty semicolon. )
<script>
"use strict";
console.log("This is strict mode. ");
</script>
<script>
console.log("This is the normal mode. ");
</script>
The "use strict" in the first line of the function body, the entire function to "strict" mode of operation.
function strict(){
"use strict";
return "This is strict mode. ";
}
function notStrict() {
return "This is the normal mode. ";
}
Because calling the method above is not conducive to the files, so it is better, the following method, the script file in an anonymous function is executed immediately.
+function (){ "use strict";
}();
The scope of strict mode constraint
Var declare variables. In strict mode, variable must use the VaR command to display the statement, and then use the.
"use strict";
v = 1; // Error, V is not a statement
for(i = 0; i <2; i++) { // Error, I is not a statement
}
Numbers don't add 0. Disable the octal algorithm because... Because the octal is not included in the ECMAScript, the digital front 0 will change the meaning of numbers, JS will think is an octal number, thus error.
"use strict";
var sum = 015 + // Error! syntax error
197 +
142;
Do not allow the function non top, it is not allowed to function nesting. ECMAScript5 (or ECMAScript3) are not allowed.
"use strict";
if (true){
function f() { } // Error! syntax error
f();
}
for (var i = 0; i <5; i++){
function f2() { } // Error! syntax error
f2();
}
function baz(){ // kosher
function eit() { } // also kosher
}
Don't use these words do variables or parameters implements, interface, let, package, private, protected, public, static, yield. In strict mode as the reserved keywords. Future ECMAScript seems to cut into the pre.
function package(protected){ // Error!
"use strict";
var implements; // Error!
interface: // Error!
while (true){
break interface; // Error!
}
function private() { } // Error!
}
function fun(static) { 'use strict'; } // Error!
Console implementation of strict mode returns undefined
(function(){ return this; })()
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
(function(){ 'use strict'; return this; })()
undefined
There are other examples
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FFunctions_and_function_scope%2FStrict_mode
Document ready immediately
!function ($) {
$(function(){
})
}(window.jQuery)
問題二:
