![](/img/trans.png)
[英]How to call constructor function inside new object in its own class
[英]How to have function call its own function on its creation
我有一個想要與類相似的PublicGame函數。 當我創建PublicGame時,我通過設置this.methodName = function給出了很多方法。 唯一的事情是我想在創建PublicGame時調用其中一些方法。 例如,現在我執行this.judge = this.setJudge()
,但是我知道這在我有的地方不起作用,因為setJudge尚未定義。 我應該把它放在PublicGame的底部嗎? 我的設計完全關閉了嗎?
碼:
'use strict';
// var GameSockets = require(‘GameSockets’);
var Games = {};
var id_counter = 0;
var minPlayers = 3;
var maxPlayers = 6;
function PublicGame (players) {
this._id = id_counter++;
this.players = players;
this.gameSocket = new GameSockets.registerPlayers(this.players, this._id, this.playerDisconnects);
this.judge = this.setJudge();
this.killGame = function() {
delete Games[this._id];
};
// When a player presses leave game
this.playerExits = function(playerToRemove) {
// Delete player from players array
this.players.splice(this.players.indexOf(playerToRemove),1);
// If less than min players
if (this.players.length < minPlayers) this.killGame();
// If less than max players
if (this.players.length < maxPlayers) {
this.needsPlayers = true;
}
gameSockets.kickPlayer(playerToRemove);
};
// When a player disconnects without warning, e.g. closes window
this.playerDisconnects = function(playerToRemove) {
// Delete player from players array
this.players.splice(this.players.indexOf(playerToRemove),1);
// If less than min players
if (this.players.length < minPlayers) this.killGame();
// If less than max players
if (this.players.length < maxPlayers) {
this.needsPlayers = true;
}
};
this.selectJudges = function() {
this.judge = this.players.pop();
this.players = this.players.unshift(this.judge);
};
this.setWinner = function(winner) {
this.winner = winner;
};
Games[this._id] = this;
}
如果在原型上定義函數,則不需要“等待”要定義的函數,因為在調用構造函數的代碼時實例將已經具有它們
function PublicGame (players) {
//...
this.judge = this.setJudge();
}
PublicGame.prototype.killGame = function(){
//...
};
PublicGame.prototype.playerExits = function(playerToRemove){
//...
};
PublicGame.prototype.setJudge = function(){
//do whatever
return whatever;
};
因此,除非您的函數需要訪問某些“私有”變量(即在構造函數中定義,而不是全局變量)或其他需要它的原因,否則請在原型上定義它,而不是在構造函數中定義它,即可使用。
您必須使用javascript原型 !
閱讀代碼示例中的注釋。
/* * utils functions * * dont take care about that **/ var el = document.getElementById('dbg'); var jj = function(val,sep){return JSON.stringify(val , null , sep || '')} var log = function(val){el.innerHTML+='<div><pre>'+val+'</pre></div>'}; var counterId = 0; /************************************************************************/ // You have to use prototype // here an example of what you can achieve // we create a Player 'class' var Player = function( name ){ this.id = counterId ++; //<-- an attribute this.name = name; //<-- an attribute this.setLevel(5);//<-- a method called at 'instanciation' return this; }; // a method available at instanciation time Player.prototype.setLevel = function(level){ this.level = level; return this; }; // we create a new Player named Toto var Toto = new Player('Toto'); log('Toto = ' + jj(Toto));//<-- utility function just to log // we create a new Player named Jane var Jane = new Player('Jane'); log('Jane = ' + jj(Jane)); //<-- utility function just to log // we change the Level of Jane Jane.setLevel(12); log('Jane.setLevel(12)');//<-- utility function just to log log('Jane = ' + jj(Jane));//<-- utility function just to log
<div id='dbg'></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.