可以使用JavaScript中的构造函数来实现一个类。构造函数通过new关键字创建实例,并且可以将属性和方法添加到实例上。例如:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name);
}
const person = new Person("Alice", 25);
person.sayHello(); // 输出:Hello, my name is Alice