function Car(brand, color, displacement) { this.brand = brand; this.color = color; this.displacement = displacement; this.info = function() { return `颜色为${this.color}的${this.brand},排量为${this.displacement}T。`; } } function Person({ brand, color, displacement, name, }) { Car.apply(this, [brand, color, displacement]); this.name = name; this.say = function() { console.log(`${this.name}买了一辆${this.info()}`); // 半晨买了一辆颜色为寒山暮紫的五菱星辰,排量为1.5T。 } } let infomation = { brand: '五菱星辰', color: '寒山暮紫', displacement: '1.5', name: '半晨', }, person = new Person(infomation); person.say();
JavaScript实现构造函数的封装
关注
打赏