老司機(jī)匿名函數(shù)觸發(fā)(解決作用域的污染問題)
;(function(){
var a = 2
alert(a)
})()
閉包使用的案例(取值存值)
function user(name) {
var age, sex;
return {
getName: function() {
return name;
},
setName: function(newName) {
name = newName;
},
getAge: function() {
return age;
},
setAge: function(newAge) {
age = newAge;
},
getSex: function() {
return sex;
},
setSex: function(newSex) {
sex = newSex;
}
}
}
var whh = user('王花花');
whh.setSex('女');
whh.setAge(22);
var name = whh.getName();
var sex = whh.getSex();
var age = whh.getAge();
console.log(name, sex, age);
解構(gòu)賦值
var [a,b,c] = [12,34,56] //數(shù)組解構(gòu)賦值
alert(b) //34
string.includes() string.startsWith() string.endsWith() string.repeat()
面向?qū)ο缶幊毯兔嫦蜻^程的編程區(qū)別
ES6創(chuàng)建類class方法和屬性
class Star {
constructor(uname,age){
this.uname = uname
this.age = age
}
sing(song){
console.log(this.uname + song)
}
}
var ldh = new Star('李慷',10)
console.log(ldh)
ldh.sing('傳遞')
vue的基本使用
store.js
//適合存儲組件之間共享的數(shù)據(jù)
//響應(yīng)數(shù)據(jù)這個組件變化另一個也變化
//安裝:cnpm install vuex --save
import Vue from 'vue'
import Vuex from 'Vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
num:0,
number:12
},
//統(tǒng)一的計算屬性
getters:{
number(state){
return state.number
}
},
//相當(dāng)于組建的方法
mutations:{
//判斷形參
interadd(state,payload){
state.number += payload ? payload : 1;
}
}
})
組件中
<template>
<div class="hello">
{{$store.getters.number}}
<button @click="addFn()">添加</button>
<button @click="$store.commit('interadd',2)">添加</button>
</div>
</template>
<script>
export default {
computed:{
},
methods:{
addFn(){
this.$store.commit('interadd',2)
}
}
}
</script>