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

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


当前回答

JavaScript中的变量赋值可以创建全局变量。如果一个变量在函数中被赋值,并且没有在相同的作用域中声明为var,那么它将隐式声明为global。

function foo() {
  x = "juhu";  // creates a global variable x!
  var y = "kinners"
}

foo();
alert(x); // alerts "juhu"
alert(y); // alerts undefined

注意,var语句也可以在给变量赋值后使用:

function foo() {
  x = 12;
  var x; // x is now local
  return x;
}

alert(foo()); // will alert 12;
alert(x); // will alert undefined

其他回答

在C:

warning C4013: 'myfunc' undefined; assuming extern returning int

我记得由于某些原因没有看到警告(在一些遗留代码中有太多警告?),并且困惑于为什么从int转换会在使用非int返回函数时导致编译器错误。

编译器假设这样的东西是相当出乎意料的。

在流行性腮腺炎中,你可以有一个带偏移的GOTO。如果你有(我的腮腺炎已经生锈了…)

some_label if x=1 do_something
           else  do_something_else

然后是代码

           goto some_label+1

将跳转到ELSE语句…

不久前,当我在CS课上第一次发现C语言时,看到指针的行为方式是非常奇怪的。我们只是编写程序并猜测它会做什么,直到它们得到正确的行为

C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.

JavaScript日期全是WTF。

var d = new Date("1/1/2001");

var wtfyear = d.getYear(); // 101 (the year - 1900)
// to get the *actual* year, use d.getFullYear()

var wtfmonth = d.getMonth(); // 0
// months are 0-based!