diff --git "a/\346\233\276\351\271\217/\344\275\234\344\270\232/10-11\344\275\234\344\270\232.md" "b/\346\233\276\351\271\217/\344\275\234\344\270\232/10-11\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1416295de33b51db82c38cb2245a307e318543ff --- /dev/null +++ "b/\346\233\276\351\271\217/\344\275\234\344\270\232/10-11\344\275\234\344\270\232.md" @@ -0,0 +1,78 @@ +``` + + + + + + + Document + + + + + +``` \ No newline at end of file diff --git "a/\346\233\276\351\271\217/\344\275\234\344\270\232/\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/\346\233\276\351\271\217/\344\275\234\344\270\232/\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..df2922667b667af35eb67aa46539d8d3c0e61e8d --- /dev/null +++ "b/\346\233\276\351\271\217/\344\275\234\344\270\232/\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,57 @@ +``` +var text ='I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.'; +//将上述字符串去除标点空格后统计有多少个单词 +const arr=text.split(""); +const arr1 =arr.filter( + function(i){ + if(i!=','&&i!='.'){ + return i + } + } +) +var text1= arr1.join(""); +const arr3=text1.split(" "); +console.log('单词一共有'+arr3.length+'个'); + + + +const arr4 = [87,85,74,70,43,59,65] + +// 0.将元素65 与43 调换位置 +// arr4.splice(4,3,65,59,43); +// console.log(arr4); + +// 1.移除第一个元素87,并在开头添加 元素86 +// arr4.shift(); +// arr4.unshift(86); +// console.log(arr4); + + +// 2.移除最后一个元素43, 并在末尾添加1 +// arr4.pop(); +// arr4.push(1); +// console.log(arr4); + +// 3.在 70 65 之间插入 元素 68 +// arr4.splice(4,5,43,59,68,65); +// console.log(arr4); + +// 4.删除元素65 +// arr4.pop(); +// console.log(arr4); + +// 5.使用map返回一个新的数组new_arr,要求新数组new_arr比原来数组大2 +const arr5= arr4.map( + function(i){ + return i*2 + } +) + +// 6.筛选数组new_arr返回new_arr1,要求new_arr1: 能被2整除 +const arr6=arr4.filter( + function(i){ + if(i%2==0){return i} + } +) +console.log(arr6); +``` \ No newline at end of file diff --git "a/\346\233\276\351\271\217/\347\254\224\350\256\260/10-11 \345\207\275\346\225\260.md" "b/\346\233\276\351\271\217/\347\254\224\350\256\260/10-11 \345\207\275\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..d05c2395959111be8892ad06e615bed8913f0583 --- /dev/null +++ "b/\346\233\276\351\271\217/\347\254\224\350\256\260/10-11 \345\207\275\346\225\260.md" @@ -0,0 +1,135 @@ +## JavaScript 函数 + +**A.基本语法(function 关键字)** +function 函数名([参数1],[参数2]....){ + 函数体 + [retrun] //返回值 +} + +``` +function a(){ + alert(1) +}; +``` + +**B.字面量定义的形式(匿名函数)** +var 变量=function ([参数1],[参数2]....){ + 函数体 + [retrun] //返回值 +} + +``` +var a=function(){ + alert(1); +``` + +### 二、函数的调用方式 + +**A.函数名() 、变量名();** + +``` +function a(){ + alert(1) +}; +a(); + +var a=function(){ + alert(1); +} +a(); +``` + +***\*B.直接调用执行;\**** + +``` +(function () { + alert(1); +})(); +//结果 1 +``` + +##### 箭头函数 + +允许我们用更短的语法定义函数。箭头函数可用于替代传统函数function() {}。 + +语法 +允许写的函数,在2号取为参数a和b,然后返回的总和。 + +作为常规功能: + +function sum(a, b) { + return a + b +} +现在,作为箭头函数: + +``` +const sum = (a, b) => a + b +``` + +使用箭头函数时,参数位于一对括号中的开头。然后是 a =>,它声明了一个箭头函数。之后,您将输入{}您希望运行的任何代码。但是,在返回值时,您可以排除花括号。 + +如果只有一个参数,您可以排除它周围的括号。 + +``` +const addOne = num => num + 1 +``` + + +如果您的函数没有参数,您只需使用一对空括号 + +``` +const say Hello = () => { + console.log("Hello") +} +``` + + + + +## 数组解构 + +**1、数组解构就是能快速提取数组中的指定成员(数组的某一项值或所有的值)** + +例如: + +**解构赋值都是一一对应的,按照顺序。解构赋值都是一一对应的,按照顺序。 + +``` +const arr = [200,300,400] +const [a,b,c] = arr +console.log(a,b,c) // 200,300,400 +也可以取数组的某一项值(结构必须保持一致) +``` + +``` +const arr = [200,300,400] +const [, , c] = arr +console.log(c) // 400 +``` + +还可在用“...”的方式提取所有的成员(注意的是这种...的写法只能在解构成员的最后一个成员使用)代码如下 + +``` +const arr = [200,300,400] +const [a,...all] = arr +console.log(all) // [300,400] 会返回得到一个最后所有的数组 +``` + +如果提取的解构成员小于数组的长度,就会从前到后的顺序来提取,代码如 + + + +``` +const arr = [200,300,400] +const [a] = arr +console.log(a) // 200 按顺序提取第一个 +``` + +如果提取成员大于数组长度,那么最后的提取的最后是undefined,代码如下 + +``` +const arr = [200,300,400] +const [a,b,c,d] = arr +console.log(d) // undefined +``` +