저는 C# 배경을 가지고 있으며 Angular JS를 탐색하기 시작합니다. 그리고이 기사에서 내 공장 클래스/개체를 기반으로합니다 : https://medium.com/opinionated-angularjs/2e6a067c73bc각도/자바 스크립트 - 객체를 인스턴스화하기 전에 "클래스"속성을 참조 하시겠습니까?
그래서 2 개의 공장 클래스가 있지만 다른 팩토리에 하나의 팩토리 인스턴스를 참조하고 싶습니다. 그러나 자바 스크립트는 타입 화 된 언어가 아니기 때문에 하나의 팩토리의 프로퍼티와 메소드는 "컴파일"타임에 다른 팩토리에서 사용할 수 없습니다.
이 피디를 확인하십시오. http://jsfiddle.net/Frinkahedron/PxdSP/1337/ var myApp = angular.module ('myApp', []); 예상대로
myApp.factory('grid', function() {
// Constructor, with parameters
function grid(sizeX, sizeY, gridName) {
//public properties
this.cells = new Array();
this.name = gridName;
//load cells
var numCells = sizeX * sizeY;
for (var i = 0; i < numCells; i++) {
this.cells.push(i);
}
}
//Return the constructor function
return grid;
});
myApp.factory('game', ['grid', function (_grid_) {
//private reference for the grid factory
var grid = _grid_;
function game(){
//do some setup with grid reference
this.gridName = "Grid : " + grid.gridName;
};
game.prototype.isWinner = function() {
//iterate cells to see if game has been won
//this loop doesn't cause "compile" errors
for (var c in grid.cells){
//do something with each cell in the grid
}
//this loop doesn't work due to grid.cells.length
//because there is no length property of undefined
//uncomment to see it blow up
//for(var i=0; i< grid.cells.length;i++){}
return true;
};
return game;
}]);
function MyCtrl($scope, grid, game) {
var g = new grid(3, 3, "Test Grid");
$scope.myLength = g.cells.length;
$scope.myGridName = g.name;
//how to pass the grid reference to the game?
//if i pass the grid in the constructor of the game,
//it still doesn't work because javascript doesn't have types
//and the grid.cells (and other grid property references) are
//problematic
var a = new game();
$scope.myGameName = a.gridName;
$scope.myWinner = a.isWinner();
}
그리드 공장 노력하고 있습니다,하지만 난 게임 공장에서 그리드의 인스턴스를 참조하는 방법을 알아낼 수 없습니다. 나는 "개체"그리드를 게임 생성자에 전달하려고 시도했지만 자바 스크립트는 타입이 지정된 언어가 아니기 때문에 그리드 속성/메소드는 게임 팩토리에서 정의되지 않았습니다.
난 이미 컨트롤러 그리드의 인스턴스를 얻을 수 있습니다. 내 문제는 게임 공장에서 그리드의 인스턴스를 참조 할 수 없다는 것입니다. 다른 말로하면 게임 팩토리에서 클래스 속성/메서드를 참조 할 수 있어야합니다. 그런 다음 컨트롤러에서 그리드, 게임을 모두 인스턴스화하고 그리드 인스턴스를 게임 인스턴스에 전달할 수 있습니다. – user3551865
나는 당신의 코드로 나의 대답을 편집했다. 나는 그것을 당신이 의도 한대로 만들려고했지만 확실하지 않습니다. ** ** 테스트되지 않았습니다 **. 얼마나 많은'Grid'와'Game' 인스턴스가'myApp'에 존재할 것입니까? 앵귤러'factory'에서 함수를 반환하는 것은 의도 한 방식이 아니지만, JavaScript 함수에서는 first class 객체이기 때문에 작동 할 것입니다. 'factory'라는 이름은 약간의 실수입니다. – phylax