JavaScript 中最伟大的双关语
The Greatest Pun in JavaScript

原始链接: https://shukla.io/blog/2026-09/pun.html

作者描述了一个利用 JavaScript 标签模板字面量(tagged template literals)创造的巧妙技术“双关语”。在语言学中,双关语是指一段文本可以通过两种不同的方式进行解读。作者通过编写一个 `prompt` 标签函数,使得开发者能够使用 `${{ variable }}` 这种语法来编写代码。 对于人类读者而言,`${{ url }}` 看起来就像许多模板语言中常见的标准占位符。然而,对于 JavaScript 引擎来说,这根本不是什么模板标签,而是一个包含对象字面量简写属性的模板字面量:`${ { url } }`。由于 JavaScript 会在将内容传递给标签函数之前先对其进行求值,因此该函数既能接收到为可观测性工具(如 Helicone)标注字符串所需的数据,又能让开发者享受到简洁且熟悉的语法。通过利用 JavaScript 对象简写与模板字面量语法的交集,作者创造出的代码既能作为一方的功能性工具,又能作为另一方熟悉的视觉模式——这在编程领域确实称得上是一种“双关”。

一篇名为《JavaScript 中最伟大的双关语》的 Hacker News 帖子在评论区引发了关于语言学和文字游戏的简短讨论。 一位用户强调了波兰诗人米科瓦伊·森普-沙任斯基(Mikołaj Sęp-Szarzyński)的作品,指出他精通重组语序,在保持语法正确的同时,能实现复杂且令人惊叹的诗歌效果——得益于斯拉夫语言灵活的特性,他得以实现这一创举。 另一位评论者澄清说,原文开头引用的莎士比亚名句在技术上属于“逆序叠句”(antimetabole),而非双关语。最后,第三位用户冷淡地评价了原帖的质量,称自己“确实感到被惩罚了”。
相关文章

原文
The Greatest Pun: JavaScript’s ${{ }} tagged template literal

Shakespeare’s chock-full of puns, no doubt as a sign of his mastery and love of words.

Rarely can you swap two words to form a correct grammatically sentence, yet he played with words like a magician.

Better a witty fool than a foolish wit.

Twelfth Night

In the example above, the noun “fool” flips to the adjective “foolish”, while the adjective “witty” flips to the noun “wit”. The simplest puns leverage the rules of grammar to change the meaning of words.

Then there are puns that play with phrases in a way that leverages context outside the grammar to change the meaning. Here’s one of my favorites:

Put out the light, and then put out the light.

Othello

The first phrase literally means to extinguish a candle, but the second is a metaphor for killing someone (in this case, Desdemona).

The formal dictionary definition of a pun usually talks about the “humorous” effect.

pun /pʌn/ noun
The use of a word in such a way as to suggest two or more meanings or different associations, or of two or more words of the same or nearly the same sound with different meanings, so as to produce a humorous effect; a play on words.

Oxford English Dictionary

Now, I very well believe humor is in the eye of the human. What’s funny to me isn’t always funny to you. But I’d like to introduce you to the greatest pun I’ve ever discovered. It isn’t in English. Natural languages and programming languages have one property in common: grammar. You’ve already seen what a pun can do with that.

Tagged Template Literal

First, a quick background on the grammar. In JavaScript, there’s a thing called a tagged template literal that lets you do powerful string manipulation like this:

tag function, which processes the template literal to remove excessive indentation. A tag function receives the literal’s text pieces and the evaluated ${...} values as separate arguments, so it can do whatever it likes with each.

Templating Languages

Most templating languages let you write the template in the same medium as the end product: text with holes where the variables go. Any time your controls live in the same system they are controlling, things get interesting. There is some Gödel incompleteness side-quest worth exploring here. But I digress. Here’s a quick example of a template:

hi {{ name }}

The {{ }} syntax is very common. In fact, the following templating languages use it:

Prompt Template Literal

Check this out. We can define a tag function called prompt that looks like it’s accepting {{ }} templates.

Justin’s PR, my PR). I thought the pun was funny. Justin was happy to include it.

A pun is one string with two parses. In Grammar models are back, baby! I take that seriously and hand back a probability distribution over them. In Spatial languages I make it worse and hand the parser a second dimension.

联系我们 contact @ memedata.com