我目前正在尝试访问我的依赖关系链中特定祖先的方法。具体来说,我想从我初始化的GrandChildClass中运行ParentClass.logName()
。这就是我访问ParentClass原型的方法(从Get parent class name from child with ES6?获取的方法)。
class AncestorClass {
constructor (name, id){
this.name = name + id;
}
logName () {
console.log(this.name);
}
}
class ParentClass extends AncestorClass {
constructor (name, id) {
super(name, 1);
this.name = name + id;
}
}
class ChildClass extends ParentClass {
constructor (name, id) {
super(name, 2);
this.name = name + id;
}
}
class GrandchildClass extends ChildClass {
constructor (name, id){
super(name, 3);
this.name = name + id;
}
}
const grandchild = new GrandchildClass('testName', 4);
const parent = Object.getPrototypeOf((Object.getPrototypeOf(grandchild.constructor)));
console.log(parent);
const initParent = new parent();
initParent.logName();

所以我得到了原型并使用new
关键字对其进行了初始化,但出于某种原因,initParent.logName()
会返回NaN。
那么如何在没有直接初始化ParentClass的情况下从孙子中调用ParentClass.logName?
答案 0 :(得分:2)
使用空构造函数初始化父级,这意味着属性为int foregroundNbackground(VideoCapture cap){
int count=0;
vector<Mat> img_bg;
Mat img, img_diff, b1;
Mat Background = T12(cap); // This calculates the average of previous 100 frames and make it the background model
imshow("Model", Background);
while(1){
cap >> img;
if(count < 100){
img_bg.push_back(img);
}
if(count == 100){
Background = getMean(img_bg);
cvtColor(Background, b1, CV_BGR2GRAY);
imshow("Model", b1);
count = 0;
img_bg.clear();
}
absdiff(img, Background, img_diff);
count++;
cvtColor(img_diff,img_diff, CV_BGR2GRAY);
threshold(img_diff,img_diff,80,255, CV_THRESH_BINARY);
imshow("Task13", img_diff);
waitKey(33);
}
}
,undefined
为undefined + undefined
。
NaN
const initParent = new parent('foo', 1);
initParent.logName() // print 'foo1'