Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: 为什么很多教程中都有 foo bar? | ||
date: 2024-01-17T09:58:58+08:00 | ||
slug: 20b75e9 | ||
categories: | ||
- Grocery | ||
lightgallery: false | ||
--- | ||
|
||
foo 和 bar 是在编程中常用的元变量(metasyntactic variables),它们是在教程和示例代码中用来代表任意的函数或者变量的名称。类似的元变量还有 baz,qux,quux,corge,grault,garply,waldo,fred,plugh,xyzzy 和 thud 等。 | ||
|
||
<!--more--> | ||
|
||
## foo 和 bar | ||
|
||
这些名称起源于 MIT,然后通过 Hacker 文化传播开来。它们主要的用途是作为示例代码的占位符,这样编程教程或者文档可以更专注于解释语法和逻辑,而不需要花费太多的精力去考虑如何命名这些函数或变量。 | ||
|
||
这就像是在其他文字中使用"John Doe" 或 "Jane Doe"来代表一个非特定的人一样。在编程的世界中,foo 和 bar 就扮演了类似的角色。 | ||
|
||
例如,如果我们要解释 JavaScript 中函数的使用,我们可能会写出这样的代码: | ||
|
||
```javascript | ||
function foo(bar) { | ||
return bar + 1; | ||
} | ||
foo(1); // 2 | ||
``` | ||
|
||
在这个例子中,foo 是一个函数名,bar 是一个参数名,它们都可以被替换为任何合法的函数名和参数名,而不改变代码逻辑。 | ||
|
||
## quick brown fox | ||
|
||
另外,还常常看到一个这样的句子: | ||
|
||
```plaintext | ||
The quick brown fox jumps over the lazy dog | ||
``` | ||
|
||
相应中文可简译为“快狐跨懒狗”,完整翻译则是“那只敏捷的棕毛狐狸跃过了那只懒狗”,是一个著名的英语全字母句,常用于测试字体显示效果和键盘是否故障。此句也常以“quick brown fox”做为指代简称。 | ||
|
||
简而言之,这些都是英文世界里一些约定俗成的用语,类似于中文里的“张三李四”、“甲乙丙丁”等等。 |