简体   繁体   English

为什么Javascript map函数String.toLowerCase在nodejs 6中不起作用?

[英]Why doesn't Javascript map function String.toLowerCase doesn't work in nodejs 6?

I'm going through the book Learning Javascript by Ethan Brown. 我正在阅读Ethan Brown撰写的“学习Javascript”一书。 There is an example on how to use the map function to convert all the elements in a array to lowercase like this: 有一个关于如何使用map函数将数组中的所有元素转换为小写的示例,如下所示:

const cart = [ { name: "Widget", price: 9.95 }, { name: "Gadget", price: 22.95 }];
const names = cart.map(x => x.name);
const lcNames = names.map(String.toLowerCase);

If I run this in the Firefox (v51) browser console, it works, however if I try to run this in nodejs v6.9.4 I get: 如果我在Firefox(v51)浏览器控制台中运行它,它可以工作,但是如果我尝试在nodejs v6.9.4中运行它,我得到:

TypeError: undefined is not a function
    at Array.map (native)
    at repl:1:27
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:545:10)
    at emitOne (events.js:101:20)

If I change the lcNames assignment to the following for nodejs then it runs fine: 如果我将lcNames赋值更改为nodejs的以下内容,那么它运行正常:

const lcNames = names.map(x => x.toLowerCase());

I checked the ES6 Javascript compatibility chart for node6 vs Firefox 50 and they both seem to support the same features. 我检查了node6与Firefox 50的ES6 Javascript兼容性图表,它们似乎都支持相同的功能。 So why doesn't the code from the book work in nodejs? 那么为什么本书中的代码不能在nodejs中运行呢?

String.toLowerCase does not exist. String.toLowerCase不存在。 However, String.prototype.toLowerCase does. 但是, String.prototype.toLowerCase可以。 But keep in mind that this would need to be set for that call to succeed, whereas map would pass it as a parameter . 但请记住, this将需要该呼叫成功进行设置,而map通过它作为一个参数 Hence, this is the most straightforward thing to do: 因此,这是最直接的事情:

const lcNames = names.map(name => name.toLowerCase());

The function is defined on the prototype ( String.prototype.toLowerCase ), meaning that instances of the string type have access to the toLowerCase function. 该函数在原型( String.prototype.toLowerCase )上定义,这意味着字符串类型的实例可以访问toLowerCase函数。

That's why you can access that function via const lcNames = names.map(x => x.toLowerCase()); 这就是你可以通过const lcNames = names.map(x => x.toLowerCase());访问该函数的原因const lcNames = names.map(x => x.toLowerCase()); .

This on the other hand works, because isFinite is not defined through the prototype, but on Number itself. 另一方面,这是有效的,因为isFinite不是通过原型定义的,而是基于Number本身。

const prices = cart.map(x => x.price);
prices.map(Number.isFinite);

Note that: 注意:

names.map(String.toLowerCase);

works in Firefox because it has String generic methods that include toLowerCase and are documented with the note: 在Firefox中有效,因为它具有包含toLowerCase的 String泛型方法 ,并记录了注释:

String generics are non-standard, deprecated and will get removed near future. 字符串泛型是非标准的,已弃用,将来会被删除。 Note that you can not rely on them cross-browser without using the shim that is provided below. 请注意,如果不使用下面提供的垫片,则不能跨浏览器依赖它们。

Most important is that they aren't part of ECMAScript, so likely not supported in other browsers. 最重要的是它们不是ECMAScript的一部分,因此很可能在其他浏览器中不受支持。 If you want to use it, you can conditionally add a pollyfill, though there is a shim for adding all the generic methods at MDN . 如果你想使用它,你可以有条件地添加一个pollyfill,虽然有一个垫片可以在MDN上添加所有通用方法。

 // Built-in support? console.log('Has built-in String.toLowerCase? ' + (typeof String.toLowerCase == 'function')); // Polyfill if not supported if (!String.toLowerCase) { String.toLowerCase = function(s) { return String(s).toLowerCase(); } } // Test it console.log(['A','B','C'].map(String.toLowerCase)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM