Blogger Information
Blog 26
fans 0
comment 3
visits 17466
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0104作业
后网络时代
Original
768 people have browsed it

1. 实例演示变量与常量的区别

答:变量与常量都是标识符,命名规则一致,变量定义可以现在声明,再赋值,也可以声明的时候初始化赋值,在ES6中新增了let关键字来声明变量,作用与var相似,但其所声明的变量只在声明所在块内有效,ES6标准中引入了新的关键字const来定义常量,常量一经定义不能更改。
变量定义:

  1. let user;//只声明
  2. console.log(user);//undefined
  3. let user="zhanghsan"; //变量定义
  4. user="lisi";//可以更改

常量定义:

  1. const APP='php递归学习';
  2. APP="重新赋值";//会报错

2. 函数与匿名函数的区别

答:函数定义:

  1. function functionName(){
  2. console.log('ok');
  3. }

声明会自动提升,无论在其实定义前后调用都不会出错
functionName();
匿名函数定义:

  1. let getName=function(){
  2. console.log('okay');
  3. }

声明不会自动提升,只能在其后调用:
getName();

3. 箭头函数的参数特征

答:箭头函数是用来简化匿名函数的声明的,参数特征:
如果函数体只有一条语句,则return 可省了
如果只有一个参数则小括号可省略
如果没有参数,小括号保留:

4. 闭包原理与实现并演示它

答:闭包是能够访问自由变量的函数,理论上都是闭包函数。
自由变量:既不是函数的参数变量,也不是函数内部定义的私有变量,存在于函数调用的上下文中,伴随着函数调用的全程,具有相对性。
demo1:

  1. let name="自由变量"
  2. function wrap(a,b){
  3. let c=1;
  4. return (a+b+c)+name;
  5. }

demo2:利用闭包访问私有变量
函数的私有变量做自由变量:

  1. function wrapF(){
  2. let wr="私有";//自由变量
  3. return function(){
  4. return "访问私有变量的闭包:"+wr;
  5. }
  6. }

5. 四种高阶函数,全部实例演示,并理解回调和纯函数是什么

答:回调函数:作为参数传递给其他的函数或对象执行的函数,自身并不独自执行,

html页面:

  1. <button id="clickMe">点击测试</button>

demo1:

  1. document.getElementById('clickMe').addEventListener("click",hello);//回到函数调用
  2. function hello(){
  3. alert("hello world!");
  4. }
  5. demo2
  6. function numCompare(a,b){
  7. return a-b;
  8. }
  9. let num=new Array(1,2,3,4);
  10. sortNum=num.sort(numCompare);//回到函数调用

2。偏函数:
函数的参数,一部分由自己定义处理,另一部分交由它之中子函数(一般是匿名函数)处理:
简化了声明和调用

  1. let xyz=function(a,b){
  2. return function(c,d){
  3. return a+b+c+d;
  4. }
  5. }
  6. //外层参数定义:
  7. let am1=[1,2],am2=[3,4];
  8. xyz(...am1)(...am2)

柯里化:

  1. let laypar=function (a){
  2. return function(b){
  3. return function(c){
  4. return function(d){
  5. return a+b+c+d;
  6. }
  7. }
  8. }
  9. }
  10. laypar(10)(20)(30)(40)//简化了调用参数
  11. #### 纯函数:完全独立于上下文,返回值只受传入参数影响;
  12. function sum(a,b){
  13. return a+b;
  14. }

箭头函数:
匿名函数

  1. let nonF=function(a,b){
  2. return a+b;
  3. }

简化匿名函数的声明

  1. nonF=(a,b)=>{
  2. return a+b;
  3. }

如果函数体只有一条语句,则return 可省了

  1. nonF=(a,b)=>a+b;

如果只有一个参数则小括号可省略

  1. nonF=a=>a+b;

如果没有参数,小括号保留:

  1. nonF=()=>console.log('not fail');

箭头函数没有原型属性prototype,不能当构造函数使用.
箭头函数的this始终和它的上下文绑定

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!