Websocket同http同样都是是基于tcp的,可靠性的双向通讯协议,是创建在tcp之上的,而且是持久化的协议。css
相同点html
不一样点node
不一样点git
基本类型github
复杂类型web
值类型:面试
引用类型:ajax
特殊引用类型:function正则表达式
注意:typeof没法详细区分引用类型的类型,除了function. 只能准确区分值类型的类型
好比:算法
typeof {} //object typeof [] //object typeof null //object typeof console.log // function
function是一个比较特殊的类型,因此typeof可以区分
尽量使用 === 缘由以下
== 的使用状况,可参考
var obj = {}; if( obj.a == null ){ //这里相对于:obj.a === null || obj.a === undefined 的简写形式,JQ源码的推荐写法 }
function fn( a, b){ if( b == null ){ //这里至关于 b===null || b === undefined 的简写 } }
从纯JS的角度看,JSON就是对象,而且只有两个API
JSON.stringify({a:10,b:30}) //将对象转为字符串 JSON.parse('{"a":10,"b":30}') //将JSON格式的字符串转为 对象
var a = 100; console.log( !!a ); //true
可参看这篇博文:https://github.com/youngwind/...
//该方法最简单,效果通常,每一个元素仍然有很大机率在它原来的位置附近出现 arr.sort(function () { return Math.random() - 0.5; });
//Fisher–Yates shuffle费雪耶兹随机置乱算法) !!!推荐
//算法思想:从0~i(i的变化为 n-1到0递减)中随机取得一个下标,和最后一个元素(i)交换。 var arr = [5,8,59,56]; function shuffle(arr) { var i = arr.length, t, j; while (i) { j = Math.floor(Math.random() * i--); t= arr[i]; arr[i] = arr[j]; arr[j]= t; } } shuffle(arr) console.log(arr);//[56, 8, 5, 59]
原型链是针对构造函数的,好比我建立了一个函数并经过变量new了一个函数,那这个函数就会继承建立处理函数的属性,若是访问这个函数的属性时,并无在new处理的变量中写该属性,那么就会往上,根据protype逐级向上查找,这个查找的过程就叫原型链。
因为它的隐式原型等于它的显式原型,因此也会去 prototype 中去找。
function Foo(name,age){ this.name = name; this.age = age; } var foo = new Foo('h1',25); var foo2 = new Foo('h1',250); console.log(foo,foo2); //循环对象自身的属性 var item; for( item in foo) { //只遍历对象自身的属性,过滤掉该对象的显式原型 if(foo.hasOwnProperty(item)) { console.log(item) } }
var arr = [1,2,3]; console.log(Array.isArray(arr)); //true //instanceof运算符用于测试构造函数的prototype属性是否出如今对象的原型链中的任何位置 console.log( arr instanceof Array) //true
function Elem(id){ this.dom = document.getElementById(id); } Elem.prototype.html = function(val){ var dom = this.dom; if(val){ dom.innerHTML = val; return this; //用来链式调用 }else{ return dom.innerHTML; } } Elem.prototype.on = function(type ,fn){ var dom = this.dom; dom.addEventListener( type , fn); } var h1 = new Elem('h1'); h1.html("你被修改了").on('click', function(){ console.log(this) })
`做用域是针对变量的,好比我建立了一个函数,这个函数中包含了另一个函数。那么该变量中就有3个做用域
全局做用域》函数做用域》内层函数的做用域
做用域的特色就是,先在本身的变量范围中查找,若是找不到,就会沿着做用域往上找。
`
注意:this要在执行时才能确认值,定义时没法确认
function f1(name,age){ console.log(name,age) console.log(this); //this为x对象 } f1.apply({x:'我是this'}, ["seek",20]); f1.call({x:'我是this'}, "seek",20); //使用bind改变this时,需用函数表达式 var f1 = function (name,age){ console.log(name,age) console.log(this); //this为x对象 }.bind('我是被绑定的this') f1("seek",20)
`当一个函数的返回值是另一个函数,而返回的那个函数若是调用了其父函数内部的其它变量,
若是返回的这个函数在外部被执行,就产生了闭包。
表现形式:使函数外部可以调用函数内部定义的变量。`
闭包的使用场景
函数做为返回值
function fn(){ var a = 10; return function(){ console.log(a); //a是自由变量,从父做用域开始找。 } } var f1 = fn(); var a = 20; f1(); //10
函数做为参数来传递
function fn(){ var a = 10; return function(){ console.log(a); } } var fn1 = fn(); function fn2(fn){ var a =20; fn(); } fn2(fn1); //10
var str,a; for( a=0; a<10;a++){ str = document.createElement("a"); str.innerHTML = a + "点我" + "<br/>"; document.body.appendChild(str); (function(a){ str.addEventListener("click",function(e){ e.preventDefault(); console.log(a) }) })(a) }
同步是阻塞模式,异步是非阻塞模式。
var arr= [2,3,9,0];
arr.forEach(function(item,index){ console.log(item) // 2390 console.log(index) //0123 })
var result = arr.every(function(item,index){ if(item < 4) { return true; } }) console.log(result); //false, 由于9并不小于4
var result = arr.some(function(item,index){ if(item < 4) { return true; } }) console.log(result); //true 由于2,3,0小于4
var result = arr.sort(function(a,b){ // return a-b; //正序 return b-a; // 倒序 // return return Math.random() - 0.5; //最简单的随机数组排序,并不推荐 }) console.log(result); // [9, 3, 2, 0]
//map适用范围仍是较广的,学会思考 var result = arr.map(function(item,index){ return '<h1>' + item + '</h1>'; }) console.log(result); // ["<h1>2</h1>", "<h1>3</h1>", "<h1>9</h1>", "<h1>0</h1>"]
var result = arr.filter(function(item,index){ if(item >=3){ return true; } }) console.log(result); // [3, 9]
function formatDate(dt) { if (!dt) { //若是不传参数,则默认为当前时间 dt = new Date(); } var year = dt.getFullYear(); var month = dt.getMonth() + 1; var day = dt.getDate(); if (month <= 10) { month = '0' + month; } if (day <= 10) { day = '0' + day; } return year + '-' + month + '-' + day; } var date = formatDate(); console.log(date); //2019-03-23
var random = Math.random(); random = random + '0'.repeat(10); //repeat 重复10个0, 防止随机数出现少于10位数的状况 random = random.slice(0,10) console.log(random); //0.70728618 每次返回的只有10位数的字符串
function foreach(info, fn) { //数组处理 if(info instanceof Array) { info.forEach(fn) }else{ //对象处理 for( key in obj){ fn(key, obj[key]) } } } //使用方法 var obj = {x: '我是x',y: '我是y'}; foreach(obj, function(key,value){ console.log(value); //我是x,我是y }) var arr = [5,8,9]; foreach(arr, function(elem,index){ console.log(elem);//5,8,9 })
function bindEvent(elem,type,fn){ elem.addEventListener( type ,fn) } //使用方法 bindEvent(id,'click', function(e){ console.log(e) }) bindEvent(a,'click', function(e){ e.preventDefault(); //阻止默认事件 })
//使用代理,由父级帮忙去作 <div id="div1"> <a href="#">a1</a> <a href="#">a2</a> <span>ddd</span> <a href="#">a5</a> <!-- 动态加载更多 --> </div> div1.addEventListener('click', function(e){ if (e.target.nodeName == "A"){ alert(e.target.innerHTML) } })
//HTML结构 <div id="div1"> <a href="#">a1</a> <a href="#">a2</a> <span>ddd</span> <a href="#">a5</a> <!-- 动态加载更多 --> </div> <div id="div2">不使用代理</div> // function bindEvent(elem,type,selector, fn){ if(fn == null){ fn=selector; selector =null; } elem.addEventListener( type ,function(e){ var target; if(selector){ target = e.target; //matches() 方法用于检测字符串是否匹配给定的正则表达式。 if(target.matches(selector)){ fn.call(target,e); } }else{ fn.call(e); } }) } //使用代理 bindEvent(div1,'click','a',function(e){ console.log(this) }) //不使用代理 bindEvent(div2,'click',function(e){ //call改变了this指向为e console.log(this.toElement.innerHTML) })
<img src="" alt=""> //用于打点统计 <link rel="stylesheet" href=""> //使用CDN <script></script> // 使用JSONP
JS中,在函数外部没法访问函数内部的值,使用闭包就能够作到。
优势:
缺点
Cache-Control: http1.1推出,指文件缓存的有效期。