多继承控制台输出 [英] Multiple inheritance console output
问题描述
我正在使用下面的代码。我的代码输出控制台出错了。现在我得到这是一个B但我在输出控制台上需要与构造函数相关的输出。就像这是aA一样,这是aB第二,这是aC第三个控制台。
I am using below code. I'm getting wrong output console with my code. Right now i am getting "this is aB" but i required constructor related output on my output console. Like "this is aA" for first, "this is aB" for second, "this is aC" for third console.
function A () {
this.name = "A";
}
A.prototype.a = function () {
console.log("this is a"+this.name);
}
function B () {
this.name = "B";
}
B.prototype.b = function () {
console.log("this is b"+this.name);
}
function C() {
this.name = "C";
A.call(this);
B.call(this);
}
C.prototype = Object.assign({}, A.prototype, B.prototype);
C.prototype.constructor = C;
C.prototype.c = function () {
console.log("this is c"+this.name);
}
var x = new C();
x.a(); //this is aB
x.b(); //this is bB
x.c(); //this is cB
推荐答案
所有三个构造函数中的此
引用相同的对象:由 new
运算符创建的对象。该一个对象只能有一个 名称
属性。因此,您最后调用的构造函数将获胜,名称将由此分配。因此,你一直看到B,因为即使使用 new C
,首先 C
写C,然后 A
写A(覆盖C),最后 B
写B(覆盖A)。
this
in all three constructors refers to the same object: The one created by the new
operator. That one object can only have one name
property. So whichever constructor you call last will "win" and the name will be assigned by that. Thus, you're seeing B all the time because even with new C
, first C
writes C, then A
writes A (overwriting C), and finally B
writes B (overwriting A).
如果您希望与层次结构中每个级别相关的代码拥有自己的 name
属性,那么您无法真正做到这一点,但您可以关闭通过让每个人使用自己的属性(例如, nameA
, nameB
和 nameC
)。您可以通过使用括号表示法和每个级别的所有代码共享的变量来不要求您记住编写代码的级别。
If you want the code related to each level in the hierarchy to have its own name
property, you cannot literally do that, but you can get close by having each one use its own property (e.g., nameA
, nameB
, and nameC
). You can do this in a way that doesn't require you to remember which level you're writing the code at by using brackets notation and a variable shared by all the code for each level.
我不是推荐那个。无论你想要解决的实际问题是什么,都可能有更好的解决方案。
I'm not recommending that. Whatever the actual problem you're trying to solve is, there's probably a better solution.
但是你可以这样做:
var A = (function() {
var name = "nameA"; // <== We declare a variable and put this level's property name in it
function A () {
this[name] = "A"; // <== Note we're using brackets notation here
}
A.prototype.a = function () {
snippet.log("this is a"+this[name]); // <== Brackets again here
};
return A;
})();
var B = (function() {
var name = "nameB"; // <== Same again for B
function B () {
this[name] = "B";
}
B.prototype.b = function () {
snippet.log("this is b"+this[name]);
};
return B;
})();
var C = (function() {
var name = "nameC";
function C() {
this[name] = "C";
A.call(this);
B.call(this);
}
C.prototype = Object.assign({}, A.prototype, B.prototype);
C.prototype.constructor = C;
C.prototype.c = function () {
snippet.log("this is c"+this[name]);
};
return C;
})();
var x = new C();
x.a(); //this is aA
x.b(); //this is bB
x.c(); //this is cC
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
一个对象全部三个构造函数使用将最终有三个属性: nameA
, nameB
,以及 nameC
。
The one object all three constructors work with will end up with three properties: nameA
, nameB
, and nameC
.
同样,我不是建议那样,只是指出它是可能的,并且可以适应一些问题,虽然目前还不清楚它是否适合你的问题。
Again, I'm not recommending that, just pointing out that it's possible, and can suit some problems, although it's unclear whether it suits yours.
这篇关于多继承控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!