搜索 K
Appearance
Appearance
3
分钟668
字2022-03-20
Invalid Date
const a = 1
let res
switch (a) {
case 1:
res = 'one'
break
case 2:
res = 'two'
break
// 超级多...
}
console.log(res) // one
const a = 1
const map = {
'1': 'one',
'2': 'two',
// ...
}
let res = map[a]
const a = 1
if (a === 1 || a === 2 || a === 3) {
// ...
}
const a = 1
if ([1, 2, 3].includes(a)) {
// ...
}
const fn = (name) => {
name = name || 'lhd'
console.log(name)
}
const fn = (name = 'lhd') => {
console.log(name)
}
const obj = {
eat: {
breakfast: 'egg',
dinner: 'meal'
}
}
console.log(obj.eat.breakfast) // egg
console.log(obj.eat.dinner) // meal
const {
eat: {
breakfast,
dinner
}
} = obj
console.log(breakfast) // egg
console.log(dinner) // meal
let player
let no = 24
if (no === 24) {
player = 'kobe'
} else {
player = 'james'
}
let no = 24
const name = no === 24 ? 'kobe' : 'james'
const a = 1
const fn = () => {
if (a === 1) {
return 1
} else if (a === 2) {
return 2
} else if (a === 3) {
return 3
}
}
const a = 1
const fn = () => {
if (a === 1) {
return 1
}
if (a === 2) {
return 2
}
if (a === 3) {
return 3
}
}
if (key === null) {
// 进行对应操作
}
if (key) {
// 进行对应操作
}
如果 selects 是不需要响应式的,但是你放在这里,会进行响应式的处理,浪费性能
data() {
return {
selects: [
{label: '选项一', value: 1},
{label: '选项二', value: 2},
{label: '选项三', value: 3}
]
};
}
放在外面,则不会进行响应式处理,节省性能
data() {
// 放在这
this.selects = [
{label: '选项一', value: 1},
{label: '选项二', value: 2},
{label: '选项三', value: 3}
]
return { };
}
export default{
data(){
timer:null
},
mounted(){
this.timer = setInterval(()=>{
//具体执行内容
console.log('1');
},1000);
}
beforeDestory(){
clearInterval(this.timer);
this.timer = null;
}
}
小程序不像 React,小程序的 setData 并没有做多次修改自动合并,所以需要我们手动合并
this.setState({
name: 'lhd'
})
if (confition1) {
this.setState({
age: 22
})
}
if (condition2) {
this.setState({
gender: '男'
})
}
const model = {
name: 'lhd'
}
if (confition1) {
model.age = 22
}
if (condition2) {
model.gender = '男'
}
// 合并更新
this.setData(model)
// test2 test3 都是函数
// 值为1时调用 test1(),否则调用 test2()
var temp = 1
if(temp == '1'){
test1()
} else {
test2()
}
var temp = 1
(temp=='1' ? test1:test2)()
let test1,test2,test3
test1 = 1
test2 = 2
test3 = 3
let [test1,test2,test3] = [1,2,3]