Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试继承for循环中名称引用的所有类时出现问题_Javascript_Oop_Inheritance_Subclassing - Fatal编程技术网

Javascript 尝试继承for循环中名称引用的所有类时出现问题

Javascript 尝试继承for循环中名称引用的所有类时出现问题,javascript,oop,inheritance,subclassing,Javascript,Oop,Inheritance,Subclassing,好吧,这个标题很有意思,但我想不出比它更好的了,欢迎提出意见。 无论如何,我有一个javascript对象,其中包含类作为属性。我想通过子类化创建另一个在各个方面都与第一个相同的对象。我想总结一下: var L1 = {}; L1.Foo = function() {/*...*/}; L1.Bar = function() {/*...*/}; //... L1.Baz = function() {/*...*/}; var L2 = {}; L2.Foo = function() {

好吧,这个标题很有意思,但我想不出比它更好的了,欢迎提出意见。
无论如何,我有一个javascript对象,其中包含类作为属性。我想通过子类化创建另一个在各个方面都与第一个相同的对象。我想总结一下:

var L1 = {};

L1.Foo = function() {/*...*/};
L1.Bar = function() {/*...*/};
//...
L1.Baz = function() {/*...*/};

var L2 = {};

L2.Foo = function() { L1.Foo.call(this); /*possibily some other code here*/ };
L2.Foo.prototype = Object.create(L1.Foo.prototype);
L2.Foo.prototype.constructor = L2.Foo;

L2.Bar = function() { L1.Bar.call(this); /*possibily some other code here*/ };
L2.Bar.prototype = Object.create(L1.Bar.prototype);
L2.Bar.prototype.constructor = L2.Bar;

//...

L2.Baz = function() { L1.Baz.call(this); /*possibily some other code here*/ };
L2.Baz.prototype = Object.create(L1.Baz.prototype);
L2.Baz.prototype.constructor = L2.Baz;

var foo = new L2.Foo();
console.log(foo); //L2.Foo
var bar = new L2.Bar();
console.log(bar); //L2.Bar
var baz = new L2.Baz();
console.log(baz); //L2.Baz
.
我告诉自己:“嗯,看起来这里有一个模式”,所以我去修改我的代码如下:

//first 10 lines unaltered

for(prop in L1) {
    L2[prop] = function() { L1[prop].call(this); /*Call super method by default,
    unless overriden below*/ };
    L2[prop].prototype = Object.create(L1[prop].prototype);
    L2[prop].prototype.constructor = L2[prop];
}
//Here I decide that I want to override only the constructor
//for Foo, so naturally:
L2.Foo.prototype.constructor = function() {
    L1.Foo.call(this);
    this.someOtherProperty = "foo";
};

var foo = new L2.Foo();
console.log(foo); //L2.(anonymous function)?
console.log(foo.someOtherProperty); //undefined?
var bar = new L2.Bar();
console.log(bar); //L2.(anonymous function)?
var baz = new L2.Baz();
console.log(baz); //L2.(anonymous function)?
for(prop in L1) {
    L2[prop] = function() { L1[prop].call(this);
.

我做错了什么?

这可能就是我,但如果子类需要在各个方面都与超类相同,为什么还要麻烦创建子类呢?我不完全清楚您试图从代码中实现什么

但是只要给类一个类本身的属性就可以了 e、 g.(纯java语言)

“嗯,看起来这里有一个模式”所以我去修改了我的代码 详情如下:

//first 10 lines unaltered

for(prop in L1) {
    L2[prop] = function() { L1[prop].call(this); /*Call super method by default,
    unless overriden below*/ };
    L2[prop].prototype = Object.create(L1[prop].prototype);
    L2[prop].prototype.constructor = L2[prop];
}
//Here I decide that I want to override only the constructor
//for Foo, so naturally:
L2.Foo.prototype.constructor = function() {
    L1.Foo.call(this);
    this.someOtherProperty = "foo";
};

var foo = new L2.Foo();
console.log(foo); //L2.(anonymous function)?
console.log(foo.someOtherProperty); //undefined?
var bar = new L2.Bar();
console.log(bar); //L2.(anonymous function)?
var baz = new L2.Baz();
console.log(baz); //L2.(anonymous function)?
for(prop in L1) {
    L2[prop] = function() { L1[prop].call(this);
您已经找到了一个共同点-所有的
L2
函数实际上都在调用
L1.Baz
,因为
prop
将具有值
“Baz”
。有关如何解决此问题,请参见链接问题

另外,请注意,没有一个构造函数将其参数传递给超级调用,这也可能会影响您


这里我决定只覆盖Foo的构造函数,所以 自然地:

L2.Foo.prototype.constructor = function() {
    L1.Foo.call(this);
    this.someOtherProperty = "foo";
};
我做错了什么

覆盖原型对象上的
.constructor
属性不会产生任何效果。您的代码仍在调用
new L2.Foo
,而不是
new L2.Foo.prototype.constructor
。你可能想看看

相反,您确实需要替换
L2.Foo
。这可以通过以下方式实现:

(或者您只需输入第一个版本的标准模式)。如果这太重复,您还可以通过编程方式执行
.prototype
.constructor
设置:

// whole code
var L2 = {
    Foo: function() {
        L1.Foo.call(this);
        this.someOtherProperty = "foo";
    }
    // … other overwritten constructors
};
for (var prop in L1) {
    if (!L2[prop]) // unless overridden above, create default that only…
        (function(parent) {
            L2[prop] = function() {
                parent.apply(this, arguments); // calls super
            };
        }(L1[prop]));
    L2[prop].prototype = Object.create(L1[prop].prototype);
    L2[prop].prototype.constructor = L2[prop];
}

当然,在一个循环中结束!我觉得自己好笨,我现在应该知道了!谢谢你的(完整记录的)解决方案,我会在午餐休息时试一试,然后回击你。不管怎样,我还是要接受它。您也遇到了常见的“循环中的闭包”陷阱,只是现在才注意到:在匿名
函数(父){…}
中,您仍然引用了
prop
。我修复了它,但是
L2.Bar
L2.Baz
函数被报告为“匿名函数”的问题仍然存在:(虽然还没有浏览你提供的链接,但它们可能会有一些亮点。暂时,谢谢。另外,我不允许编辑你的帖子,因为编辑必须至少有6个字符长,但我相信
For
循环中匿名函数的方括号可以从以下形式更改:
(函数(父项){..}(L1[prop])
到此:
(函数(父项){..})(L1[prop])
。我只是引用
prop
将新构造函数分配到它的位置,我没有在构造函数内部使用它。您得到的是
L2。(匿名函数)
不是问题,这是很自然的-这些函数没有命名。如果你想这样,请看一看。不,事实上,我不会担心的,是的,关于闭包问题,你完全正确,我放了一个
console.log(prop)
在构造函数内部,它总是记录
Baz
;但在构造函数外部,它记录
Bar
然后
Baz
。干杯。谢谢,但不确定这在我的上下文和javascript中对我有何作用。