javascript
解析和执行过程
var a = 1;
b = 2
function c(){
console.log('c')
}
var d = function(){
console.log('d')
}
console.log(a)
function a(){}
var a = 1
console.log(a)
b()
function b(){
console.log("1")
}
function b(){
console.log("2")
}
console.log(a)
console.log(b)
console.log(c)
console.log(d)
var a = 1;
console.log(a)
b = 2
console.log(b)
function c(){
console.log('c')
}
var d = function(){
console.log('d')
}
console.log(d)
作用域
let = 10
function fn(){
console.log(x)
}
function show(){
let x = 20
(function(){
fn()
})()
}
show()
变量提升
var foo = 111
function hoistVariable(){
if(!foo){
var foo = 5
}
console.log(foo)
}
hoistVariable()
var foo = 111
function hoistVariable(){
console.log(foo)
var foo = foo||5
console.log(foo)
}
hoistVariable()
函数提升
foo()
function foo(){
}
foo()
let foo = function(){
}
String
反转
function reverseString(str){
return str.split('').reverse().join('')
}
var str = 'abcdef'
reverseString(str)
function Stack(){
this.data = []
this.top = 0
}
Stack.prototype = {
push:function(element){
this.data[this.top++] = element
},
pop:function(){
return this.data[--this.pop]
},
length: function(){
return this.top
},
}
function reverseString(str){
let stack = new Stack()
let arr = str.split("")
for(let i=0;i<arr.length;i++){
stack.push(arr[i])
}
let result = ''
for(let i=0;i<arr.length;i++){
result += stack.pop()
}
return result
}
let str = 'abcde'
let result = reverseString(str)
function reverseString(str,pos,out){
if(pos<0){
return out
}
out += str.charAt(pos--)
return reverseString(str,pos,out)
}
let str = 'abcdefg'
let result = ''
result = reverseString(str,str.length,'')
查询出现次数最多字符
function getMax(str){
let json = {}
for(let i=0;i<str.length;i++){
let ch = str.charAt(i)
if(!json[ch]){
json[ch] = 1
}else{
json[ch] = json[ch]+1
}
}
console.log(json)
let maxChar = ''
let maxNum = 0
for(let key in json){
if(json[key]>maxNum){
maxNum = json[key]
maxChar = key
}
}
return maxChar
}
let str = 'xsadasxasdsad'
getMax(x)
数组
判断为空
let obj = {}
function isEmpty(obj){
for(let key in obj){
if(obj.hasOwnProperty(key)){
return false
}
}
return true
}
let arr = []
function isEmpty(arr){
if(arr.length===0){
return true
}else{
return false
}
}
function isEmpty(str){
if(str===''||str.trim().length===0){
return true
}else{
return false
}
}
instanceof
let arr = []
typeof arr
let obj = {}
typeof obj
arr instanceof Array
obj instanceof Object
function getDataType(data){
if(data instanceof Array){
return 'Array'
}else if(data instanceof Object){
return 'Object'
}else{
return 'Data not is Array or Object'
}
}
filter
let arr = [1,2,3,4,5,6,7,8,9]
function filterFun(x){
return x%2
}
arr.filter(filterFun)
reduce累加器
let arr = [1,2,3,4,5,6,7,8,9]
arr.reduce(function(accumulator,currentValue){
return accumulator+currentValue
})
自定义获取最小值&最大值
let arr = [1,2,3,4,5,6,7,8,9]
Array.prototype.min = function(){
let min = this[0]
let len = this.length
for(let i=0;i<len;i++){
if(this[i]<min){
min = this[i]
}
}
return min
}
arr.min()
Array.prototype.max = function(){
let max = this[0]
let len = this.length
for(let i=0;i<len;i++){
if(this[i]>max){
max = this[i]
}
}
return max
}
Array.prototype.min = function(){
return Math.min.apply(Math,this)
}
Array.prototype.max = funtion(){
return Math.max.apply(Math,this)
}
forEach
let arr = []
arr.forEach(function(element,index,array){
})
数组去重
let arr = [1,2,2,3,4,4,5,6,7,7,8,8,9,9]
function arrayRepeat(arr){
let result = [arr[0]]
arr.sort(function(a,b){
return a-b
})
for(let i=0;i<arr.length;i++){
if(arr[i]!=result[result.length-1]){
result.push(arr[i])
}
}
return result
}
Date
函数
let add = new Function('num1','num2','return num1+num2')
add(1,2)
var Person = (function(){
let _name = ''
return {
setName:function(){
return _name
},
setName:function(newName){
_name = newName
}
}
}())
Person.setName('xxx')
Person.getName()
函数调用
let obj = {
name: 'xxx',
getNmae:function(){
return name
},
setName:function(name){
this.name = name
return this
}
}
obj.getName()
obj['getName']()
obj.setNmae('xxx').getNmae()
function Person(name,age,sex){
this.name = name
this.age = age
this.sex = sex
}
Person.prototype.getName = function(){
return this.name
}
Person.prototype.setName = function(name){
this.name = name
}
let p = new Pserson('xxx')
function sum(num1,num2){
return num1 + num2
}
let Perosn = {}
sum.call(Person,1,2)
sum.apply(Person,[1,2])
匿名函数
(function(){
})()
arguments
function test(){
console.log(arguments)
}
test(1,2,3)
function aaa(){
console.log(arguments.callee === aaa)
a()
function a(){
console.log(arguments.callee === a)
}
}
function aa(){
return function(n){
if(n<=1){
return 1
}
return n * arguments.callee(n-1)
}
}
function sum(){
let arr = Array.prototype.slice.call(arguments)
return arr.reduce(function(pre,cur){
return pre+cur
},0)
}
sum(1,2)
sum(1,2,3)
sum(1,2,3,4)
闭包
function test(){
let max = 10
return function bar(x){
if(x>10){
console.log(x)
}
}
}
let f = test()
f(11)
let cacheMemory = (function(){
let cache = {}
return {
set : function(id){
if(id in cache){
return cache[id]
}
let result = fn(id)
cache[id] = result
return cache[id]
}
}
})()
function fn(id){
return 'xx'
}
var name = 'other'
let obj = {
name:'inner',
method:function(){
_this = this
return function(){
return _this.name
}
}
}
this
function Person(name){
this.name = name
}
let p = new Person('xxx')
console.log(p.name)
var value = 10
var obj = {
value:100,
method:function(){
var foo = function(){
console.log(this.value)
}
foo()
console.log(this.value)
}
}
obj.method()
var number = 10
function Person(){
number = 20
this.number = 30
console.log(number)
}
Person.prototype.getName = function(){
return this.number
}
var p = new Person()
console.log(p.number)
重新绑定对象
var value = 10
var obj = {
value:20
}
var method = function(){
console.log(this.value)
}
method()
method().call(obj)
method().apply(obj)
var newMethod = method.bind(obj)
newMethod()
闭包中的this
var user = {
sport:'xxxxx',
data:[
{name:'a',age:11},
{name:'b',age:11},
{name:'c',age:11},
],
clickHandler:function(){
this.data.forEach(person){
console.log(this)
console.log(person.name+":"+this.sprot)
}
}
}
user.clickHandler()
var user = {
sport:'xxxxx',
data:[
{name:'a',age:11},
{name:'b',age:11},
{name:'c',age:11},
],
clickHandler:function(){
var _this = this
this.data.forEach(person){
console.log(_this)
console.log(person.name+":"+_this.sprot)
}
}
}
call&apply&bind
function add(num1,num2){
return num1 + num2
}
function myAddCall(x,y){
return add.call(this,x,y)
}
function myAddApply(x,y){
return add.apply(this,[x,y])
}
function myAddBind(x,y){
var bindfn = add.bind(this,x,y)
return bindfn()
}
调用匿名函数
var animals = [
{species:'xx',name:'xxx'},
{species:'xx',name:'xxx'}
]
for(var i=0;i<animals.length;i++){
(function(i){
this.print = function(){
console.log(this.species)
}
this.print()
}).call(animals[i],i)
}
bind配合setTimeout
function LateBloomer(){
this.petalCount = Math.call(Math.reandom()*12)+1
}
LateBloomer.prototype.bloom = function(){
window.setTimeout(this.doclare.bind(this),1000)
}
LateBloomer.prototype.doclare = function(){
return this.petalCount
}
var s = new LateBloomer()
s.bloom()
对象属性
var person = {}
Object.definePrperty(person,"name",{
writable:false,
value:'xxxxx'
})
person.name
私有属性
let book = {
_year:2017,
edition: 1
}
对象
对象创建
let person1 = new Object()
person1.name = 'xxx'
let person2 = {
name:'xxx'
}
function createPerson(name,age,address){
let obj = new Object()
obj.name = name
obj.age = age
obj.address = address
obj.getName = function(){
return this.name
}
return obj
}
function Person(name,age,address){
this.name = name
this.age = age
this.address = address
this.getName = function(){
return this.name
}
}
function Person(name,age,address){
this.name = name
this.age = age
this.address = address
}
Person.prototype.getName = function(){
return this.name
}
function Person(name,age,address){
this.name = name
this.age = age
this.address = address
if(typeof Person._initialized === 'undefined'){
Person.prototype.getName = function(){
return this.name
}
Person._initialized = true
}
}
对象克隆
let origin = {}
let obj = origin
function showClone(origin){
let result = {}
for(let key in origin){
if(origin.hasOwnProperty(key)){
result[key] = origin[key]
}
}
return result
}
let origin = {}
let result = Object.assign({},origin)
let origin = {}
let result = JSON.pares(JSON.stringify(origin))
原型
function Person(){}
Person.prototype = {
constructor:Person,
name:'xx',
age:29,
sayName:function(){
return this.name
}
}
原型链
function Person(){
}
let person = new Person()
person.__proto__ === Person.prototype
Person.prototype.__proto__ === Object.prototype
Object.prototype.__proto__ === null
Function.prototype.a = 'a'
Object.prototype.b = 'b'
function Person(){}
let p = new Person()
p.a
p.b
hasOwnProperty
function Person(name){
this.name = name
}
let person = new Person('ccc')
Person.prototype.age = 12
person.hasOwnProperty('name')
person.hasOwnProperty('age')
继承
原型链继承
function Animal(name){
this.type = 'animal'
this.name = name || 'animal'
this.sleep = function(){
console.log(this.name+'is sleeping')
}
}
Animal.prototype.age = 5
Animal.prototype.eat = function(food){
console.log(this.name+"is eating "+food)
}
function Cat(name){
this.name = name
}
Cat.prototype = new Animal()
Cat.prototype.constructor = Cat
let cat = new Cat('xxx')
cat.type
cat.name
cat.sleep()
cat.age
构造函数继承
function Animal(name){
this.name = name
this.age = 15
this.sleep = function(){
console.log(this.name+'is sleeping')
}
}
Animal.prototype.eat = function(food){
console.log(this.name+"is eating "+food)
}
function Cat(name){
Animal.call(this)
this.name = name
}
复制继承
function Animal(name){
this.name = name
this.age = 15
this.sleep = function(){
console.log(this.name+'is sleeping')
}
}
Animal.prototype.eat = function(food){
console.log(this.name+"is eating "+food)
}
function Cat(name){
let animal = new Animal(name)
for(let key in animal){
if(animal.hasOwnProperty(key)){
this[key] = animal[key]
}else{
Cat.prototype.key = animal[key]
}
}
}
组合继承
function Animal(name){
this.name = name
this.age = 15
this.sleep = function(){
console.log(this.name+'is sleeping')
}
}
Animal.prototype.eat = function(food){
console.log(this.name+"is eating "+food)
}
function Cat(name){
Animal.call(this,name)
}
Cat.prototype = new Animal()
Cat.prototype.constructor = Cat
function Animal(name){
this.name = name
this.age = 15
this.sleep = function(){
console.log(this.name+'is sleeping')
}
}
Animal.prototype.eat = function(food){
console.log(this.name+"is eating "+food)
}
function Cat(name){
Animal.call(this,name)
}
(function(){
let Super = function(){}
Super.prototype = Animal.prototype
Cat.prototype = new Super()
Cat.prototype.constructor = Cat
})()
事件
事件级别
let ad = document.qquerySelector('#ad')
ad.onclick = function(){
console.log(1)
}
ad.addEventListener('click',function(){
console.log(2)
})
var customEvent;
(function(){
if(document.implementation.hasFeature('CustomEvents','3.0')){
var detailData = {name:'xxx'}
customEvent = document.createEvent('customEvent')
customEvent.initCustomEvent('myEvent',true,false,detailData)
}
})()
ad.addEventListener('myEvent',function(e){
console.log(e.detail)
})
ad.addEventListener('click',function(){
ad.disatchEvent(customEvent)
})
阻止冒泡
ad.addEventListener('click',function(event){
console.log('xxx')
event.stopPropagation()
})
阻止默认行为
事件委托
let oUl = document.querySelector('ul')
oUl.addEventListener('click',function(event){
let target = event.target
console.log(target.nodeName)
if(targe.nodeName.toLowerCase === 'li'){
console.log(target.innerText)
}
})
浏览器
ajax
function createXMLHttp(){
let xmlHttp = new XMLHttpRequest()
return xmlHttp
}
let data = {xx:'xx',aa:'aa'}
data= JSON.stringify(data)
let xhr = createXMLHttp()
xhr.open('post','/xx/xx',true)
xhr.setRequestHeader('Content-type','application/json;charset=UTF-8')
xhr.send(data)
xhr.onreadystatechange = function(){
if(xhr.readyState ==4 && xhr.status == 200){
console.log(xhr.responseText)
}
}
ajax进度事件
function createXMLHttp(){
let xmlHttp = new XMLHttpRequest()
return xmlHttp
}
let data = {xx:'xx',aa:'aa'}
data= JSON.stringify(data)
let xhr = createXMLHttp()
xhr.onloadStart = function(event){
console.log('onloadStart事件--开始接受数据')
}
xhr.onerror = function(event){
console.log('onerror事件')
}
xhr.ontimeout = function(event){
console.log('ontimeout事件')
}
xhr.onload = function(event){
let status = xhr.status
if(status>=200 && status<300 || status === 304){
console.log('onload事件--数据接收完成')
}
}
xhr.onloadend = function(event){
console.log('onloadend事件')
}
xhr.open('post','/xx/xx',true)
xhr.setRequestHeader('Content-type','application/json;charset=UTF-8')
xhr.send(data)
JSON序列化
- JOSN.stringify()
- JSON.parse()
跨域
|