当前位置: 首页 > news >正文

js笔记(函数参数、面向对象、装饰器、高级函数、捕获异常)

JavaScript 笔记

函数参数

默认参数

在 JavaScript 中,我们可以为函数的参数设置默认值。如果调用函数时没有传递参数,那么参数将使用默认值。

function greet(name = 'World') {console.log(`Hello, ${name}!`);
}greet(); // 输出:Hello, World!
greet('Alice'); // 输出:Hello, Alice!

Rest 参数

Rest 参数允许我们将不确定数量的参数表示为一个数组。

function sum(...numbers) {let total = 0;for (let number of numbers) {total += number;}return total;
}console.log(sum(1, 2, 3)); // 输出:6
console.log(sum(4, 5));    // 输出:9
console.log(sum(6));       // 输出:6

面向对象

JavaScript 是一种面向对象的语言,它支持通过构造函数和原型来创建对象。

构造函数

构造函数允许我们创建具有相同属性和方法的对象实例。

function Person(name, age) {this.name = name;this.age = age;
}Person.prototype.greet = function() {console.log(`Hello, my name is ${this.name}. I'm ${this.age} years old.`);
};const person1 = new Person('Alice', 25);
const person2 = new Person('Bob', 30);person1.greet(); // 输出:Hello, my name is Alice. I'm 25 years old.
person2.greet(); // 输出:Hello, my name is Bob. I'm 30 years old.

ES6 引入了 class 关键字,使得创建类更加简洁和直观。

class Person {constructor(name, age) {this.name = name;this.age = age;}greet() {console.log(`Hello, my name is ${this.name}. I'm ${this.age} years old.`);}
}const person1 = new Person('Alice', 25);
const person2 = new Person('Bob', 30);person1.greet(); // 输出:Hello, my name is Alice. I'm 25 years old.
person2.greet(); // 输出:Hello, my name is Bob. I'm 30 years old.

装饰器

装饰器是一种语法,可以修改类、方法、属性等的行为。它是 JavaScript 中的一项实验性功能,并需要使用 Babel 等工具进行转译。

function log(target, name, descriptor) {const originalMethod = descriptor.value;descriptor.value = function(...args) {console.log(`Calling ${name} with arguments: ${args.join(', ')}`);return originalMethod.apply(this, args);};return descriptor;
}class Calculator {@logadd(a, b) {return a + b;}
}const calculator = new Calculator();
console.log(calculator.add(2, 3)); // 输出:Calling add with arguments: 2, 3//      5

高级函数

JavaScript 中的高级函数可以接受其他函数作为参数或返回函数。

高阶函数

高阶函数是指接受一个或多个函数作为参数,并/或者返回一个函数的函数。

function applyOperation(a, b, operation) {return operation(a, b);
}function add(a, b) {return a + b;
}function subtract(a, b) {return a - b;
}console.log(applyOperation(2, 3, add));      // 输出:5
console.log(applyOperation(2, 3, subtract)); // 输出:-1

匿名函数和箭头函数

匿名函数是没有具名标识符的函数,我们可以将其作为参数传递给其他函数或直接调用。

箭头函数是一种匿名函数的简写形式,并且它的行为与普通函数略有不同。箭头函数没有自己的 this,而是继承外部作用域的 this 值。

const multiply = function(a, b) {return a * b;
};const divide = (a, b) => a / b;console.log(multiply(2, 3)); // 输出:6
console.log(divide(6, 2));   // 输出:3

捕获异常

在 JavaScript 中,我们可以使用 try...catch 语句来捕获和处理异常。

try {// 可能会抛出异常的代码throw new Error('Something went wrong!');
} catch (error) {// 捕获并处理异常console.error(`Error: ${error.message}`);
}

通过使用 try...catch 块,我们可以在程序出现异常时执行特定的操作并提供更友好的错误处理。

http://www.lryc.cn/news/211404.html

相关文章:

  • Istio实战(八)- Istio 动态准入 Webhook 配置
  • Vue的安装
  • macOS M1安装wxPython报错
  • 【数据结构】交换排序
  • 腾讯云2023年双11服务器优惠活动及价格表
  • PointNet++复现、论文和代码研读
  • 轨迹规划 | 图解路径跟踪PID算法(附ROS C++/Python/Matlab仿真)
  • 吴恩达《机器学习》1-3:监督学习
  • Flutter PopupMenuButton下拉菜单
  • 国家数据局正式揭牌,数据专业融合型人才迎来发展良机【文末送书五本】
  • H5游戏源码分享-像素小鸟游戏(类似深海潜艇)
  • Vue 3 响应式对象:ref 和 reactive 的使用和区别
  • H5游戏源码分享-密室逃脱小游戏(考验反应能力)
  • 【LeetCode刷题-哈希】--706.设计哈希映射
  • 前端 : 用HTML ,CSS ,JS 做一个点名器
  • css:button实现el-radio效果
  • 算法工程师-机器学习-数据科学家面试准备4-ML系统设计
  • git重装后如何连接以前项目
  • 【java学习—十】TreeSet集合(5)
  • JMeter的使用,傻瓜式学习【上】
  • 主定理(一般式)
  • WLAN的组网架构和工作原理
  • 使用OBS Browser+访问华为云OBS存储【Windows】
  • C++总结(3):类的动态内存分配、异常、类型转换运算符
  • 折半搜索(meet in the middle)
  • 【机器学习】loss损失讨论
  • LeetCode 779. 第K个语法符号【递归,找规律,位运算】中等
  • java try throw exception finally 遇上 return break continue造成异常丢失
  • 设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码
  • MATLAB R2018b详细安装教程(附资源)