TypeScript中的Friend类 [英] Friend class in TypeScript

查看:142
本文介绍了TypeScript中的Friend类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,有一个称为朋友类"的东西.据我所知,TypeScript/JavaScript中没有这样的东西.有没有办法在TypeScript/JavaScript中模拟好友类的这种行为?

In C++ there is something called a friend class. As far as I know, there's no such thing in TypeScript/JavaScript. Is there a way to simulate such behavior of friend class in TypeScript/JavaScript?

为了提供更好的背景信息(如果需要),说明我为什么要做的事情,我制作了一些有趣的小游戏(并学习了一些东西),然后尝试进行

To give better context (if needed) of why and what I try to do, I make some small game for fun (and to learn stuff) and try to do this. At the moment I just use public methods and everything works, but I would like to limit accessibility of those methods to just one other class. I use TypeScript, if that helps.

推荐答案

TypeScript仅提供 friend

TypeScript only provides protected and private access modifiers. It does not currently have something like friend or internal.

要获得类似的效果:如果将代码打包为库并为其发出.d.ts声明文件,则可以在不想让外人使用的属性上使用/** @internal */ JSDoc注释,以及指定--stripInternal 编译器选项.这将导致导出的声明忽略这些属性.

To get similar effects: if you are packaging your code as a library and emitting .d.ts declaration files for it, you can use the /** @internal */ JSDoc annotation on the properties you don't want outsiders to use, along with specifying the --stripInternal compiler option. This will cause the exported declaration to leave out these properties.

进行类似操作的另一种方法是提出一个由您的类实现的公共interface,然后仅将该类导出为公共接口.例如:

Another way to do something similar is to come up with a public interface which your class implements, and then only export the class as the public interface. For example:

// public interfaces
export interface UnitStatic {
  new(grid: Grid, x: number, y: number): Unit;
}
export interface Unit {
  move(x: number, y: number): void;
}
export interface GridStatic {
  new(): Grid;
  NUM_CELLS: number;
  CELL_SIZE: number; 
}
export interface Grid {
  // public methods on Grid
}

// private implementations
class UnitImpl implements Unit {
  constructor(private grid: GridImpl, private x: number, private y: number) {

  }
  move(x: number, y: number) {
    // ...
  }
}

class GridImpl implements Grid {
  cells: Unit[][] = [];
  constructor() {
    // ...
  }
  static NUM_CELLS = 10;
  static CELL_SIZE = 20;
}

//public exports
export const Unit: UnitStatic = UnitImpl;
export const Grid: GridStatic = GridImpl;

这很繁琐,但是却非常明确地说明了代码的哪些部分是供外部人员使用的,哪些不是.

This is tedious but makes it very explicit which parts of your code are meant for outsiders and which are not.

或者,由于上述两种方法均未真正阻止人们在运行时访问JavaScript中的私有/内部信息,因此您可以使用

Or, since neither of the above actually prevent people from accessing private/internal info in JavaScript at runtime, you could use an IIFE to really hide those things from outsiders. This might be more annoying to do in TypeScript, though, since it will probably require you to create the above public interfaces in order to typecheck propertly.

因此,有一些选项供您选择.希望他们能帮上忙.祝你好运!

So there are some options for you. Hope they help. Good luck!

这篇关于TypeScript中的Friend类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆