Javascript 对象调用会导致无休止的递归

Javascript 对象调用会导致无休止的递归,javascript,recursion,Javascript,Recursion,我需要你的帮助。目前,我正试图理解JavaScript是如何处理类和对象的。所以我写了一个小例子: function aClass(test, something_else){ this.string1 = "I'am just a class !"; this.string2 = ""; this.methode1 = function(){ console.log("Greetings :)"); this.constructor("C

我需要你的帮助。目前,我正试图理解JavaScript是如何处理类和对象的。所以我写了一个小例子:

function aClass(test, something_else){
    this.string1 = "I'am just a class !";
    this.string2 = "";
    this.methode1 = function(){
        console.log("Greetings :)");
        this.constructor("Called from method1","Pretty nice or ?");
    };
    console.log("Line 8 !");
    this.methode1();
    this.constructor = function(input1, input2){
        console.log("Method 2 was called !\n"+ input1 +"\n"+ input2);
    };
    this.constructor(test, something_else);
}

var object_contianer1 = new aClass("You");
object_contianer1.methode1();
但是第9行(
this.method1();
)中的调用似乎会导致无休止的递归,我不知道为什么。如果我删除这一行,那么第13行(
this.constructor(test,something);
)不会产生这种递归,所以我想知道原因是什么


我希望你们中的一位能给我解释一下。

new aClass()
使用构造函数
aClass
创建一个新对象。在该函数调用中,
this.constructor
指的是
aClass
本身

因此:

function aClass() {
  this.constructor();
}
与此直接等效,这显然会产生一个无限循环:

function aClass() {
  aClass();
}
请注意,这仅适用于调用
new aClass()
,因为
new
此设置设置为“正在创建的对象”


如果调用
aClass()。这也会失败,这次是一个
TypeError
,因为调用
Window

这个是不合法的。在分配自己的函数之前,构造函数是指向
aClass
mdn的指针:所以如果在调用
this.methode1()之前分配
this.constructor=function(input1,input2){
全部work@Grundy很抱歉,我不明白你的答案。为什么会产生递归。老实说,我不知道这个原型是什么:(很抱歉这么问,很明显,但你怎么知道它是无止境的递归?好吧,这似乎很合理……我想,但我怎么能避免它?@Feirell只是不调用
这个。构造函数
@Grundy,我该如何调用内部方法?@feirel,简单地重命名你的方法,并像redular方法一样使用它