@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 */