批改状态:合格
老师批语:
常用的函数类型主要有:命名函数,匿名函数,箭头函数,立即执行函数.
/*命名函数*/function sum(a,b){// 返回相加后的值return a+b;}console.log(sum(20,30));/*模板字面量*/function sum(a,b){return `${a} + ${b} = ${a + b}`}console.log(sum(1,8));
// 函数表达式(函数变量):值是一个匿名函数let sum5 = function(a,b){return `${a} + ${b} = ${a + b}`;}console.log(sum5(100,300));
//箭头函数(匿名函数的简写)// fn=>(){}/*** 声明简化* 1. 删除:function* 2.(){}: ()=>{}* (参数列表)=>{函数的代码块}*/let sum6 = (a,b)=>{return `${a} + ${b} = ${a + b}`}console.log(sum6(12,38));/*** 参数简化* 1. 单参数:可以不加括号()* 2. 多个参数或者无参数:必须加括号()*//*** 返回值可以简化* 如果只有一条return语句* 1. 可以不写: {}* 2. 可以不写: return*/sum6 = (a,b)=>`${a} + ${b} = ${a + b}`;console.log(sum6(1,9));
let sum8 = (function (a,b){return `${a} + ${b} = ${a + b}`})(12,11)console.log(sum8);
// 1.number类型console.log(12345,typeof(12345));console.log(3.14,typeof(3.14));//都返回number类型
// 2.string字符串类型console.log('baidu',typeof('baidu'));
// 3.boolean布尔类型console.log(true,typeof(true));
// 4.null类型// null返回的是objectconsole.log(null,typeof null);
//5. undefined类型let a;// 声明了没赋值,就是undefined;console.log(a,typeof a);
// 3种类型本质上都是对象// 1.数组const arr=[1,'php',true]console.log(arr);// 访问数组中单个值,可以用索引访问arr[1]console.log(arr[1]);// typeof判断返回都是object;// 正确判断数组的类型方式是console.log(Array.isArray(arr));//返回true表示是数组
// 2.对象// 与数组是有区别的,可以视为语义化字符串的数组,像php中的关联数组let user = {id:10,name:'admin',isMarried:true,'my mail':'123@qq.com'};console.log(user['name']);//如果属性都是合法标识符(比如标识符中间有空格等),可以用 "."来访问成员console.log(user.name);//属性是非法的标识符,就必须用数组的方式索引方式访问user['my mail']console.log(user['my mail']);//将操作封装到对象中去user = {//属性:对象中的变量id:10,name:'admin',isMarried:true,//方法:对象中的函数show:function(){//this:当前对象的引用return `id=${this.id},name=${this.name}`},}console.log(user.show());
let fn = function(){}console.log(typeof(fn));//因为函数是对象,所以可以给其添加属性和方法fn.price = 998.00fn.sweet = function (uname){return `hello,${uname}`;}console.dir(fn.price);console.dir(fn.sweet('张老师'));
通过3月22日晚上的学习,基本熟悉四种函数和几种数据类型.
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号