相关文章推荐
高大的火龙果  ·  arkts ...·  4 小时前    · 
逆袭的日光灯  ·  ThinkPHP6 模型scope ...·  7 月前    · 
有情有义的沙滩裤  ·  Can not create a ...·  1 年前    · 
时尚的砖头  ·  livecharts - OSCHINA ...·  1 年前    · 

废弃 escape / unescape

* Computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. * @deprecated A legacy feature for browser compatibility * @param string A string value declare function escape ( string : string ): string ; * Computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. * @deprecated A legacy feature for browser compatibility * @param string A string value declare function unescape ( string : string ): string ;

这两个接口的功能,可以参考MDN的资料 escape() unescape()

escape/unescape 对宽字符进行重新编码,并做了特殊的标记。例如:

escape('中文')
// %u4E2D%u6587

粗略还原一下这个过程

'中文'.split('').map(s => '%u' + s.charCodeAt(0).toString(16)).join('')
// %u4e2d%u6587

当然,和encodeURIComponent一样,对于ASCII的字符集,并不会做重新编码。

废弃RegExp状态和compile方法

interface RegExp {
    // Non-standard extensions
    /** @deprecated A legacy feature for browser compatibility */
    compile(pattern: string, flags?: string): this;
interface RegExpConstructor {
    new(pattern: RegExp | string): RegExp;
    new(pattern: string, flags?: string): RegExp;
    (pattern: RegExp | string): RegExp;
    (pattern: string, flags?: string): RegExp;
    readonly prototype: RegExp;
    // Non-standard extensions
    /** @deprecated A legacy feature for browser compatibility */
    $1: string;
    /** @deprecated A legacy feature for browser compatibility */
    $2: string;
    /** @deprecated A legacy feature for browser compatibility */
    $3: string;
    /** @deprecated A legacy feature for browser compatibility */
    $4: string;
    /** @deprecated A legacy feature for browser compatibility */
    $5: string;
    /** @deprecated A legacy feature for browser compatibility */
    $6: string;
    /** @deprecated A legacy feature for browser compatibility */
    $7: string;
    /** @deprecated A legacy feature for browser compatibility */
    $8: string;
    /** @deprecated A legacy feature for browser compatibility */
    $9: string;
    /** @deprecated A legacy feature for browser compatibility */
    input: string;
    /** @deprecated A legacy feature for browser compatibility */
    $_: string;
    /** @deprecated A legacy feature for browser compatibility */
    lastMatch: string;
    /** @deprecated A legacy feature for browser compatibility */
    "$&": string;
    /** @deprecated A legacy feature for browser compatibility */
    lastParen: string;
    /** @deprecated A legacy feature for browser compatibility */
    "$+": string;
    /** @deprecated A legacy feature for browser compatibility */
    leftContext: string;
    /** @deprecated A legacy feature for browser compatibility */
    "$`": string;
    /** @deprecated A legacy feature for browser compatibility */
    rightContext: string;
    /** @deprecated A legacy feature for browser compatibility */
    "$'": string;

RegExpConstructor中被废弃的各种属性中,最常用的是分组编组属性。例如:

if(/<(\w+)/.test('<div>')) {
    console.log(RegExp.$1)
// div

这种更关注规则是否匹配,对匹配结果不敏感的场景,用分组属性来获得匹配结果,使用上还是有一定便利的。

但这里有个特别容易踩到的坑,就是每次匹配执行后,分组属性都会有变化,必须非常小心地操作这个属性结果,尤其是有await/async的时候。

总的来说,这是一种stateness的情况,模式上不推荐,且容易进坑,虽然我个人觉得有点可惜,但通过废弃不好的模式,来避免程序员踩坑的做法,还是应当支持的。

另外的compile方法,原来的环境是编译正则后会有性能提升,但现在这个需求应该不多了。

废弃Stringsubstr方法

* Gets a substring beginning at the specified location and having the specified length. * @deprecated A legacy feature for browser compatibility * @param from The starting position of the desired substring. The index of the first character in the string is zero. * @param length The number of characters to include in the returned substring. substr(from: number, length?: number): string;

很早之前就有推荐使用substring来替代substr,但我个人的思考习惯,更喜欢用substr。行吧。

前端技术 @ 苏宁易购 69.8k
粉丝