为什么是“你好,世界!” JavaScript代码片段被识别为可接受的程序指令?\[重复\]

JavaScript Node.js

飞云Tom

2020-03-24

Recently a coworker showed this fragment of JavaScript code:

greet = "‮".toString.bind("hello world!")

If you paste this inside the Developer Console and execute it will print a "Hello, World!" message:

>> console.log(greet())
hello, world!

Another interesting thing I found is that if you paste the same greet code inside Node.js REPL it will automatically transpile it to a "readable" format.

How does this work? Why is this behaviour possible in a browser and why does Node.js automatically format it?

第3701篇《为什么是“你好,世界!” JavaScript代码片段被识别为可接受的程序指令?\[重复\]》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
Jim西里 2020.03.24

实际的代码是:

greet = "...".toString.bind("hello world!")

...在字符串字面量是字节E2 80 AE,这是自右至左的优先Unicode字符,这会导致其后的所有反向显示。它用于编写从右到左的语言,例如阿拉伯语或希伯来语。

十六进制编辑器是您的朋友

老丝阿飞 2020.03.24

您有隐藏的字符使文本反转。在这里您可以看到原始字符:https : //www.soscisurvey.de/tools/view-chars.php

在此处输入图片说明

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android