在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
COMEFROM是我见过的最奇怪,也可能是最没用的语言功能。
其次是三元运算符,因为它违反了优化的第一条规则。它带来的危害大于它解决的问题。它的危害更大,因为它使代码可读性更差。
并不是一个真正的语言功能,但有趣/很棒的功能使用是Duff的设备。
其他回答
PHP的列表构造:
$array = array(0,1,2);
list (,,$x) = $array;
$x == 2; // true
在JavaScript中,方法的结果取决于所放置的样式花括号。这是K&R风格,其中括号放在方法签名之后和return语句之后:
var foo = function() {
return {
key: 'value'
};
}
foo() // returns an object here
现在,如果我将这段代码格式化为Allman风格,其中括号总是放在新行上,结果是不同的:
var foo = function()
{
return
{
key: 'value'
};
}
foo() // returns undefined here
如何来吗?在JavaScript中,如果您不自己做,语言会自动在每行末尾放置分号。所以在最后一个代码片段中真正发生的是这样的:
var foo = function()
{
return; // here's actually a semicolon, automatically set by JavaScript!
{
key: 'value'
};
}
因此,如果调用foo(),方法中的第一个语句将是一个返回语句,该语句将返回undefined,并且不会执行后面的其他语句。
在C中,a[b][C]与C [b[a]]完全相同。
A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.
在PHP中:
echo 'foo' == 0; // echos '1'
echo 'foo' == true; // echos '1'
echo 0 == true; // echos '0'
$foo = 'foo';
echo $foo['bar'] // echos 'f'
PHP有一些最烦人的类型强制转换…