在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在JavaScript中,你可以使用双位负(~~n)来代替Math.floor(n)(如果n是正数)或parseInt(n, 10)(即使n是负数)。N | N和N & N总是得到和~~ N相同的结果。
var n = Math.PI;
n; // 3.141592653589793
Math.floor(n); // 3
parseInt(n, 10); // 3
~~n; // 3
n|n; // 3
n&n; // 3
// ~~n works as a replacement for parseInt() with negative numbers…
~~(-n); // -3
(-n)|(-n); // -3
(-n)&(-n); // -3
parseInt(-n, 10); // -3
// …although it doesn’t replace Math.floor() for negative numbers
Math.floor(-n); // -4
单个位的求反(~)计算-(parseInt(n, 10) + 1),因此两个位的求反将返回-(-(parseInt(n, 10) + 1) + 1)。
更新:这里有一个jsPerf测试用例,比较了这些替代方案的性能。
其他回答
在Java中,如果x的值为NaN,则x == x返回false, x != x返回true。
NSIS (Nullsoft Scriptable安装系统)有StrCmp指令:
StrCmp str1 str2 jump_if_equal [jump_if_not_equal] 比较str1和str2(不区分大小写)。如果str1和str2相等,则Gotos jump_if_equal,否则Gotos jump_if_not_equal。 StrCmp $0“一个字符串”0 +3 DetailPrint '$$0 == "a string"' 转到+ 2 DetailPrint '$$0 != "a string"'
锦上添花:jump_if_equal和jump_if_not_equal也可以是负数。但我猜你们已经从正数前面的+符号中算出来了。我不记得这是强制性的,还是只是一个可怕的惯例。
这基本上结合了最差的BASIC和最差的Assembler。
在Java中(实际上,我最近在不同的SO帖子上写过这个):
int x = 1 + + + + + + + + + + + + + 1;
System.out.println(x);
Perl
my %h1 = map { $_ => 1 } qw/foo bar baz/; // construct an 'is_member' type lookup table
my %h2 = map { "$_" => 1 } qw/foo bar baz/;
第二行是一个语法错误,即使对一个有经验的perl程序员来说,它看起来是一样的。perl的缺点是总是试图做你想做的,而不是你说的。
我发现Javascript Date Object对110年的热爱令人愉快。 试一试。
<Script language ="JavaScript">
<!--
var now = new Date()
var dia = now.getDay()
var mes = now.getMonth()
var fecha
//Day of the week
if(dia==0){
fecha="Domingo, ";
}else if(dia==1){
fecha="Lunes, ";
}else if(dia==2){
fecha="Martes, ";
}else if(dia==3){
fecha="Miércoles, ";
}else if(dia==4){
fecha="Jueves, ";
}else if(dia==5){
fecha="Viernes, ";
}else{
fecha="Sábado, ";
}
fecha = fecha + now.getDate() + " de "
//Which month is it?
if(mes==0){
fecha=fecha + "Enero"
}else if(mes==1){
fecha=fecha + "Febrero"
}else if(mes==2){
fecha=fecha + "Marzo"
}else if(mes==3){
fecha=fecha + "Abril"
}else if(mes==4){
fecha=fecha + "Mayo"
}else if(mes==5){
fecha=fecha + "Junio"
}else if(mes==6){
fecha=fecha + "Julio"
}else if(mes==7){
fecha=fecha + "Agosto"
}else if(mes==8){
fecha=fecha + "Septiembre"
}else if(mes==9){
fecha=fecha + "Octubre"
}else if(mes==10){
fecha=fecha + "Noviembre"
}else{
fecha=fecha + "Diciembre"
}
//Year
fecha = fecha + " del " + now.getYear()
document.write(fecha);
//-->
</Script>
脚本是西班牙语-抱歉如果你不明白代码..这个想法是为了让你看到110年的结果。