简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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