简体   繁体   English

Javascript中具有“未定义”输出的Array.prototype方法

[英]Array.prototype method with 'undefined' Output in Javascript

I don't understand why is the output is 'undefined'? 我不明白为什么输出是“未定义的”?

    JSON.stringify(a.maxKey())

The output is still the same even if it is used like in the above. 即使像上面那样使用,输出仍然是相同的。

  Array.prototype.maxKey = function (){ Math.max.apply(Math, this.map( function(item){ return item.key} ) ) } var a = [{key:1}, {key:2}] alert(a.maxKey()) 

You miss return keyword. 您错过了return关键字。

 Array.prototype.maxKey = function() { return Math.max.apply(Math, this.map(item => item.key)); } var a = [{ key: 1}, { key: 2}]; console.log(a.maxKey()) 

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

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