在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在JavaScript中,下面的构造
return
{
id : 1234,
title : 'Tony the Pony'
};
返回undefined是一个语法错误,因为在返回后的换行上偷偷地插入了分号。以下工作正如你所期望的那样:
return {
id : 1234,
title : 'Tony the Pony'
};
更糟糕的是,这个也可以(至少在Chrome中):
return /*
*/{
id : 1234,
title : 'Tony the Pony'
};
下面是同一个问题的一个变种,它不会产生语法错误,只是无声地失败了:
return
2 + 2;
其他回答
在Java中:
int[] numbers() {
return null;
}
可以写成:
int numbers() [] {
return null;
}
在JavaScript(和Java)中,你可以转义这样有趣的字符:
var mystring = "hello \"world\"";
如果你想把回车放到字符串中,那是不可能的。你必须像这样使用\n:
var mystring = "hello, \nworld";
这是正常的,也是意料之中的——至少对于一种编程语言来说。奇怪的是你也可以像这样转义一个实际的回车:
var mystring = "hello, \
world";
我最喜欢的奇怪的C语言是5[“Hello World”],但因为已经发布了,我最喜欢的第二个奇怪的是Windows版本结构初始化黑客:
void someWindowsFunction() {
BITMAPINFOHEADER header = {sizeof header};
/* do stuff with header */
}
这条微妙的线条达到了以下效果:
Declares a BITMAPINFOHEADER structure Concisely sets the "size" member of the structure, without hardcoding a size constant (since many Window structures, including BITMAPINFOHEADER, follow the convention of specifying the size of the structure as the first member} Declares the version of the structure (since many Windows structures, including BITMAPINFOHEADER, identify their version by the declared size, following the convention that structures definitions are append-only) Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).
JavaScript是面向对象的,对吧?因此,在文字字符串和数字上运行方法应该是可行的。比如"hello". touppercase()和3.toString()。第二个是语法错误,为什么?因为解析器期望一个数字后面跟一个点是一个浮点字面值。这不是WTF, WTF是你只需要再加一个点就可以了:
3..toString()
原因是字面上的3。被解释为3.0,3.0. tostring()工作正常。
腓backticks
从http://www.php.net/manual/en/language.operators.execution.php
PHP支持一种执行操作符:反撇号(' ')。注意,这些不是单引号!PHP将尝试作为shell命令执行反勾号的内容;输出将被返回(即,它不会简单地转储到输出;它可以赋值给一个变量)。
$output = `ls -al`;
echo "<pre>$output</pre>";
在代码中发现“instead of”是“相当容易的”。
这也很有趣:
经过一番折腾,我得出结论,反勾运算符(和shell_exec)的返回缓冲区有限。我的问题是,我正在处理一个超过50万行的文件,收到的回复远远超过10万行。短暂的停顿之后,我收到了大量来自grep的关于管道关闭的错误。