在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Perl的许多内置变量:
$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is
还有很多这样的东西。点击这里阅读完整列表。
其他回答
JavaScript是面向对象的,对吧?因此,在文字字符串和数字上运行方法应该是可行的。比如"hello". touppercase()和3.toString()。第二个是语法错误,为什么?因为解析器期望一个数字后面跟一个点是一个浮点字面值。这不是WTF, WTF是你只需要再加一个点就可以了:
3..toString()
原因是字面上的3。被解释为3.0,3.0. tostring()工作正常。
在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++中,创建受保护抽象虚基纯虚私有析构函数的能力。
这是从受保护的抽象虚拟基继承的纯虚拟私有析构函数。
IOW,只能由类的成员或友元调用的析构函数(private),在声明它的基类(抽象基)中赋值0(纯虚),稍后将在以受保护的方式共享多重继承基(虚基)的派生类中定义/重写它。
INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.
我想到的第一件事是“noop”,当我第一次看到它时,我的大脑也做了同样的事情!