0
這是我第一次在JavaScript中使用原型,我可以使用一些幫助。JavaScript:如何使用參數創建原型函數
我有一類就是像
function snakegame(C, C_w, C_h, Ls, spd)
{
// .... bunch of other stuff ...
this.boxCollision = function(A, B)
{
return A.x == B.x && A.y == B.y;
}
// ... bunch of other stuff here too ...
}
,我想原型的boxCollision
功能,即定義它裏面snakegame
然後寫之外的實施。我試過
function snakegame(C, C_w, C_h, Ls, spd)
{
// .... bunch of other stuff ...
this.boxCollision(A, B);
// ... bunch of other stuff here too ...
}
snakegame.prototype.boxCollision = function(A, B)
{
return A.x == B.x && A.y == B.y;
}
但是沒有奏效。
我也試着編寫原型爲this.boxCollision(var, var)
,但那也沒用。這樣做的正確方法是什麼?
那麼,你是怎麼在第一個案例中調用'boxCollision'的? 'this.boxCollision(A,B);'只有在定義了'A'和'B'時纔有效。問題似乎與原型不同,但與您如何調用該方法有關。你傳遞給它的參數必須存在(就像使用其他函數,原型一樣)。 – 2014-09-04 15:01:22
你不需要定義。你可以直接執行。我,e刪除線this.boxCollision(A,B);它會按你的意願工作。 – Sunand 2014-09-04 15:04:57
順便說一句,我推薦閱讀一些關於這個主題的文檔,比如MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript – 2014-09-04 15:09:02