在javascript中,如何从同一个类中的另一个方法调用类方法?

时间:2022-11-25 08:04:41

I have this:

我有这个:

var Test = new function() {  
    this.init = new function() {  
        alert("hello");  
    }
    this.run = new function() {  
        // call init here  
    }  
}

I want to call init within run. How do I do this?

我想在运行中调用init。我该怎么做呢?

4 个解决方案

#1


6  

Use this.init(), but that is not the only problem. Don't call new on your internal functions.

使用this.init(),但这不是唯一的问题。不要在内部函数上调用new。

var Test = new function() {
    this.init = function() {
        alert("hello");
    };

    this.run = function() {
        // call init here
        this.init();
    };
}

Test.init();
Test.run();

// etc etc

#2


3  

Instead, try writing it this way:

相反,尝试这样写:

function test() {
    var self = this;
    this.run = function() {
        console.log(self.message);
        console.log("Don't worry about init()... just do stuff");
    };

    // Initialize the object here
    (function(){
        self.message = "Yay, initialized!"
    }());
}

var t = new test();
// Already initialized object, ready for your use.
t.run()

#3


2  

Try this,

尝试这个,

 var Test =  function() { 
    this.init = function() { 
     alert("hello"); 
    }  
    this.run = function() { 
     // call init here 
     this.init(); 
    } 
} 

//creating a new instance of Test
var jj= new Test();
jj.run(); //will give an alert in your screen

Thanks.

谢谢。

#4


1  

var Test = function() {
    this.init = function() {
        alert("hello");
    } 
    this.run = function() {
        this.init();
    }
}

Unless I'm missing something here, you can drop the "new" from your code.

除非我在这里遗漏了什么,否则你可以从你的代码中删除“新”。

#1


6  

Use this.init(), but that is not the only problem. Don't call new on your internal functions.

使用this.init(),但这不是唯一的问题。不要在内部函数上调用new。

var Test = new function() {
    this.init = function() {
        alert("hello");
    };

    this.run = function() {
        // call init here
        this.init();
    };
}

Test.init();
Test.run();

// etc etc

#2


3  

Instead, try writing it this way:

相反,尝试这样写:

function test() {
    var self = this;
    this.run = function() {
        console.log(self.message);
        console.log("Don't worry about init()... just do stuff");
    };

    // Initialize the object here
    (function(){
        self.message = "Yay, initialized!"
    }());
}

var t = new test();
// Already initialized object, ready for your use.
t.run()

#3


2  

Try this,

尝试这个,

 var Test =  function() { 
    this.init = function() { 
     alert("hello"); 
    }  
    this.run = function() { 
     // call init here 
     this.init(); 
    } 
} 

//creating a new instance of Test
var jj= new Test();
jj.run(); //will give an alert in your screen

Thanks.

谢谢。

#4


1  

var Test = function() {
    this.init = function() {
        alert("hello");
    } 
    this.run = function() {
        this.init();
    }
}

Unless I'm missing something here, you can drop the "new" from your code.

除非我在这里遗漏了什么,否则你可以从你的代码中删除“新”。