function MyClass() {
const args = Array.from(arguments);
if (args.length === 1 && typeof args[0] ==='string') {
this.initWithString(args[0]);
} else if (args.length === 1 && typeof args[0] === 'number') {
this.initWithNumber(args[0]);
}
}
MyClass.prototype.initWithString = function (str) {
this.strValue = str;
console.log(`Initialized with string: ${this.strValue}`);
};
MyClass.prototype.initWithNumber = function (num) {
this.numValue = num;
console.log(`Initialized with number: ${this.numValue}`);
};
// 测试
const instance1 = new MyClass('test string');
const instance2 = new MyClass(42);