數據類型、運算、對象、function、繼承、閉包、做用域、原型鏈、事件、RegExp、JSON、Ajax、DOM、BOM、內存泄漏、跨域、異步加載、模板引擎、前端MVC、前端MVVM、路由、模塊化、Http、Canvas、jQuery、ECMAScript 2015(ES6)、Node.js、AngularJS、Vue、React......
javascript
document.write只能重繪整個頁面 innerHTML能夠重繪頁面的一部分
(1) navigator.userAgent (2) 不一樣瀏覽器的特性,如addEventListener
簡單,Number,Boolean,String,Null,Undefined 複合,Object,Array,Function
var myvalue=」abcdefghi」; var jiequ=myvalue.substring(myvalue.length-4,myvalue.length); alert(jiequ);
-寫出下面的運算結果 alert(typeof(null)); // object alert(typeof(undefined)); // undefined alert(typeof(NaN)); // number alert(NaN==undefined); // false alert(NaN==NaN); // false var str="123abc"; alert(typeof(str++)); // number alert(str);// NaN
var x = 1, y = z = 0; function add(n) { n = n+1; } y = add(x); function add(n) { n = n + 3; } z = add(x); 一、undefined、undefined
//阻止冒泡的方法 function stopPP(e) { var evt = e|| window.event; //IE用cancelBubble=true來阻止而FF下須要用stopPropagation方法 evt.stopPropagation ?evt.stopPropagation() : (evt.cancelBubble=true); }
for(var i=0, j=0; i<10, j<6; i++, j++){ k = i + j; } 10
/*假設:一個英文字符佔用一個字節,一箇中文字符佔用兩個字節*/ function getBytes(str){ var len = str.length, bytes = len, i = 0; for(; i<len; i++){ if (str.charCodeAt(i) > 255) bytes++; } return bytes; } alert(getBytes("玩,as"));
function cloneObject(o) { if(!o || 'object' !== typeof o) { return o; } var c = 'function' === typeof o.pop ? [] : {}; var p, v; for(p in o) { if(o.hasOwnProperty(p)) { v = o[p]; if(v && 'object' === typeof v) { c[p] = Ext.ux.clone(v); } else { c[p] = v; } } } return c; };
\n alert(「p\np」);
function parseQueryString(url){ var params = {}, arr = url.split("?"); if (arr.length <= 1) return params; arr = arr[1].split("&"); for(var i=0, l=arr.length; i<l; i++){ var a = arr[i].split("="); params[a[0]] = a[1]; } return params; } var url = "http://witmax.cn/index.php?key0=0&key1=1&key2=2", ps = parseQueryString(url); console.log(ps["key1"]);
啓用GZIP壓縮 保持良好的編程習慣,避免重複的CSS,JavaScript代碼,多餘的HTML標籤和屬性
function say() { // Local variable that ends up within closure var num = 888; var sayAlert = function() { alert(num); } num++; return sayAlert; } var sayAlert = say(); sayAlert();//889
function proto(object) { return !object? null : '__proto__' in object? object.__proto__ : /* not exposed? */ object.constructor.prototype }
if (!Array.prototype.forEach) { Array.prototype.forEach = function(fn){ for ( var i = 0; i < this.length; i++ ) { fn( this[i], i, this ); } }; } ["a", "b", "c"].forEach(function(value, index, array){ assert( value, "Is in position " + index + " out of " + (array.length - 1) ); });
Object.prototype.slice.call(arguments);
var ninja = function myNinja(){ alert(ninja == myNinja); }; ninja(); myNinja(); true、報錯(error - myNinja is not defined.)
function getX(e){ e = e || window.event; //先檢查非IE瀏覽器,在檢查IE的位置 return e.pageX || e.clentX + document.body.scrollLeft; }
function getStyle(elem, name){ if(elem.style[name]){//若是屬性存在於style[]中,直接取 return elem.style[name]; } else if(elem.currentStyle){//不然 嘗試IE的方法 return elem.currentStyle[name]; }//嘗試W3C的方式 else if(document.defaultView && document.defaultView.getComputedStyle){ name = name.replace(/([A-Z])/g, "-$1");//W3C中爲textAlign樣式,轉爲text-align name = name.toLowerCase(); var s = document.defaultView.getComputedStyle(elem, ""); return s && s.getPropertyValue(name); }else{ return null; } }
function print_r(theObj) { var retStr = ''; if(typeof theObj == 'object') { retStr += '<div style="font-family:Tahoma; font-size:7pt;">'; for(var p in theObj) { if (typeof theObj[p] == 'object') { retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>'; retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>'; } else { retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>'; } } retStr += '</div>'; } return retStr; }
var b = parseInt("01"); alert("b="+b); var c = parseInt("09/08/2009"); alert("c="+c); b=一、c=0。
var foo = 'hello'; (function() { var foo= foo || 'world'; console.log(foo); })(); world
(1) 能夠開發前規定命名規範,根據不一樣開發人員開發的功能在函數前加前綴 (2) 將每一個開發人員的函數封裝到類中,調用的時候就調用類的函數,即便函數重名只要類名不重複就行
減小http請求次數:cssspirit,data uri
JS,CSS源碼壓縮
前端模板JS+數據,減小因爲HTML標籤致使的帶寬浪費,前端用變量保存AJAX請求結果,每次操做本地變量,不用請求,減小請求次數
用innerHTML代替DOM操做,減小DOM操做次數,優化javascript性能
用setTimeout來避免頁面失去響應
用hash-table來優化查找 當須要設置的樣式不少時設置className而不是直接操做style 少用全局變量 緩存DOM節點查找的結果 避免使用CSS Expression 圖片預載 避免在頁面的主體佈局中使用table,table要等其中的內容徹底下載以後纔會顯示出來,顯示比div+css佈局慢
Ajax請求總共有八種Callback
onSuccess onFailure onUninitialized onLoading onLoaded onInteractive onComplete onException
異步加載方式:
defer,只支持IE
async: 建立script,插入到DOM中,加載完畢後callBack,見代碼: function loadScript(url, callback){ var script = document.createElement("script") script.type = "text/javascript"; if(script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == "loaded" ||script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others: Firefox, Safari, Chrome, and Opera script.onload = function(){ callback(); }; } script.src = url; document.body.appendChild(script); }