首页 > 其他分享 >es6 解构赋值 扩展运算符 字符串模板 等

es6 解构赋值 扩展运算符 字符串模板 等

时间:2023-01-03 17:03:53浏览次数:37  
标签:es6 console log ... 解构 运算符 let 字符串 name


解构赋值

<template>
<div>
<h1>解构赋值</h1>
</div>
</template>

<script>
export default {
name: "demo4",
mounted(){
//以前
// let a=0;
// let b =1;
// let c =2;
//现在 数组解构
let [a,b,c]=[0,1,2]
console.log(a) //0
console.log(b) //1
console.log(c) //2
//注意结构赋值的方法 等号两边的结构必须一样

//2.设置默认值
let [aa,bb='world']=['hello'];
// console.log(aa+bb) //helloworld

//3 对象解构
let {foo,bar} = {foo:'hello',bar:'world'}
console.log(foo+bar) //helloworld
//注意点变量名 等号左右也必须统一

//4 字符串的解构
const [a1,b1,c1,d1,e1]='hello'
console.log(a1) //h
console.log(b1) //e
console.log(c1) //l
console.log(d1) //l
console.log(e1) //o
}
}
</script>

<style scoped>

</style>

扩展运算符

<template>
<div>
<h1>扩展运算符</h1>
</div>
</template>

<script>
export default {
name: "demo5",
mounted(){
function win(...arg) {
console.log(arg[0]) //1
console.log(arg[1]) //2
console.log(arg[2]) //3
}
win(1,2,3)

//拷贝数组 不改变原数组 添加删除值
let arr1 = ['我','是','小羽'];
let arr2 =[...arr1];
arr2.push('hello world');
console.log(arr2); //["我", "是", "小羽", "hello world"]
console.log(arr1); //["我", "是", "小羽"]

//rest 运算符... 剩余参数不确定
function win2(first,...arg) {
// console.log(arg.length) //6 剩余参数
//打印出每项
for(let val of arg){
console.log(val)
}
}
win2(0,1,2,3,4,5,6)


es6数组展开
let arr = [1,2,3,4]
console.log(...arr)


合并数组
let arr1=[1,2,3]
let arr2=[4,5,6]
let arr3=[...arr1,...arr2] //1,2,3,4,5,6


es6 参数收集
function show(a,b,...arr){
console.log(a,b,arr)
}
show(1,2,6,7,3,4); //1,2[6,7,3,4]
剩余参数必须是最后一个
}
}
</script>

<style scoped>

</style>

字符串模板

<template>
<div>
<h1>字符串模板</h1>
</div>
</template>

<script>
export default {
name: "demo6",
mounted(){
//过去
let name='我是王文琪';
let say='很高兴认识你们'
let my=name+'今年18,'+say
//console.log(my) //我是王文琪今年18,很高兴认识你们

//es6
let my2 =`${name}今年18,${say}`
//console.log(my2) //我是王文琪今年18,很高兴认识你们

//可换行 可加html标签
let my3 =
`${name}
<b>今年18,</b>
${say}`
document.write(my3)

//字符串查找
let food = '西瓜'
let foods='你爱吃的水果有:苹果,香蕉,葡萄,西瓜,百香果'
//console.log(foods.includes(food)) //true

//重复打印
//document.write('winter|'.repeat(3)) winter打印三次
}
}
</script>

<style scoped>

</style>

startsWith endWith

startsWith()方法确定字符串是否以指定字符串的字符开头,返回​true​​false​视情况而定。

判断字符串以什么开头一般的话是用于来判断是否以http://开头或者以file:///开头

参数

接受两个参数

第一个参数,要在此字符串开头搜索的字符,第二个参数是指定从字符串开始的位置,默认从零开始。

注意:

此方法区分大小写;

let str1 = "file:///C:/Users/iTAze/Desktop/1.html";
let str2 =
console.log(str1.startsWith("https://"))// false;
console.log(str1.startsWith("file:///"))// true;
console.log(str2.startsWith("https://"))// true;
console.log(str2.startsWith("file:///"))// false;

endsWith()方法

endsWith()方法和startsWith()方法的语法都是一样的,不过endsWith()方法是从字符串的末尾开始查找。

比如你要判断这个字符串是不是以 .png .jpg 等这种

let str1 = "file:///C:/Users/iTAze/Desktop/1.html.png";
let str2 =
console.log(str1.endsWith(".png"))// true;
console.log(str1.endsWith(".jpg"))// false;
console.log(str2.endsWith(".png"))// false;
console.log(str2.endsWith(".jpg"))// true;

这个方法在IE中完全不兼容,在Edge中是兼容的,如果你想用这个方法请寻找兼容写法。


标签:es6,console,log,...,解构,运算符,let,字符串,name
From: https://blog.51cto.com/u_12422954/5986140

相关文章

  • 运算符和表达式
    一、运算符1.含义C语言中,数据是程序处理的对象,运算是对数据进行加工的过程,体现数据之间的各种不同运算关系的符号就称为运算符。C语言中,除了控制语句和输入输出以外的几乎......
  • 字符串 ES6 方法和正则相关方法
    JavaScript内部字符是以UTF-16的格式进行存储;每个字符固定2个字节;对于哪些需要4个字节存储的(unicode码大于0xFFFF的字符),JavaScript会认为它们是2个字符;如上土下口(吉的......
  • 运算符
    基本运算符算术运算符:+,-,*,/,%,++,--赋值运算符:=关系运算符:>,<,>=,<=,==,!=,instanceof逻辑运算符:&&,||,!位运算符:&,|,^,~,>>,<<,>>>条件运算符:?扩展赋值运算符:+=,-=,*=,/=各运算符优先......
  • 深入浅出es6模块化
      天下苦CommonJs久矣EsModule的独特之处在于,既可以通过浏览器原生加载,也可以与第三方加载器和构建工具一起加载。支持Esmodule模块的浏览器可以从顶级......
  • 运算符
    (1)算术运算符:+、-、、/、%、++、--(2)关系运算符:<,>,==,<=,>=,!=,得到的值是boolean类型(3)逻辑运算符:||、&&、!Boolean非“!”或:其中一个条件为真就为真1、短路或“||”......
  • 20、前端基础-ES6---模块化
    ......
  • JavaScript 运算符-运算符,算数运算符,递增,递减,逻辑运算符,赋值运算符,运算符的优先级
    JavaScript运算符-运算符,算数运算符,递增,递减,逻辑运算符,赋值运算符,运算符的优先级目录JavaScript运算符-运算符,算数运算符,递增,递减,逻辑运算符,赋值运算符,运算符的优先级1......
  • C语言中的单目运算符
    !逻辑反操作 - 负值+ 正值&取地址sizeof操作数的类型长度(以字节为单位)~ 对一个数的二进制按位取反-- 前置--、后置--++前置++、后置++*  间接访问操作符(解引......
  • 19、前端基础-ES6---异步编排
    ......
  • 18、前端基础-ES6---数组的map&reduce
    ......