Javascript Angular/Java脚本-参考;“类”;对象实例化之前的属性?
我有c#背景,我开始探索角度JS。我的工厂类/对象基于本文: 所以我有两个工厂类,但我想引用另一个工厂中一个工厂的实例。但是,由于java脚本不是类型化语言,因此在“编译”时,一个工厂的属性和方法在另一个工厂中不可用 检查这把小提琴: var myApp=angular.module('myApp',[])Javascript Angular/Java脚本-参考;“类”;对象实例化之前的属性?,javascript,angularjs,Javascript,Angularjs,我有c#背景,我开始探索角度JS。我的工厂类/对象基于本文: 所以我有两个工厂类,但我想引用另一个工厂中一个工厂的实例。但是,由于java脚本不是类型化语言,因此在“编译”时,一个工厂的属性和方法在另一个工厂中不可用 检查这把小提琴: var myApp=angular.module('myApp',[]) myApp.factory('grid',function(){ //构造函数,带有参数 功能网格(sizeX、sizeY、gridName){ //公共财产 this.cells=新数组(
myApp.factory('grid',function(){
//构造函数,带有参数
功能网格(sizeX、sizeY、gridName){
//公共财产
this.cells=新数组();
this.name=gridName;
//称重传感器
var numCells=sizeX*sizeY;
对于(var i=0;i
网格工厂按预期工作,但我不知道如何在游戏工厂中引用网格实例。我曾尝试将网格“对象”传递到游戏构造函数中,但由于java脚本不是类型化语言,网格属性/方法在游戏工厂中是未定义的 在angular中,工厂应该返回一个对象。返回一个函数 您似乎希望供应商能够配置网格,而不是工厂 示例(未测试): 使用此提供商,您可以:
myApp.configure(['gridProvider',
function (gridProvider) {
'use strict';
gridProvider.setx(100);
gridProvider.sety(100);
gridProvider.setname('name');
}
]);
myApp.controller('myCtrl',
['grid', '$scope',
function (grid, $scope) {
'use strict';
// grid is now an instance of 'Grid'
}
]);
编辑
myApp.factory('Grid',function(){
"严格使用",;
功能网格(sizeX、sizeY、gridName){
变量
i、 numCells=sizeX*sizeY;
this.cells=[];
this.name=gridName;
对于(i=0;i
我找到了一个解决方案,但并不理想:
我现在将网格的参数传递到游戏函数中,并在网格内创建游戏
myApp.factory('game', ['grid', function (_grid_) {
var grid = _grid_;
//TODO: instead of passing the parameters for the grid
//into the game, I would prefer to pass the grid "class"
//But that doesn't seem possible since java script
//doesn't recognize the "class" properties until the
//object is instantiated
function game(sizeX, sizeY, gridName){
this.gameGrid = new grid(3, 3, "Test Grid");
this.gridName = "Grid - " + this.gameGrid.name;
};
game.prototype.isWinner = function () {
for(var i=0; i< this.gameGrid.cells.length;i++){
//set to false if necessary
}
return true;
};
return game;
}]);
但是,我仍然希望能够将网格“类”传递到游戏工厂中。但这不起作用,因为grid.cells在网格实例化之前是未定义的。在Java脚本/Angular中可以克服这个问题吗
function game(grid){
this.gridName = "Grid - " + grid.name;
};
game.prototype.isWinner = function () {
for(var i=0; i< grid.cells.length;i++)
//set to false if necessary
}
return true;
};
功能游戏(网格){
this.gridName=“Grid-”+Grid.name;
};
game.prototype.isWinner=函数(){
对于(var i=0;i
我已经可以在控制器中获取网格实例。我的问题是,我无法在游戏工厂中引用网格实例。或者换句话说,我需要能够引用游戏工厂中的类属性/方法,然后我可以在控制器中实例化这两个(网格,游戏),并将网格实例传递给游戏实例。我用您的代码编辑了我的答案。我试着按你的意愿去做,但我不确定。这是未经测试的。您的myApp
中将存在多少Grid
和Game
实例?从角度工厂返回函数并不是它的本意,但因为在JavaScript中函数是第一类对象,所以它可以工作。工厂的名称有点误读。
myApp.factory('Grid', function () {
'use strict';
function Grid(sizeX, sizeY, gridName) {
var
i, numCells = sizeX * sizeY;
this.cells = [];
this.name = gridName;
for (i = 0; i < numCells; i++) {
this.cells.push(i);
}
}
return Grid;
});
myApp.factory('Game', function () {
'use strict';
function Game(grid){
this.gridName = "Grid : " + grid.name;
};
Game.prototype = {
isWinner: function () {
var i, l;
for (i = 0, l = grid.cells.length; i < l; i++) {
//do something with each cell in the grid
}
return true;
}
};
return Game;
});
myApp.controller('myCtrl', ['$scope', 'Grid', 'Game',
function ($scope, Grid, Game) {
'use strict';
var
grid = new Grid(3, 3, "Test Grid"),
game = new Game(grid);
$scope.myLength = grid.cells.length;
$scope.myGridName = grid.name;
$scope.myGameName = game.gridName;
$scope.myWinner = game.isWinner();
}]);
myApp.factory('game', ['grid', function (_grid_) {
var grid = _grid_;
//TODO: instead of passing the parameters for the grid
//into the game, I would prefer to pass the grid "class"
//But that doesn't seem possible since java script
//doesn't recognize the "class" properties until the
//object is instantiated
function game(sizeX, sizeY, gridName){
this.gameGrid = new grid(3, 3, "Test Grid");
this.gridName = "Grid - " + this.gameGrid.name;
};
game.prototype.isWinner = function () {
for(var i=0; i< this.gameGrid.cells.length;i++){
//set to false if necessary
}
return true;
};
return game;
}]);
function MyCtrl($scope, grid, game) {
var aGame = new game(3, 3, "Test Grid");
$scope.myGridName = aGame.gameGrid.name;
$scope.myLength = aGame.gameGrid.cells.length;
$scope.myGameName = aGame.gridName;
$scope.myWinner = aGame.isWinner();
}
function game(grid){
this.gridName = "Grid - " + grid.name;
};
game.prototype.isWinner = function () {
for(var i=0; i< grid.cells.length;i++)
//set to false if necessary
}
return true;
};