How to use five digit long Unicode characters in JavaScript

前端 未结 5 2079

In JavaScript I can do this:

foo = \"\\u2669\" // 1/4 note

But I can\'t do this

foo = \"\\u1D15D\" // full note  -five hex digi         


        
5条回答
  •  时光说笑
    2021-02-19 10:31

    In the MDN documentation for fromCharCode, they note that javascript will only naturally handle characters up to 0xFFFF. However, they also have an implementation of a fixed method for fromCharCode that may do what you want (reproduced below):

    function fixedFromCharCode (codePt) {
        if (codePt > 0xFFFF) {
            codePt -= 0x10000;
            return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF));
        }
        else {
            return String.fromCharCode(codePt);
        }
    }
    
    foo = fixedFromCharCode(0x1D15D);
    

提交回复
热议问题