Javascript - 自定义原型 - 语法错误

3 javascript prototype

我正在尝试用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)

T.J*_*der 5

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)

  • 还有[这个有用的工具](https://mothereff.in/js-variables)验证Unicode变量名称.(确实确认[`₹`](https://mothereff.in/js-variables#%E2%82%B9)不是有效的变量名.) (2认同)