Javascript DRY 模式 - 函数
Javascript DRY Pattern - function
OO 新手,我正在制作一个游戏,我将其作为我的 initialState
:
let initialState = {
grid : null,
player : {
coordinates : null,
health : 100 ,
weapon : "Stick",
expLevel : 1
},
enemies : [],
weapons : [],
items : []
}
当加载 DOM 时,我能够正确地初始化 weapons
和 items
。但是,除了初始化的对象文字外,这两个属性共享确切的功能:
initialState.weapons = placeWeapons(3);
initialState.items = placeItems(2);
在哪里
放置武器
function placeWeapons(numberofWeapons){
let weapons = [];
let availableSpots = [];
let placementWeapons = []; //list of coordinates that will place weapons
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the weapons. When placed, it will update initialState.occupiedCoordinates
while( placementWeapons.length < numberofWeapons ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementWeapons.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementWeapons.forEach( coord => {
let weapon = {
name : "Weapon Name",
coords : coord,
damage : 3
}
weapons.push(weapon)
})
return weapons;
}
placeItems
function placeItems(numberofItems){
let items = [];
let availableSpots = [];
let placementItems = []; //list of coordinates that will place items
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the items. When placed, it will update initialState.occupiedCoordinates
while( placementItems.length < numberofItems ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementItems.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementItems.forEach( coord => {
let item = {
name : "Item Name",
coords : coord,
health : 3
}
items.push(item)
})
return items;
}
我怎样才能擦干这个图案?
我看到的唯一显着差异是放置的东西。所以合乎逻辑的做法是提取它并将其作为参数传递:
// WARNING: Untested code:
function placeThings(thing, numberofThings){
let things = [];
let availableSpots = [];
let placementItems = []; //list of coordinates that will place things
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the items. When placed, it will update initialState.occupiedCoordinates
while( placementItems.length < numberofThings ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementItems.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementItems.forEach( coord => {
things.push(Object.create(thing))
})
return things;
}
现在您可以:
// Weapons:
placeThings({
name : "Weapon Name",
coords : coord,
damage : 3
},50);
// Items:
placeThings({
name : "Item Name",
coords : coord,
health : 3
},100);
如果你想让东西随机放置,你可以只传递一个函数而不是一个对象:
// WARNING: Untested code:
function placeThings(thingGenerator, numberofThings){
let things = [];
/*
* bla bla..
*/
placementItems.forEach( coord => {
things.push(thingGenerator())
})
return things;
}
所以你会做类似的事情:
placeThings(makeWeapon, 50);
placeThings(makeItem, 100);
OO 新手,我正在制作一个游戏,我将其作为我的 initialState
:
let initialState = {
grid : null,
player : {
coordinates : null,
health : 100 ,
weapon : "Stick",
expLevel : 1
},
enemies : [],
weapons : [],
items : []
}
当加载 DOM 时,我能够正确地初始化 weapons
和 items
。但是,除了初始化的对象文字外,这两个属性共享确切的功能:
initialState.weapons = placeWeapons(3);
initialState.items = placeItems(2);
在哪里 放置武器
function placeWeapons(numberofWeapons){
let weapons = [];
let availableSpots = [];
let placementWeapons = []; //list of coordinates that will place weapons
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the weapons. When placed, it will update initialState.occupiedCoordinates
while( placementWeapons.length < numberofWeapons ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementWeapons.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementWeapons.forEach( coord => {
let weapon = {
name : "Weapon Name",
coords : coord,
damage : 3
}
weapons.push(weapon)
})
return weapons;
}
placeItems
function placeItems(numberofItems){
let items = [];
let availableSpots = [];
let placementItems = []; //list of coordinates that will place items
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the items. When placed, it will update initialState.occupiedCoordinates
while( placementItems.length < numberofItems ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementItems.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementItems.forEach( coord => {
let item = {
name : "Item Name",
coords : coord,
health : 3
}
items.push(item)
})
return items;
}
我怎样才能擦干这个图案?
我看到的唯一显着差异是放置的东西。所以合乎逻辑的做法是提取它并将其作为参数传递:
// WARNING: Untested code:
function placeThings(thing, numberofThings){
let things = [];
let availableSpots = [];
let placementItems = []; //list of coordinates that will place things
let grid = initialState.grid;
//collect whats available of coords that are not taken in initialState.occupiedCoordinates
grid.forEach( (row, rowIndex) => (
row.forEach( (cell, colIndex) => {
if (cell === 1){
availableSpots.push([rowIndex,colIndex])
}
})
))
//lets place the items. When placed, it will update initialState.occupiedCoordinates
while( placementItems.length < numberofThings ){
let randCoords = availableSpots[Math.floor(Math.random() * availableSpots.length)];
if (grid[randCoords[0]][randCoords[1]] === 1){
placementItems.push(randCoords);
grid[randCoords[0]][randCoords[1]] = 0
}
}
placementItems.forEach( coord => {
things.push(Object.create(thing))
})
return things;
}
现在您可以:
// Weapons:
placeThings({
name : "Weapon Name",
coords : coord,
damage : 3
},50);
// Items:
placeThings({
name : "Item Name",
coords : coord,
health : 3
},100);
如果你想让东西随机放置,你可以只传递一个函数而不是一个对象:
// WARNING: Untested code:
function placeThings(thingGenerator, numberofThings){
let things = [];
/*
* bla bla..
*/
placementItems.forEach( coord => {
things.push(thingGenerator())
})
return things;
}
所以你会做类似的事情:
placeThings(makeWeapon, 50);
placeThings(makeItem, 100);