批改状态:合格
老师批语:function函数中的序号有点问题
function fname(...){...}
function user(a, b) {return a + b;}console.log(user(10, 20));// 1. `user` 函数的名字// 2. ` (a, b)` 函数的参数// 3. `return a + b;` 函数的返回值// 4. `user(10, 20)` 给函数 user 传值(10,20)
const = function (...){...}把函数保存到变量(常量)中 -任何一个可以计算出确定的’值’的过程
‘函数声明’,变成了 ‘变量声明’, 只不过,变量的值,是一个函数声明
const getName = function (a) {return "Hello " + a;};console.log(getName("Ianren"));// 1. `getName` 常量的名字// 2. ` (a)` 函数的参数// 3. `"Hello " + a;` 函数的返回值// 4. `getName("Ianren")` 给常量`getName`的函数 传值("Ianren")// `let` 定义变量 `const` 定义常量
console.log( (function(..){ ... })(...));
console.log((function (username) {return "Hello" + username;})("Ianren"));// 1. 直接把函数写在 `console.log()` 也就是直接调用函数// 2.`("Ianren")` 是为 `username` 传的值// ! 也可以写为(function (username) {console.log("Hello " + username);})("Ianren");
(...){}return 也可不写
let f1 = function (a, b) {return a + b;};console.log(f1(10, 30));// 使用箭头函数简化f1 = (a, b) => {return a + b;};console.log(f1(10, 30));// 1. 去掉 function// 2. 在参数和{}之间加 =>f1 = (a, b) => {return a + b;};console.log(f1(10, 30));// 只有一条 `return `的情况下 `{}` 和 `return` 不写// 只有一个参数 `()` 也可以不写f1 = (a, b) => a + b;console.log(f1(10, 30));
是一个典型的:多值类型const a = [1, "你好", true];
用 []包含多个数值,里面的值 用 , 分开
引用类型,都是对象,默认返回值 object. 函数(function)除外
声明const a = [1, "你好" true];
调用
const arr = [1, "手机", 5000];console.log(arr);console.log(arr[0]);console.log(arr[1]);console.log(arr[2]);// 数组的索引是从0开始递增的正整数 0, 1, 2, 3,....// 分别调用值结果为// console.log(arr); [1, "手机", 5000]// console.log(arr[0]); 1// console.log(arr[1]); 手机// console.log(arr[2]); 5000// 判断数组类型用`typeof`;console.log(typeof arr);console.log(typeof arr[1]);
name为属性 手机 为值
const obj = { num: 1, name: "手机", price: 5000 };console.log(obj);console.log(obj["num"]);console.log(obj["name"]);console.log(obj["price"]);// 分别调用值结果为// console.log(obj); [1, "手机", 5000]// console.log(obj["num"]); 1// console.log(obj["name"]); 手机// console.log(obj["price"]); 5000// 简化写法console.log(obj);console.log(obj.num);console.log(obj.name);console.log(obj.price);//对象中出现非法标识符 必须用 `[]`obj = { "my email": "admin@php.com" };console.log(obj["my email"]);
name为属性 手机 为值total 设置
let obj = {num: 1,name: "手机",price: 5000,total: function () {let str = obj.name + " 总计: " + obj.price * obj.num + "元";return str;},};console.log(obj.total());// 简化可以let obj = {num: 1,name: "手机",price: 5000,total: function () {let str = `${obj.name} 总计: ${obj.price * obj.num} 元`;return str;},};console.log(obj.total());/// 这里要用反引号 "``" (数字1前面的)///反引号声明的模板字符串,可以插入变量/表达式.这叫"插值"
let obj = {num: 4,name: "手机",price: 5000,total: function () {let str = this.name + " 总计: " + this.price * this.num + "元";return str;},};console.log(obj.total());
let str = this.name + " 总计: " + this.price * this.num + "元";obj 换成了 this,以后可以直接单独改变量名称.
function f1(callback) {console.log(callback());}f1(function () {return "hello";});
function f2() {let a = 1;return function () {return a++;};}console.log(f2());const f = f2();console.log(f);console.log(f());console.log(f());console.log(f());console.log(f());console.log(f());console.log(f());
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号