在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

不知道这是不是一个功能。对一些人来说,是的,但对另一些人来说,这可能是一种令人讨厌的行为。不管怎样,我认为这是值得一提的。

在Python中,内置函数round()在Python 2x和Python 3x之间的行为略有不同。

对于Py 2x,

>>> round(0.4)
0.0
>>> round(0.5)
1.0
>>> round(0.51)
1.0
>>> round(1.5)
2.0

对于Py 3x,

>>> round(0.4)
0
>>> round(0.5)
0
>>> round(0.51)
1
>>> round(1.5)
2

我只是不熟悉Py 3x中的round()与0的工作方式。

Py 2x和Py 3x中round()的文档。

其他回答

我很惊讶没有人提到大多数类c语言中丑陋的开关case实现

switch (someInt) {
    case 1:
    case 2: System.out.println("Forgot a break, idiot!");
    case 3: System.out.println("Now you're doing the wrong thing and maybe need hours to find the missing break muahahahaha");
            break;
    default: System.out.println("This should never happen -,-");        
}

好在新语言正确地实现了它。

在Coldfusion中,任何地方的变量都被视为全局变量,无论它们放在哪里。

<cffunction name="one" returntype="void">
    <cfset var wtf="coldfusion">
    <cfinvoke method="second">
</cffunction>

<cffunction name="two" returntype="void">
    <cfoutput>#wtf#</cfoutput>
</cffunction>

在JavaScript中,void不是关键字,不是类型声明,也不是变量名,也不是函数,也不是对象。Void是一个前缀操作符,类似于-、——、++和!你可以给任何表达式加上前缀,这个表达式的值将是undefined。

它经常被用在bookmarklet和内联事件处理程序中,比如下面这个比较常见的例子:

<a href="javascript:void(0)">do nothing</a>

在这个例子中使用它的方式使它看起来像一个函数调用,而实际上它只是一种获得原始未定义值的过于聪明的方法。大多数人并没有真正理解JavaScript中void的真正本质,这可能会导致许多讨厌的bug和奇怪的意想不到的事情发生。

不幸的是,我认为void操作符是在JavaScript中获得未定义值的唯一真正保证的方法,因为未定义,正如在另一个回答中指出的,是一个可以重新赋值的变量名,而{}。a可以被Object.prototype.a = 'foo'打乱

更新:我想到了另一种生成undefined的方法:

(function(){}())

嗯,有点啰嗦,返回“undefined”是它的目的就更不清楚了。

有趣的自动装箱和Java中的整数缓存:

Integer foo = 1000;
Integer bar = 1000;

foo <= bar; // true
foo >= bar; // true
foo == bar; // false

//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:

Integer foo = 42;
Integer bar = 42;

foo <= bar; // true
foo >= bar; // true
foo == bar; // true

解释

快速浏览一下Java源代码将会出现以下内容:

/**
 * Returns a <tt>Integer</tt> instance representing the specified
 * <tt>int</tt> value.
 * If a new <tt>Integer</tt> instance is not required, this method
 * should generally be used in preference to the constructor
 * {@link #Integer(int)}, as this method is likely to yield
 * significantly better space and time performance by caching
 * frequently requested values.
 *
 * @param  i an <code>int</code> value.
 * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
 * @since  1.5
 */
public static Integer valueOf(int i) {
    if (i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

注意:IntegerCache。High默认为127,除非由属性设置。

自动装箱的情况是,foo和bar都是从缓存中检索到相同的整数对象,除非显式创建:例如,foo = new integer(42),因此在比较引用是否相等时,它们将为真而不是假。比较Integer值的正确方法是使用.equals;

VBScript的日期/时间文字(为什么这个仍然如此罕见?):

mydate = #1/2/2010 5:23 PM#

If mydate > #1/1/2010 17:00# Then ' ...

编辑:日期文字是相对的(那么它们在技术上是文字吗?):

mydate = #Jan 3# ' Jan 3 of the current year

VB。NET,因为它是编译的,所以不支持相对日期文字。只支持日期或时间字面量,但缺失的时间或日期被假定为零。

编辑[2]:当然,有一些奇怪的极端情况会出现相对日期……

mydate = #Feb 29# ' executed on 2010-01-05, yields 2/1/2029