Javascript Javascript在构造函数中调用原型函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14441156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:46:32  来源:igfitidea点击:

Javascript calling prototype functions in constructor

javascriptclassmethodsprototype

提问by Danny

I keep getting an error saying that my functions are not defined when I was trying to call the prototype functions in the constructor and I dont know whats wrong with it.

当我试图在构造函数中调用原型函数时,我一直收到一个错误,说我的函数没有定义,我不知道它有什么问题。

Here's the code I have:

这是我的代码:

function Renderer()
{
    initialiseWebGL();
    initialiseShader();
    initialiseBuffer();
}

Renderer.prototype.initialiseWebGL()
{
    //Do stuff.
};

Renderer.prototype.initialiseShader()
{
        //Do Shader's stuff
};

Renderer.prototype.initialiseBuffer()
{
        //Do Buffers
};

What is wrong with it?

它有什么问题?

回答by Minko Gechev

Your syntax is wrong. Use this:

你的语法是错误的。用这个:

function Renderer() {
    this.initialiseWebGL();
    this.initialiseShader();
    this.initialiseBuffer();
}

Renderer.prototype.initialiseWebGL = function () {
    //Do stuff.
};

Renderer.prototype.initialiseShader = function () {
        //Do Shader's stuff
};

Renderer.prototype.initialiseBuffer = function () {
        //Do Buffers
};

After that you can create new object and use it by:

之后,您可以创建新对象并通过以下方式使用它:

var rendererInstance = new Renderer();

回答by Moritz Roessler

There are a few things wrong with your Code

您的代码有一些问题

1.initialiseWebGl()would look for a function declared in the Global scope -> there is no function

1.initialiseWebGl()会寻找在全局范围内声明的函数 -> 没有函数

  • You should use this.initialiseWebGl()to access the Objects Method
    Note: thisrefers to the Instance of Rendererin this case
  • 您应该使用this.initialiseWebGl()访问 Objects 的方法
    注意:在这种情况下this指的是 InstanceRenderer

2.You are not assigning a function with Renderer.prototype.initialiseWebGL()instead you try to invoke the Renderers prototype method initialiseWebGlwhich gives you an error, as its not defined

2.您没有分配函数,Renderer.prototype.initialiseWebGL()而是尝试调用Renderers 原型方法initialiseWebGl,该方法会给您一个错误,因为它未定义

3.Because the {are moved down a line they get interpreted as a Block -> this code gets executed.
If you'd had them after your ()you would get a Syntax Error-> Renderer.prototype.initialiseWebGL() {...would result in Uncaught SyntaxError: Unexpected token {

3.因为{它们被向下移动了一行,所以它们被解释为一个块 -> 此代码被执行。
如果你在你之后拥有它们,()你会得到一个语法错误-> Renderer.prototype.initialiseWebGL() {...会导致Uncaught SyntaxError: Unexpected token {

Heres an Commented Example

这是一个注释示例

function Renderer() {
    initialiseWebGL(); // I call the global declared function
    this.initialiseShader(); //I call the Prototypes function
    this.initialiseBuffer(); //Me too
}

Renderer.prototype.initialiseWebGL = function (){ //Here a function gets assigned to propertie of `Renderer`s `prototype` Object
    //Do stuff.
};

Renderer.prototype.initialiseShader = function (){
        console.log("Do Shader Stuff");
};

Renderer.prototype.initialiseBuffer = function (){
        console.log("Do initialise stuff");
};
 Renderer.prototype.initialiseBuffer() // I invoke the method above
{
 console.log("I'm a Block statement");
}; 

function initialiseWebGL () { //I'm the global declared function
  console.log("Global");
}

var ren1 = new Renderer();

/*"Do initialise stuff"  
"I'm a Block statement"  
"Global"  
"Do Shader Stuff"  
"Do initialise stuff"*/

As you can see in the consoles Output

正如您在控制台输出中看到的那样

Heres a JSBin

这是一个JSBin

回答by Bergi

Since your instances inherit the (method) propertiesfrom the prototype objects, you need to access them as properties and not as plain variables:

由于您的实例从原型对象继承了(方法)属性,因此您需要将它们作为属性而不是普通变量来访问:

function Renderer() {
    this.initialiseWebGL();
    this.initialiseShader();
    this.initialiseBuffer();
}