js基础强化
2020-08-31 本文已影响0人
五四青年_4e7d
老司机匿名函数触发(解决作用域的污染问题)
;(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);
解构赋值
var [a,b,c] = [12,34,56] //数组解构赋值
alert(b) //34
string.includes() string.startsWith() string.endsWith() string.repeat()
面向对象编程和面向过程的编程区别
ES6创建类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
//适合存储组件之间共享的数据
//响应数据这个组件变化另一个也变化
//安装: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
},
//统一的计算属性
getters:{
number(state){
return state.number
}
},
//相当于组建的方法
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>