@code-char:"```"
code:before,code:after{
concent:@code-char;
background:red;
}javascript
typeof isNaN => function
if(typeof Array.isArray==="undefined"){ Array.isArray = function(arg){ return Object.prototype.toString.call(arg)==="[object Array]"; }; };
寄生構造函數[^1]
[^1]:——實際上是工廠模式的變種,只不過是放在其餘構造函數中建立對象php
function SpecialArray(){ var values = []; values.push.apply(values, arguments); values.toPipedString = function(){ return this.join("|"); }; return values; } var colors = new SpecialArray("red", "blue", "green"); alert(colors.toPipedString()); //"red|blue|green"
// 方式1、事件對象 for(var i = 0; i < dom.length; i++) { dom[i].onclick = function(e) { var e = window.e || e; var target = e.target || e.srcElement; return target; }; }; // 方式2、閉包實現 for(var i = 0; i < dom.length; i++) { dom[i].onclick = (function (index) { return function () { return index; }; })(i); }
var removeMore = function(arr) { var newArr = []; for(var i = 0; i < arr.length; i++) //遍歷當前數組 { //若是當前數組的第i已經保存進了臨時數組,那麼跳過, //不然把當前項push到臨時數組裏面 if (newArr.indexOf(arr[i]) === -1) newArr.push(arr[i]); } return newArr; }; var arr = [1,3,3,4,6,0,8,6]; //方法1 function unqiue(arr){ var json = {}; var result = []; for(var i=0;i<arr.length;i++){ //這裏會遍歷json的arr[i]屬性 由於json爲空 理所固然返回undefined //因此當if知足!undefined 即爲true的時候 給json的arr[i]的屬性的值設定爲1 //而後放入結果數組裏 因此當arr數組中有重複的值時 if不知足條件 就不會添加到數組了 if( !json[arr[i]] ){ json[arr[i]] = 1; result.push( arr[i] ); } } return result; } console.log(unqiue(arr)); //方法2 Array.prototype.unique1 = function() { var a = [];var l = this.length; for(var i = 0 ;i< l; i++) { for(var j = i + 1 ;j < l; j++) { if (this[i] === this[j]) {j = ++i} } a.push(this[i]); } return a; } console.log(arr.unique1()); //方法3 Array.prototype.unique3 = function() { var n = []; //一個新的臨時數組 for(var i = 0; i < this.length; i++) //遍歷當前數組 { //若是當前數組的第i已經保存進了臨時數組,那麼跳過, //不然把當前項push到臨時數組裏面 if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n; };
var json = {a:6,b:4,c:[1,2,3]}; function clone(obj){ var oNew = new obj.constructor(obj.valueOf()); if(obj.constructor === Object){ for(var i in obj){ oNew[i] = obj[i]; // 遞歸調用 if(typeof(oNew[i]) === 'object'){ clone(oNew[i]); } } } return oNew; }
// 將arr按age屬性降序排列 var arr = [ {"name": "wang","age": 18}, {"name": "liu","age": 34}, {"name": "wu","age": 57}, {"name": "qiu","age": 9}, {"name": "lie","age": 14} ]; // 1.冒泡排序法 function sequence(arr) { for(var i = 0; i < arr.length; i++) { for(var j = 0; j < arr.length; j++) { if(arr[i].age < arr[j].age) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }; }; return arr; }; // 2.數組的**sort**方法 function sequence(arr) { var down = function(x,y) { return (x.age < y.age) ? 1 : -1; }; arr.sort(down); return arr; } // 多屬性(鍵值)排序 var sequence = function(name,minor) { return function (o,p) { var a,b; if(o && p && typeof o === 'object' && typeof p === 'object') { a = o[name]; b = p[name]; if(a === b) { return typeof minor === 'function' ? minor(o,p) : 0; } if(typeof a === typeof b) { return a < b ? 1 : -1; } return typeof a < typeof b ? -1 : 1; } else { throw { name : 'error'; }; } }; };
var domList = document.getElementsByTagName(‘input’); var checkBoxList = [];//返回的全部的checkbox var len = domList.length; //緩存到局部變量 while (len--) { if (domList[len].type === ‘checkbox’) { checkBoxList.push(domList[len]); } };
var url="http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx"; var str=url.substr(url.indexOf("?")+1); var arr=str.split('&'); var obj={}; for (var i = 0; i < arr.length; i++) { var arri = arr[i]; //a=1 var i2=arri.split("=");//["a","1"] obj[i2[0]]=i2[1]; //obj["a"]="1"; };
function countByts(str) { if(!arguments.length || !s) return null; if("" === s) return 0; var len = 0; for(var i = 0; i < str.length; i++) { if(str.charCodeAt(i) > 255) len += 2; else len++; }; return len; }
if (!String.prototype.trim) { String.prototype.trim = function() { return this.replace(/^\s+/, "").replace(/\s+$/,"");//\s匹配空白字符:回車、換行、製表符tab 空格 } } // test the function var str = " \t\n test string ".trim(); alert(str == "test string"); // alerts "true"
function fn(){ console.log(arguments.callee==fn); // true console.log(fn.caller); // fn2 } function fn2(){ fn(); //此時fn.caller就指向了fn2這個函數 } fn2();
var getJson = function(str) { var item; var result = []; // 只有一個鍵值對 if(search.indexOf("&") < 0) { item = str.splite('='); result[item[0]] = item[1]; } // 多個鍵值對,以&符分開 else{ var splitArr = str.split('&'); for(var i = 0; i < splitArr.length; i++){ item = splitArr[i].split('='); result[item[0]] = item[1]; }; } return result; }
/* 異步加載script方案 */ function loadSctipt (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 { // 其餘瀏覽器 script.onload = function() { callback(); }; } script.src = url; document.body.appendChild(script); };
xhr.open('get','index.php');
(get能夠不設置)xhr.setRequestHeader('ContentType',text/html);
===>告訴瀏覽器以何種方式解析xhr.send(null);
xhr.status & xhr.statusText
xhr.getRequestHeader('Content-Type');
xhr.getAllRequestHeaders();
xhr.responseText; || xhr.responseXML;
// 建立一個xhr實例 var xhr = new XMLHttpRequest(); // 發起http請求 xhr.open('get','index.php'); xhr.send(null); // 接收服務器響應 xhr.onreadystatechange = function () { if(xhr.readyState === 4 && xhr.status === 200) { // 接收到結果 var result = xhr.responseText; } };
var script = document.createElement('script'); script.src = 'http://localhost:3000/?start=1&count=10'; // 服務端地址 document.body.appendChild(script); // 發送請求 /* 因爲腳本執行完成事後才能拿到數據 */ // 1. script.addEventListener('load',function(){ result = data; }); // 2.被動的方式 function callback(data) { result = data; };
方法 | 描述 |
---|---|
concat() | 鏈接兩個或更多的數組,並返回結果。 |
join() | 把數組的全部元素放入一個字符串。元素經過指定的分隔符進行分隔。 |
pop() | 刪除並返回數組的最後一個元素。 |
push() | 向數組的末尾添加一個或更多元素,並返回新的長度。 |
shift() | 刪除並返回數組的第一個元素 |
unshift() | 向數組的開頭添加一個或更多元素,並返回新的長度。 |
slice() | 從某個已有的數組返回選定的元素 |
reverse() | 顛倒數組中元素的順序。 |
sort() | 對數組的元素進行排序 |
splice() | 刪除元素,並向數組添加新元素。 |
toSource() | 返回該對象的源代碼。 |
toString() | 把數組轉換爲字符串,並返回結果。 |
toLocaleString() | 把數組轉換爲本地數組,並返回結果。 |
valueOf() | 返回數組對象的原始值 |
<!DOCTYPE> <a> <abbr> <acronym> <address> <applet> <area> <article> <aside> <audio> <b> <base> <basefont> <bdi> <bdo> <big> <blockquote> <body> <br> <button> <canvas> <caption> <center> <cite> <code> <col> <colgroup> <command> <datalist> <dd> <del> <details> <dfn> <dir> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <font> <footer> <form> <frame> <frameset> <h1> - <h6> <head> <header> <hgroup> <hr> <html> <i> <iframe> <img> <input> <ins> <keygen> <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <meta> <meter> <nav> <noframes> <noscript> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <rp> <rt> <ruby> <s> <samp> <script> <select> <small> <source> <span> <strike> <strong> <style> <sub> <summary> <sup> <table> <tbody> <td> <textarea> <tfoot> <th> <thead> <time> <title> <tr> <track> <tt> <u> <ul> <var> <video> <wbr>
// 1.組合式繼承 var o = {num : 123}; var obj = {age : 18}; for (var k in o) { obj[k] = o[k] }; //應用 Object.prototype.extend = function(obj) { for(var k in obj) { this[k] = obj[k]; } } // 2.原型繼承(不提倡,由於太慢) 經過修改原型對象來實現繼承 多繼承很難維護,提倡單繼承
實例對象 | 構造函數 | 原型對象 |
---|---|---|
基於構造函數實例化的對象 | 自定義構造函數 | 自定義構造函數原型對象 |
foo | Foo | Foo.prototype |
__proto__ | __proto__ | __proto__ |
prototype | constructor | |
內置 函數對象 | 內置 函數原型對象 | |
Function | Function.prototype | |
__proto__ | __proto__ | |
prototype | constructor | |
基於Object實例化的對象 | 內置 對象 | 內置 對象原型對象 |
obj | Object | Object.prototype |
__proto__ | __proto__ | __proto__ |
prototype | constructor |
set_key: function(params){ key = params; }
clear:both;
overflow:hidden;
的樣式
clearfix的類
:僞元素法;.clearfix::before, .clearfix::after{ content: "."; display: block; height: 0; line-height: 0; visibility: hidden; clear: both; }
display:table;
2)子盒子display:table-cell;vertical-align:middle;
img{vertical-align;} span{height:100%;display:inline-block;vertical-align: middle;}
position:absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); <!-- margin:-100px, 0, 0,-100px; --> <!-- 子盒子大小的一半 -->
border: 1px solid red; transform: scale(0.5) translate(-50%,-50%);
(::before,::after{content:'';display:block;})
function foo(id,name,age,gender) { }; var arr = /.+\((.*)\)/.exec(foo.toString); var newArr = arr[1].split(','); // newArr裏存的是形參的字符 // console對象的時候默認將其打印成tostring的形式
請求地址即所謂的接口,一般咱們所說的接口化開發,實際上是指一個接口對應一個功能,而且嚴格約束了請求參數和響應結果的格式,這樣先後端在開發過程當中,能夠減小沒必要要的討論,從而並行開發,能夠極大的提高開發效率,另一個好處,當網站進行改版後,服務端接口只須要進行微調。css
return arr.some(i => i>5) // 條件爲真時候返回
// 定義一個空數組 var newArr = []; arr.forEach(i => { if(arr[i] !== m){ newArr.push(arr[i]); } })
function getOuterHtml(id) { var ele = document.getElementById(id); var newNode = document.createChild('div'); document.appendChild(newNode); var clone = ele.cloneNode(true); newNode.appendChild(clone); return newNode.innerHTML; }
console.log(Array.prototype); // Array[0] console.log(String.prototype); // String console.log(Object.prototype) // Object console.log(Function) // none function Function() { [native code] } console.log(Number.prototype) // Number dom.getElementsByTagName() // HTMLCollection[3] dom.querySelectorAll() // NodeList[3] var form = document.forms[0]; console.log(form.elements); /* HTMLFormControlsCollection[2] 0:input 1:input length:2 password:input username:input __proto__:HTMLCollection */