あなたのコードにはいくつか問題があります
1.initialiseWebGl()
グローバルスコープで宣言された関数を探します->関数はありません
this.initialiseWebGl()
Objects メソッドへのアクセスに使用する必要があります
。注:この場合this
のインスタンスを指します。Renderer
2.関数を割り当てていない代わりに、 sプロトタイプメソッドRenderer.prototype.initialiseWebGL()
を呼び出そうとすると、定義されていないため、エラーが発生しますRenderer
initialiseWebGl
3.{
行の下に移動されるため、ブロックとして解釈されます -> このコードが実行されます。
あなたの後にそれらを持っていた場合()
、構文エラーが発生します->
Renderer.prototype.initialiseWebGL() {...
結果は次のようになりますUncaught SyntaxError: Unexpected token {
コメント付きの例を次に示します
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"*/
コンソール出力でわかるように
JSBinはこちら