使用 class 检查 instanceof
Checking instanceof with a class
我有以下代码:
function Complex(real, imaginary) {
let c = Object.create(Complex.methods);
[c.real, c.imaginary] = [real, imaginary];
return c;
}
Complex.methods = {
toString() {return `${this.real}+${this.imaginary}i`},
add(other) {
if (!(other instanceof Complex)) throw new Error("Only supports complex-only addition");
return Complex(this.real+other.real, this.imaginary+other.imaginary);
}
}
let c1 = Complex(2,0);
let c2 = Complex(3,4);
console.log(c1.add(c2) + "");
// "Uncaught Error: Only supports complex-only addition",
发生这种情况是因为 c1 instanceof Complex
正在 returning false
。为什么它 return 是错误的?
与使用 class
关键字比较:
class CX {
toString() {return "xxx"}
}
let c1 = new CX();
console.log(c1 + "", c1 instanceof CX);
// xxx true
但还是很好奇为什么第一个不认识 instanceof
运算符。
OP 要么选择 classic(好日子)构造函数方法 ...
function Complex(real, imaginary) {
Object.assign(this, { real, imaginary });
}
Complex.prototype.toString = function toString () {
return `${this.real} + ${this.imaginary}i`
};
Complex.prototype.add = function add (other) {
if (!(other instanceof Complex)) {
throw new Error("Only supports complex-only addition");
}
return new Complex(
this.real + other.real,
this.imaginary + other.imaginary
);
};
let c1 = new Complex(2,0);
let c2 = new Complex(3,4);
console.log(c1.add(c2) + "");
.as-console-wrapper { min-height: 100%!important; top: 0; }
...或基于class的方法...
class Complex {
constructor(real, imaginary) {
Object.assign(this, { real, imaginary });
}
toString () {
return `${this.real} + ${this.imaginary}i`
}
add (other) {
if (!(other instanceof Complex)) {
throw new Error("Only supports complex-only addition");
}
return new Complex(
this.real + other.real,
this.imaginary + other.imaginary
);
}
}
let c1 = new Complex(2,0);
let c2 = new Complex(3,4);
console.log(c1.add(c2) + "");
.as-console-wrapper { min-height: 100%!important; top: 0; }
我有以下代码:
function Complex(real, imaginary) {
let c = Object.create(Complex.methods);
[c.real, c.imaginary] = [real, imaginary];
return c;
}
Complex.methods = {
toString() {return `${this.real}+${this.imaginary}i`},
add(other) {
if (!(other instanceof Complex)) throw new Error("Only supports complex-only addition");
return Complex(this.real+other.real, this.imaginary+other.imaginary);
}
}
let c1 = Complex(2,0);
let c2 = Complex(3,4);
console.log(c1.add(c2) + "");
// "Uncaught Error: Only supports complex-only addition",
发生这种情况是因为 c1 instanceof Complex
正在 returning false
。为什么它 return 是错误的?
与使用 class
关键字比较:
class CX {
toString() {return "xxx"}
}
let c1 = new CX();
console.log(c1 + "", c1 instanceof CX);
// xxx true
但还是很好奇为什么第一个不认识 instanceof
运算符。
OP 要么选择 classic(好日子)构造函数方法 ...
function Complex(real, imaginary) {
Object.assign(this, { real, imaginary });
}
Complex.prototype.toString = function toString () {
return `${this.real} + ${this.imaginary}i`
};
Complex.prototype.add = function add (other) {
if (!(other instanceof Complex)) {
throw new Error("Only supports complex-only addition");
}
return new Complex(
this.real + other.real,
this.imaginary + other.imaginary
);
};
let c1 = new Complex(2,0);
let c2 = new Complex(3,4);
console.log(c1.add(c2) + "");
.as-console-wrapper { min-height: 100%!important; top: 0; }
...或基于class的方法...
class Complex {
constructor(real, imaginary) {
Object.assign(this, { real, imaginary });
}
toString () {
return `${this.real} + ${this.imaginary}i`
}
add (other) {
if (!(other instanceof Complex)) {
throw new Error("Only supports complex-only addition");
}
return new Complex(
this.real + other.real,
this.imaginary + other.imaginary
);
}
}
let c1 = new Complex(2,0);
let c2 = new Complex(3,4);
console.log(c1.add(c2) + "");
.as-console-wrapper { min-height: 100%!important; top: 0; }