首页 > 其他分享 >函数进阶

函数进阶

时间:2022-10-26 11:55:24浏览次数:86  
标签:console 进阶 函数 sayName fun cat name

设置函数默认值

        /*
        function fun(n,m){
            return n+m;
        }
        let result=fun();//没有参数时n和m的值都是defined
        console.log(result);//NaN Not a Number
        */
        function fun(n=10,m=20){//设置默认值
            return n+m;
        }
        let result=fun();//没有参数时就以默认值计算
        console.log(result);//30

立即执行函数


声明后立即被调用,且无法再次被调用,用完一次就没了。

作用域链



类似于全局变量和局部变量,就近原则,从内往外找

闭包

函数里的函数就是闭包,如fun2

封装体现在外部可以调用闭包函数fun2,但是无法调用其中的变量n、m
f的结果是fun2,所以f();的结果是调用fun2函数
闭包的特性体现在f=fun1();表明外部函数已经执行完了
但因为后面还有内部函数result=f();没执行,所以还没有销毁里面的变量

代码封装


外部通过module调用add,但是无法获取变量ab,从而实现封装。这是老代码的写法,看懂就行

箭头函数

  • 简化写法
/*
        const fun = function(x){
            return x*x;
        }*/
        //箭头函数
        const fun = x => x*x;//如果变量有两个以上就要加括号,const fun = (x,y) => x*y;
  • this
const cat={
            name:"mao",
            sayName(){
                setInterval(function(){//每个一秒输出cat的name
                    console.log(this.name);//但是发现什么也没输出
                },1000);
            }
        };
        cat.sayName();

什么也没输出的原因是setInterval是window对象的方法,console.log(this.name)的this指的是window,而window没有name属性

要想让this指的是cat,可以定义一个变量

const cat={
            name:"mao",
            sayName(){
                let self=this;//指向cat
                setInterval(function(){
                    console.log(self.name);//self指向cat,也可以不用self,而知cat.name
                },1000);
            }
        };
        cat.sayName();

但这样很麻烦,加了个变量,这时可以用箭头函数的特性

const cat={
            name:"mao",
            sayName(){
                setInterval(() => {//sayName没有参数就用()表示
                    /*
                    普通函数指向的是调用该函数的对象,这里是setInterval调用的console
                    所以this指向setInterval的对象window

                    箭头函数:在哪定义(它只会在自己作用域的上一层继承this),this就指向谁。
                    它作用域的上一层是sayName(){},所以它在sayName中继承this,继承了cat的this
                    箭头函数是在sayName函数中定义的,this指向sayName的对象cat
                    */
                    console.log(this.name);//mao
                },1000);
            }
        };
        cat.sayName();

标签:console,进阶,函数,sayName,fun,cat,name
From: https://www.cnblogs.com/ben10044/p/16827790.html

相关文章

  • main函数前一般做这些事情
    image前8个字节数据分别是芯片上电的初始SP,PC,其中PC指向的便是本文件里的Reset_Handler,这是芯片执行的第一个函数入口,该函数主要用于完成应用系统初始化工作main函数之......
  • JAVA 进阶完结
    1.接口  这个类中有了新的关键字abstract并且里面的方法没有括号这样的类 上图就是抽象函数与抽象类 那么继承抽象类的子类需要做什么工作呢  2.代......
  • [C++]linux下实现ls()函数遍历目录
    intls(std::stringpath,std::string&ret){DIR*dirp=opendir(path.c_str());if(!dirp){return-1;}structstatst;structdirent*di......
  • 手写一个Redux,深入理解其原理-面试进阶
    Redux可是一个大名鼎鼎的库,很多地方都在用,我也用了几年了,今天这篇文章就是自己来实现一个Redux,以便于深入理解他的原理。我们还是老套路,从基本的用法入手,然后自己实现一个R......
  • day18 批量查询与模糊查询 & 聚合函数与内置函数 & distinct关键字 & 分页查询limit &
    day18两个表student和userstudentids_names_ages_sexuseridu_nameu_ageu_sex批量插入insertintostudents_names_sexselectu_name,u_sexfromuser;//......
  • 关于gets()函数 error: 'gets' was not declared in this scope; did you mean 'fgets
    关于gets函数有一段时间没有写算法了,今天重新写一道已经AC的题发现不通过。发现如下报错:error:'gets'wasnotdeclaredinthisscope;didyoumean'fgets'?也就是说......
  • c++相关的 常用帮助函数集
    ////CreatedbyDangXSon2022/4/27.//#ifndefCPLUSPLUS_PROJECT1_HELPER_H#defineCPLUSPLUS_PROJECT1_HELPER_H#include<iostream>#include<cstring>#inc......
  • python获取当前运行函数名
     两种方式:#!/usr/bin/envpython3#coding:utf-8importsys,inspectdeftest_a():print('funcname:',sys._getframe().f_code.co_name)deftest_b():print......
  • SQL-五(常用函数)
    SQL常用函数①函数的概念:1.函数的格式:函数名(参数)              即:函数名(参数1,参数2,....) 三个注意事项:Ⅰ:英括号      ......
  • (六)Linux系统IO函数
    1open打开文件打开文件intopen(constchar*pathname,intflags);创建文件intopen(constchar*pathname,intflags);关闭文件intclose(intfd);不是重载,C......