對於一些重要的變量,但願可以封裝到某一內部做用域,避免其全局污染 (任意更改)。經過返回一個函數,該函數可以操縱內部變量,暴露出訪問的方法。數組
function fn1(){ var n =0; return function add() { n++; console.log(n); } } //實現了外部訪問內部變量。 let f =fn1(); f();//1 f();//2 f();//3
六大數據類型:null,undefined,string,boolean,number,symbol閉包
1.typeof粗略判斷:缺點:null,數組斷定均視爲對象 let a =[1,2,3]; //object let b={name:"xmj"};//object let c=null; //object let d ='eeee';//string 2.instanceof:原型鏈繼承判斷:原型鏈 null->object->([],function); 從下到上查找最近的父原型。eg:let x =[], x instanceof Array ==true 特色:能夠區分[]和{};[].__proto__ =Array.prototype; 即數組的instanceof是Array,而對象的最近父原型是Object; 3.constructor:相似instanceof 4.Object.ptototype.toString:能夠判斷全部的像function,obj,和[]的區別。