为什么 Javascript `atob()` 和 `btoa()` 这样命名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33854103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why were Javascript `atob()` and `btoa()` named like that?
提问by Константин Ван
In Javascript, window.atob()
method decodes a base64string and window.btoa()
method encodes a string
into base64.
在 Javascript 中,window.atob()
method 解码base64字符串,window.btoa()
method 将 a 编码string
为base64。
Then why weren't they named like base64Decode()
and base64Encode()
?
atob()
and btoa()
don't make sense because they're not semantic at all.
那他们为什么不命名为base64Decode()
和base64Encode()
?
atob()
并且btoa()
没有意义,因为它们根本没有语义。
I want to know the reason.
我想知道原因。
采纳答案by shershen
The atob()
and btoa()
methods allow authors to transform content to and from the base64 encoding.
该atob()
和btoa()
方法允许作者进行改造的内容,并从base64编码。
In these APIs, for mnemonic purposes, the "b" can be considered to stand for "binary", and the "a" for "ASCII". In practice, though, for primarily historical reasons, both the input and output of these functions are Unicode strings.
在这些 API 中,出于助记的目的,可以认为“b”代表“二进制”,“a”代表“ASCII”。但实际上,主要出于历史原因,这些函数的输入和输出都是 Unicode 字符串。
回答by derenio
To sum up the already given answers:
总结一下已经给出的答案:
atob
stands forASCII to binary
- e.g.:
atob("ZXhhbXBsZSELCg==") == "example!^K"
- e.g.:
btoa
stands forbinary to ASCII
- e.g.:
btoa("\x01\x02\xfe\xff") == "AQL+/w=="
- e.g.:
atob
代表ASCII to binary
- 例如:
atob("ZXhhbXBsZSELCg==") == "example!^K"
- 例如:
btoa
代表binary to ASCII
- 例如:
btoa("\x01\x02\xfe\xff") == "AQL+/w=="
- 例如:
Why ASCII and binary:
为什么一个SCII和binary:
ASCII
(thea
) is the result ofbase64
encoding. A safetext composed only of a subset of ascii characters(*) that can be correctly represented and transported (e.g. email's body),binary
(theb
) is any stream of 0sand 1s(in javascript it must be represented with a string type).
ASCII
(thea
) 是base64
编码的结果。仅由可以正确表示和传输的 ascii 字符 (*) 子集组成的安全文本(例如电子邮件的正文),binary
(b
) 是0和1 的任何流(在 javascript 中,它必须用字符串类型表示)。
(*) in base64
these are limited to: A-Z
, a-z
, 0-9
, +
, /
and =
(padding, only at the end) https://en.wikipedia.org/wiki/Base64
(*)在base64
这些被限制为:A-Z
,a-z
,0-9
,+
,/
和=
(填充,仅在端部)https://en.wikipedia.org/wiki/Base64
P.S. I must admit I myself was initially confused by the naming and thought the names were swapped. I thought that b
stand for "base64 encoded string"and a
for "any string":D.
PS 我必须承认我自己最初对命名感到困惑,并认为名称被交换了。我认为b
立场“ b ase64编码字符串”,并a
为“一个纽约字符串”:d。
回答by William Hilton
I know this is old, but it recently came up on Twitter, and I thought I'd share it as it is authoritative.
我知道这是旧的,但它最近出现在 Twitter 上,我想我会分享它,因为它是权威的。
Me:
我:
@BrendanEich did you pick those names?
@BrendanEich 你选择了这些名字吗?
Him:
他:
Old Unix names, hard to find man pages rn but see https://www.unix.com/man-page/minix/1/btoa/…. The names carried over from Unix into the Netscape codebase. I reflected them into JS in a big hurry in 1995 (after the ten days in May but soon).
旧的 Unix 名称,很难找到手册页 rn 但请参阅 https://www.unix.com/man-page/minix/1/btoa/...。这些名称从 Unix 转移到 Netscape 代码库中。我在 1995 年匆忙地将它们反映到 JS 中(在五月的十天之后但很快)。
In case the Minix link breaks, here's the man page content:
如果 Minix 链接断开,这里是手册页内容:
BTOA(1) BTOA(1)
NAME
btoa - binary to ascii conversion
SYNOPSIS
btoa [-adhor] [infile] [outfile]
OPTIONS
-a Decode, rather than encode, the file
-d Extracts repair file from diagnosis file
-h Help menu is displayed giving the options
-o The obsolete algorithm is used for backward compatibility
-r Repair a damaged file
EXAMPLES
btoa <a.out >a.btoa # Convert a.out to ASCII
btoa -a <a.btoa >a.out
# Reverse the above
DESCRIPTION
Btoa is a filter that converts a binary file to ascii for transmission over a telephone
line. If two file names are provided, the first in used for input and the second for out-
put. If only one is provided, it is used as the input file. The program is a function-
ally similar alternative to uue/uud, but the encoding is completely different. Since both
of these are widely used, both have been provided with MINIX. The file is expanded about
25 percent in the process.
SEE ALSO
uue(1), uud(1).
Source: Brendan Eich, the creator of JavaScript. https://twitter.com/BrendanEich/status/998618208725684224
资料来源:Brendan Eich,JavaScript 的创造者。https://twitter.com/BrendanEich/status/998618208725684224
回答by Selfish
I can't locate a source at the moment, but it is common knowledge that in this case, the b stands for 'binary', and the a for 'ASCII'.
我目前无法找到来源,但众所周知,在这种情况下,b 代表“二进制”,而 a 代表“ASCII”。
Therefore, the functions are actually named:
因此,函数实际上被命名为:
ASCII to Binary for atob()
, and
Binary to ASCII for btoa()
.
ASCII 转二进制用于atob()
,二进制转 ASCII 用于btoa()
。
Note this is browser implementation, and was left for legacy / backwards-compatibility purposes. In Node.js for example, these don't exist.
请注意,这是浏览器实现,是为了遗留/向后兼容目的而留下的。例如,在 Node.js 中,这些都不存在。