在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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;
其他回答
在我看来,这在c++中是不允许的:
class A {
public:
virtual string foo(){return "A::foo";}
};
class B : public A {
public:
virtual string foo(){return "B::foo";}
};
int main () {
B* b = new B();
// In my opinion the following should not be allowed
cout << b->A::foo() << endl; // Will print "A::foo"
}
这似乎是正确的,但这意味着如果不允许子类的用户调用原始方法而不是新方法,就不能重写方法。
只要考虑一个集合的子类,当向集合本身添加一个元素时,您希望增加元素的数量。
一个合乎逻辑的解决方案是重写add()方法,在添加元素之前增加计数器,但是新集合的用户可以使用旧方法向其添加元素,这样就绕过了增量,导致元素计数器与集合的实际元素数量不一致。
这在Java中是不可能的。
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.
c++(或Java)中没有封装的事实。任何对象都可以违反任何其他对象的封装,破坏它的私有数据,只要它是相同的类型。例如:
#include <iostream>
using namespace std;
class X
{
public:
// Construct by passing internal value
X (int i) : i (i) {}
// This breaks encapsulation
void violate (X & other)
{
other.i += i;
}
int get () { return i; }
private:
int i;
};
int main (int ac, char * av[])
{
X a(1), b(2), c(3);
a.violate (c);
b.violate (c);
cout << c.get() << endl; // "6"
}
任何东西都会自动将任何类名和成员名复数或单数。
例如,Linq-to-Sql
有些奇怪——VBScript有一个Null关键字和一个Nothing关键字(Null是丢失的数据,Nothing是丢失的对象)。为什么不只有一个关键词…?大多数其他语言似乎都可以用一个!
Visual Basic 6.0当然还有“经典ASP”代码(因为它使用VBScript)也有同样的奇怪之处。在Visual Basic的新旧版本中,我们都有DBNull。
然而,这种情况正在改善,就像Visual Basic一样。NET Null终于消失了,所以Null不再使用,只有Nothing和DBNull被使用。