Javascript ES2015+;流:自引用(循环?)枚举

Javascript ES2015+;流:自引用(循环?)枚举,javascript,collections,circular-dependency,Javascript,Collections,Circular Dependency,使用rollup、buble、flow remove类型 是否可以创建用于棋盘表示的类实例枚举(作为类型),如下所示: // a Ref is a class or a type class Ref { /* ... */ } // Refs is an ENUM Refs.forEach((ref: Ref, key: string) => { console.log(key) // outputs: "a1", ..., "h8" successively }) // type

使用rollup、buble、flow remove类型

是否可以创建用于棋盘表示的类实例枚举(作为类型),如下所示:

// a Ref is a class or a type
class Ref { /* ... */ }

// Refs is an ENUM
Refs.forEach((ref: Ref, key: string) => {
 console.log(key) // outputs: "a1", ..., "h8" successively
}) 

// type checking should work
typeof Refs.a1 === Ref // true
// etc...
typeof Refs.h8 === Ref // true 

// move(ref) --> ref will work
Refs.a1.move(7, 7) === Refs.h8 // true
Refs.h8.move(-7, -7) === Refs.h8 // true

// with...
Refs.a1.move(0, 0) === Refs.a1 // true

// void reference
Refs.a1.move(-1, -1) === null
// or
Refs.a1.move(-1, -1) === Refs.EMPTY
一个可能的模块化实现是使用初始化代码将Ref类和Refs集合打包在同一个文件中,就像Enuify lib那样。。。但是如何使Ref#move方法正常工作

同:

TicTacToe.X.us =TicTacToe.X
TicTacToe.X.them =TicTacToe.O 
TicTacToe.O.us =TicTacToe.O
TicTacToe.O.them =TicTacToe.X  

像这样的东西,是完美的,但对我来说很好

type TF = 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'
type TR = '1'|'2'|'3'|'4'|'5'|'6'|'7'|'7'

type TRefDef = {
  file: TF,
  fidx: number,
  rank: TR,
  ridx: number
}

interface IRef {
  move (df: number, dr: number) : IRef
}

const FILES: Array <TF> = 'abcdefgh'.split('')
const RANKS: Array <TR> = '12345678'.split('')

const all: {
  [key:string] : IRef
} = {}

const compute = function(fidx: number, ridx: number): IRef {
  const file: TF = FILES[fidx]
  const rank: TR = RANKS[ridx]

  return all[file + rank]
}

const select = function(key: string) : IRef {
  return all[key]
}

const get = function(arg1: string | number, arg2: ?number) : IRef {
  if(arguments.length ===  1) {
    return select (arg1)
  }

  if(arguments.length ===  2) {
    return compute (arg1, arg2)
  }
}

const each  = function (callback) {
  Object.keys(all).forEach((key, idx) => {
    callback.call(this, all[key], idx)
  })
}

class Ref implements IRef {

  constructor (refdef: TRefDef) {
    this.file = refdef.file
    this.fidx = refdef.fidx
    this.rank = refdef.rank
    this.ridx = refdef.ridx

    this.key = this.file + this.rank
  }

  toString() : string {
    return 'Ref: ' + '(' + this.fidx + ',' + this.ridx + ')' + ' ' + this.file + this.rank
  }

  move (df: number, dr: number) : Ref {
    let f = FILES.indexOf(fidx)
    let r = RANKS.indexOf(ridx)

    f += df
    r += dr

    return all[FILES[f] + RANKS[r]]
  }
}

FILES.forEach((file, fidx) => {
  RANKS.forEach( (rank, ridx) => {
    const key: string = file + rank
    const ref: Ref = new Ref({ file, fidx, rank, ridx })

     all[key] = ref
  })
})


Ref.empty = new Ref('', -1, '', -1)

const Refs = { compute, select, each, get }
// let  f = { compute, each, selection  }
// console.log(f)

// export  { compute, each, select, Ref  }
export  { Refs, Ref  }
type TF='a''b''c''d''e''f''g''h'
类型TR='1'|'2'|'3'|'4'|'5'|'6'|'7'
类型TRefDef={
档案:TF,,
fidx:编号,
职级:TR,,
ridx:数字
}
接口IRef{
移动(df:编号,dr:编号):IRef
}
常量文件:数组='abcdefgh'。拆分(“”)
常量列组:数组='12345678'。拆分(“”)
const all:{
[键:字符串]:IRef
} = {}
常量计算=函数(fidx:number,ridx:number):IRef{
常量文件:TF=文件[fidx]
常数秩:TR=秩[ridx]
返回所有[文件+等级]
}
常量选择=函数(键:字符串):IRef{
返回所有[键]
}
const get=函数(arg1:string | number,arg2:?number):IRef{
if(arguments.length==1){
返回选择(arg1)
}
if(arguments.length==2){
返回计算(arg1、arg2)
}
}
const each=函数(回调){
Object.keys(all).forEach((key,idx)=>{
callback.call(this,all[key],idx)
})
}
类Ref实现IRef{
构造函数(refdef:TRefDef){
this.file=refdef.file
this.fidx=refdef.fidx
this.rank=refdef.rank
this.ridx=refdef.ridx
this.key=this.file+this.rank
}
toString():字符串{
返回“Ref:”+”(“+this.fidx+”,“+this.ridx+”)”+“+this.file+this.rank
}
移动(df:编号,dr:编号):参考{
设f=FILES.indexOf(fidx)
设r=RANKS.indexOf(ridx)
f+=df
r+=dr
返回所有[文件[f]+列组[r]]
}
}
FILES.forEach((文件,fidx)=>{
等级。forEach((等级,ridx)=>{
常量键:字符串=文件+秩
const ref:ref=new ref({file,fidx,rank,ridx})
全部[键]=ref
})
})
Ref.empty=新的Ref(“”,-1,,-1)
const Refs={compute,select,each,get}
//设f={compute,each,selection}
//控制台日志(f)
//导出{compute,each,select,Ref}
导出{Refs,Ref}

使用接口解决循环依赖项。。请看下面我的答案。。。