我正在尝试用Javascript开发自定义原型(请参阅下面的代码).
如果我用$字符替换₹字符,此代码可以正常工作.但是,我更喜欢使用₹字符而非$字符.如果我使用₹字符,我会收到以下错误消息:
未捕获的SyntaxError:无效或意外的令牌
是否有任何方法或解决方法使下面的代码与₹字符一起使用?
<!DOCTYPE html>
<html lang="en-IN">
<head>
<meta charset="UTF-8" />
<script>
function ?(input){
this.input=input;
var customPrototype={};
customPrototype.upper=function(){
return input.toUpperCase();
}
customPrototype.count=function(){
return input.length;
};
customPrototype.lower=function(){
return input.toLowerCase();
}
customPrototype.__proto__ = ?.prototype;
customPrototype.constructor = ?;
return customPrototype;
}
?.prototype = {
top: function() {
return this.upper();
},
size: function() {
return this.count();
},
bottom: function() {
return this.lower();
}
};
</script>
</head>
<body>
<script>
console.log(?("ProgrAmmeR").top());
console.log(?("ProgrAmmeR").bottom());
console.log(?("ProgrAmmeR").size());
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
JavaScript允许标识符中的各种字符(此处的规范中的详细信息).标识符的第一个字符比后续字符更受限制,但仍然允许很多自由.
根据Unicode标准(规范链接)$
,第一个字符必须是,_
或具有"ID_Start"属性的Unicode字符.字符 ₹没有ID_Start,因此您不能将其用作JavaScript标识符的第一个字符.
后续字符必须是$
,_
或具有"ID_Continue"属性的Unicode字符.FWIW,您根本无法在标识符中使用该字符,因为它也没有ID_Continue.
那么,为什么是$
允许时(美元符号)?
(一卢比符号)以及其它类似的货币符号£
,€
,¥
,和这样有没有关系?纯粹是因为Brendan Eich认为允许$
使用标识符并使其例外是有用的.
Rs
如果你喜欢,你可以使用.不太好,但......
function Rs() { }
Rs.prototype = ...;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61 次 |
最近记录: |