javascript - 一个类中有多个构造函数。Javascript
问题描述
所以这就是他们要求我做的事情:1)创建一个名为 Person 的类,具有以下条件。属性:姓名、年龄、DNI、性别(M 为男性,F 为女性)、体重和身高。除 DNI 外,每个属性都将根据其类型(数字为 0,字符串为空字符串等)具有默认值。性别默认为男性。
2) 创建以下构造函数: • 具有默认值的构造函数。• 以姓名、年龄和性别为参数的构造函数(默认为其他值)。• 将所有属性作为参数接收的构造函数。
我需要知道执行此操作的正确方法,以便能够创建 3 个具有不同值的类对象。
class Person {
constructor(name, age, sex, dni, weight, height){
this.name = '';
this.age = 0;
this.sex = 'M';
this.dni = createDni();
this.weight = 0;
this.height = 0;
}
static Person1(name, age, sex){
return new Person(name,age,sex);
}
static Person2(name, age, sex, dni, weight, height){
return new Person(name, age, sex, dni, weight, height);
}
}
var Person1 = new Person(){
this.name = 'Manuel'
this.age = 25;
this.sex = 'M';
this.height = 1,75;
this.weight = 90;
}
我应该能够将不同的值传递给从“类”创建的 3 个不同的对象。
解决方案
JavaScript 的class
语法创建了一个构造函数和一个关联的原型对象。JavaScript 中没有内置的函数重载,包括构造函数。在 JavaScript 中进行“重载”的唯一方法是在一个函数本身的代码中处理它。
在您的情况下,您有几个选择,但最简单的可能是简单地对所有参数使用默认参数值:
constructor(name = '', age = 0, sex = 'M', dni = createDni(), weight = 0, height = 0) {
this.name = name;
this.age = age;
this.sex = sex;
this.dni = dni;
this.weight = weight;
this.height = height;
}
不用担心,createDni
只有在调用dni
构造函数时没有提供参数时才会调用(或者如果提供的值是undefined
)。
这样做的一个优点是调用者可以为任何参数、所有参数或介于两者之间的任何参数提供参数,而不仅仅是 0、3 和 6。
现场示例:
function createDni() {
console.log("createDni was called");
return 42;
}
class Person {
constructor(name = '', age = 0, sex = 'M', dni = createDni(), weight = 0, height = 0) {
this.name = name;
this.age = age;
this.sex = sex;
this.dni = dni;
this.weight = weight;
this.height = height;
}
}
console.log("No arguments:");
console.log(JSON.stringify(new Person()));
console.log("Three arguments:");
console.log(JSON.stringify(new Person("Joe Bloggs", 42, "M")));
console.log("Six arguments:");
console.log(JSON.stringify(new Person("Joe Bloggs", 42, "M", 67, 182, 6)));
.as-console-wrapper {
max-height: 100% important;
}
如果你真的想只允许没有参数、三个参数或六个参数,你可以使用一个 rest 参数或arguments
对象。使用arguments
看起来像这样:
constructor(name = '', age = 0, sex = 'M', dni = createDni(), weight = 0, height = 0) {
const alen = arguments.length;
if (alen !== 0 && alen !== 3 && alen !== 6) {
throw new Error("0, 3, or 6 arguments are required");
}
this.name = name;
this.age = age;
this.sex = sex;
this.dni = dni;
this.weight = weight;
this.height = height;
}
使用 rest 参数看起来像这样,请注意您丢失了命名参数:
constructor(...args) {
const alen = args.length;
if (alen !== 0 && alen !== 3 && alen !== 6) {
throw new Error("0, 3, or 6 arguments are required");
}
[
this.name = "",
this.age = 0,
this.sex = "M",
this.dni = createDni(),
this.weight = 0,
this.height = 0
] = args;
}
...再次,createDni
仅在需要时调用where。
推荐阅读
- javascript - 我收到一个错误:对象作为 React 子项无效(找到:[object Promise])
- python - Python:如何检查列表是否包含数值并返回布尔值?
- react-native - 如何自动化 Expo web index.html 样式
- flutter - 当参数不是 String Flutter 时,Uri 声明失败
- php - 使标头静音并访问日志输出,但仍显示来自 php 内置服务器的 error_log()/syslog() 输出
- javascript - 如何在innerHTML中使用onClick函数
- angular - 从多个容器中获取价值 - Mat Table
- c++ - 如何从世界空间渲染到相机空间?
- javascript - 如何在 html 音频标签中自动播放 saavan 链接?
- azure-data-factory - 如何在 Azure 数据工厂映射数据流中将 UST 转换为 EST?