# Is it safe to extract functions from \`browser\` API? Do they internally use \`this\`?

**URL:** <https://discourse.mozilla.org/t/is-it-safe-to-extract-functions-from-browser-api-do-they-internally-use-this/43011>\
**Category:** Development\
**Created:** [July 25, 2019, 7:43am UTC](https://discourse.mozilla.org/t/is-it-safe-to-extract-functions-from-browser-api-do-they-internally-use-this/43011 "2019-07-25T07:43:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [July 25, 2019, 7:43am UTC](https://discourse.mozilla.org/t/is-it-safe-to-extract-functions-from-browser-api-do-they-internally-use-this/43011/1 "2019-07-25T07:43:19Z")

</div>

Can I safely do this:

```auto
export const tr = browser.i18n.getMessage;

```

To have a short function name for translating?  
I know this will break the “`this`” inside the `getMessage`, but if it’s not using it, it will work just fine.

**So my questions:**  
Is this a good idea in the first place?  
Is this an implementation detail that I can’t rely on?  
Would it also work on other `browser` API, like `storage` / `tabs`, etc…? (I will probably not use it, just curious).

**EDIT:**  
Maybe there is a better way using [Proxy API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)?

---

<div class="post-metadata">

**Author:** ![luckyrat](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/luckyrat/32/21009_2.png) [@luckyrat](https://discourse.mozilla.org/u/luckyrat)\
**Post date:** [August 7, 2019, 10:51am UTC](https://discourse.mozilla.org/t/is-it-safe-to-extract-functions-from-browser-api-do-they-internally-use-this/43011/2 "2019-08-07T10:51:49Z")

</div>

Why not just define a wrapper function instead?

```auto
export const tr = (...args) => browser.i18n.getMessage(...args);

```

> [@juraj.masiar](#):
>
> Is this an implementation detail that I can’t rely on?

Theoretically, of course you cannot rely on the private implementation of any function. Practically, on the one hand the webextensions dev team appears to be very small so it’s unlikely to change any time soon, but there’s ongoing work within Firefox to switch i18n to Fluent so perhaps this will force an implementation change at some point in the next few years.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 7, 2019, 11:38am UTC](https://discourse.mozilla.org/t/is-it-safe-to-extract-functions-from-browser-api-do-they-internally-use-this/43011/3 "2019-08-07T11:38:57Z")

</div>

Thank you for the info!  
**You are right** , the wrapper will always be the best option.

The reason why I was looking for this “nasty shortcuts” was that I’ve started to refactoring my whole codebase into TypeScript and I had issues finding a proper type for the `...args` parameter.  
I don’t know what was I thinking… sometimes I just get this “out of box” crazy ideas.

But anyway, after two weeks of fulltime TypeScript refactoring this kind of things are a piece of cake 😃 :

```auto
const tr = (msg: string, substitutions?: string | string[]) => browser.i18n.getMessage(msg, substitutions);

```
