js中的关键字的使用

变量声明关键字:var, let, const

// var
var x = 10; // 声明变量x,并赋值为10
x = 20; // 修改变量x的值为20

// let
let y = 30; // 声明块级作用域变量y,并赋值为30
y = 40; // 修改变量y的值为40

// const
const z = 50; // 声明常量z,并赋值为50
//z = 60; // 修改常量z的值为60,会报错

控制流程关键字:if-else, for, while, do-while, break, continue

// if-else
let num = 5;
if (num > 0) { // 如果num大于0
  console.log("num is positive"); // 输出"num is positive"
} else { // 否则
  console.log("num is non-positive"); // 输出"num is non-positive"
}

// for
for (let i = 0; i < 3; i++) { // 循环三次
  console.log(i); // 输出循环变量i的值
}

// while
let j = 0;
while (j < 3) { // 当j小于3时
  console.log(j); //输出循环变量j的值
  j++; // j自增1
}

// do-while
let k = 0;
do {
  console.log(k); // 输出循环变量k的值
  k++; // k自增1
} while (k < 3); // 当k小于3时

// break
for (let i = 0; i < 10; i++) {
  if (i === 5) { // 当i等于5时
    break; // 结束循环
  }
  console.log(i);
}

// continue
for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) { // 当i是偶数时
    continue; // 跳过当前循环迭代
  }
  console.log(i);
}

函数关键字:function, return

// function
function add(a, b) { // 声明函数add,接受两个参数a,b
  return a + b; // 返回a+b的值
}
console.log(add(1, 2)); // 调用函数add,输出3

// return
function multiply(a, b) {
  return a * b; // 返回a*b的值
}
console.log(multiply(2, 3)); // 调用函数multiply,输出6

面向对象关键字:class, extends, constructor, this

// class
class Person { // 定义类Person
  constructor(name, age) { // 构造函数,接受name和age两个参数
    this.name = name; // 将name赋值给this.name
    this.age = age; // 将age赋值给this.age
  }
  getName() { // 定义getName方法
    return this.name; // 返回this.name的值
  }
}
let p = new Person("John", 30); // 创建一个Person类的实例p
console.log(p.getName()); // 调用p


// extends
class Student extends Person { // 定义Student类,继承自Person类
  constructor(name, age, grade) {
    super(name, age); // 调用父类的构造函数
    this.grade = grade;
  }
  getGrade() {
    return this.grade;
  }
}
let s = new Student("Mike", 20, "A");
console.log(s.getName()); // "Mike"
console.log(s.getGrade()); // "A"

// static
class Maths {
  static add(a, b) { // 定义静态方法add
    return a + b; // 返回a+b的值
  }
}
console.log(Maths.add(1, 2)); // 调用静态方法add,输出3

// import & export
// 在main.js文件中
import { add } from './math.js';
console.log(add(1, 2)); // 调用导入的add函数,输出3

数据类型关键字:typeof, instanceof

// typeof
let num = 10;
console.log(typeof num); // "number"

// instanceof
class Person { }
let p = new Person();
console.log(p instanceof Person); // true

其他关键字: in, try-catch, switch, default, rest

// in
let obj = { name: "John" };
console.log("name" in obj); // true

// try-catch
try {
  throw new Error("Error occurred");
} catch (error) {
  console.log(error);
}

// switch
let day = 3;
switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}

// default & rest
function print(a, b = 10, ...c) {
  console.log(a);
  console.log(b);
  console.log(c);
}
print(1, 2, 3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值