在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在c++
const char* wtf()
{
char buf[100];
return buf;
}
string s = ... + wtf() + ...;
在s中创建有趣的值。部分是字符串,部分是堆栈内容,与零混合,因此s.length()!=strlen(s.c_str())。 最奇怪的是,编译器在返回指向堆栈的指针时完全没有问题——如果他在那里添加了一个警告,编译器程序员的手可能会掉下来。
其他回答
下面是一个关于python的例子:
>>> print 07
7
>>> print 08
File "<stdin>", line 1
print 08
^
SyntaxError: invalid token
那不是很美吗?
尤其当你想到人类是如何写日期的时候,你会觉得很不周到,这有以下影响:
datetime.date(2010,02,07) # ok
datetime.date(2010,02,08) # error!
(原因是0x被解释为八进制,所以打印010打印8!)
Perl充满了奇怪但整洁的特性。
If可以用在语句之前或之后,就像这样:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
foreach也是如此:
print "Hello $_!\n" foreach qw(world Dolly nurse);
我一直想知道这个函数在Java Core库的Math类中的用途:
static double expm1(double x); // Returns e^x - 1.
Python 2. x
>>>True = False
>>>True
False
你真的可以让一个人变得疯狂。
可能已经说过了(也许这对一些人来说并不奇怪),但我认为这很酷:
在Javascript中,声明函数接受的参数只是为了方便程序员。通过函数调用传递的所有变量都可以通过关键字“arguments”访问。所以下面会提醒“world”:
<script type="text/javascript">
function blah(){
alert(arguments[1]);
}
blah("hello", "world");
</script>
注意,虽然这些参数看起来像是存储在数组中(因为您可以以与数组元素相同的方式访问对象属性),但事实并非如此。arguments是一个对象,而不是数组(因此,它们是存储在数值索引下的对象属性),如下例所示(typeOf函数来自Crockford的JavaScript补救页面):
argumentsExample = function(){
console.log(typeOf(arguments));
anArray = [];
console.log(typeOf(anArray));
anObject = {};
console.log(typeOf(anObject));
}
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
argumentsExample("a", "b");