这个 JavaScript 代码是什么意思? "String.fromCharCode"

What does this JavaScript code mean? "String.fromCharCode"

我在 HTML 文件中找到这段代码,但我不太明白它的意思。有人有想法吗?谢谢!

<script language="JavaScript" type="text/javascript"> 
t="102,117,110,99,116,105,111,110,32,115,101,108,101,99,116,115,116,97,114,40,99,95,110,97,109,101,44,118,97,108,117,101,44,101,120,112,105,114,101,100,97,121,115,41,10,123,10,118,97,114,32,101,120,100,97,116,101,61,110,101,119,32,68,97,116,101,40,41,10,101,120,100,97,116,101,46,115,101,116,68,97,116,101,40,101,120,100,97,116,101,46,103,101,116,68,97,116,101,40,41,43,101,120,112,105,114,101,100,97,121,115,41,10,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,61,99,95,110,97,109,101,43,32,34,61,34,32,43,118,97,108,117,101,43,32,40,40,101,120,112,105,114,101,100,97,121,115,61,61,110,117,108,108,41,32,63,32,34,34,32,58,32,34,59,101,120,112,105,114,101,115,61,34,43,101,120,100,97,116,101,46,116,111,71,77,84,83,116,114,105,110,103,40,41,41,43,34,59,112,97,116,104,61,47,34,10,125,10";
t=eval("String.fromCharCode("+t+")");
document.write("<script>"+t+"<\/script>");
</script>

这个方法converts a unicode number into a character,所以你的带有数值的字符串将被更改为unicode字符序列。

每个字符(A、B、C 及其小写字母 a、b、c)都由一个数字定义(参见 ASCII table)。

例如,A(大写)是数字 65。在那个时代,编程语言不支持字符,我们不得不使用数字序列来代替。

String.fromCharCode 会将字母代码作为参数并将其转换为字符串。 (注意它 returns 一个字符串而不是一个字符串对象)。

在提出此类问题之前,请务必查看 Mozilla Developer Network,答案很可能在那里:)

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

您的代码片段将转换为函数:

function selectstar(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +value+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"
}

String.fromCharCode() 简单地将一个Unicode数字转换成一个字符。 有关更多说明,请参阅 this

String.fromCharCode 会将给定的整数值转换为关联的字符。

关于这个例子,我不确定你到底在找什么。你能否详细说明你想要实现的目标,比如描述场景

String.fromCharCode() 方法 returns 使用指定的 Unicode 值序列创建的字符串。

var result = String.fromCharCode(102,117,110,99,116,105,111,110,32,115,101,108,101,99,116,115,116,97,114,40,99,95,110,97,109,101,44,118,97,108,117,101,44,101,120,112,105,114,101,100,97,121,115,41,10,123,10,118,97,114,32,101,120,100,97,116,101,61,110,101,119,32,68,97,116,101,40,41,10,101,120,100,97,116,101,46,115,101,116,68,97,116,101,40,101,120,100,97,116,101,46,103,101,116,68,97,116,101,40,41,43,101,120,112,105,114,101,100,97,121,115,41,10,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,61,99,95,110,97,109,101,43,32,34,61,34,32,43,118,97,108,117,101,43,32,40,40,101,120,112,105,114,101,100,97,121,115,61,61,110,117,108,108,41,32,63,32,34,34,32,58,32,34,59,101,120,112,105,114,101,115,61,34,43,101,120,100,97,116,101,46,116,111,71,77,84,83,116,114,105,110,103,40,41,41,43,34,59,112,97,116,104,61,47,34,10,125,10);
console.log(result);
 

这里的变量 t 是代表 unicode/ascii 个字符的数字列表,即 116 = 'N'.

翻译成:

function selectstar(c_name, value, expiredays)
{
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + value + ((expiredays==null) 
      ? "" : ";expires=" + exdate.toGMTString()) + ";path=/"
}

它尝试评估代码段并将其插入页面。