我想创建一个工作方式类似于BigInt的类.所以我可以做:
BigInt('111') BigInt('222').toString()
个
请指导我如何编码.我正在使用NodeJS和普通的Java脚本.提前谢谢你了.
我想创建一个工作方式类似于BigInt的类.所以我可以做:
BigInt('111') BigInt('222').toString()
个
请指导我如何编码.我正在使用NodeJS和普通的Java脚本.提前谢谢你了.
有几种解决方案.
可能最简单的方法是先创建一个普通类,然后再创建一个帮助器函数来创建对象.
class MyRealClass {
constructor(name) {
this.name = name;
}
hello() {
console.log('Hello', this.name);
}
}
function creteMyRealClass(name) {
if (new.target) throw Error("Object should not be called with 'new'");
return new MyRealClass(name);
}
creteMyRealClass('Alpha').hello();
另一种 Select 是使用调用Object.Create的帮助器函数
// By declaring an external object with attributes
// common to all objects of this type, you save memory
// and execution time
const MyPrototype = {
hello() {
console.log('Hello', this.name);
},
};
function createMyPrototype(name) {
if (new.target) throw Error("Object should not be called with 'new'");
const obj = Object.create(MyPrototype);
obj.name = name;
return obj;
}
const b = createMyPrototype('Beta');
b.hello();