该函数的规范位于15.1.3.4 https://262.ecma-international.org/5.1/#sec-15.1.3.4.
V8 的现代版本(2018)用 C++ 实现。看src/uri.h https://chromium.googlesource.com/v8/v8/+/refs/heads/7.0.99/src/uri.h#34:
// ES6 section 18.2.6.5 encodeURIComponenet (uriComponent)
static MaybeHandle<String> EncodeUriComponent(Isolate* isolate,
Handle<String> component) {
这会调用Encode
定义于uri.cc https://chromium.googlesource.com/v8/v8/+/refs/heads/7.0.99/src/uri.cc#273.
旧版本的 V8 用 JavaScript 实现,并在 BSD 许可证下分发。参见第 359 行src/uri.js https://chromium.googlesource.com/v8/v8/+/3.26.4/src/uri.js#359.
// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
var unescapePredicate = function(cc) {
if (isAlphaNumeric(cc)) return true;
// !
if (cc == 33) return true;
// '()*
if (39 <= cc && cc <= 42) return true;
// -.
if (45 <= cc && cc <= 46) return true;
// _
if (cc == 95) return true;
// ~
if (cc == 126) return true;
return false;
};
var string = ToString(component);
return Encode(string, unescapePredicate);
}
它不叫encodeURIComponent
在那里,但同一文件中的这段代码建立了映射:
InstallFunctions(global, DONT_ENUM, $Array(
"escape", URIEscape,
"unescape", URIUnescape,
"decodeURI", URIDecode,
"decodeURIComponent", URIDecodeComponent,
"encodeURI", URIEncode,
"encodeURIComponent", URIEncodeComponent
));