我不明白为什么输出是“未定义的”?
JSON.stringify(a.maxKey())即使像上面那样使用,输出仍然是相同的。
Array.prototype.maxKey = function (){
Math.max.apply(Math,
this.map(
function(item){
return item.key}
)
)
}
var a = [{key:1}, {key:2}]
alert(a.maxKey())
发布于 2017-03-30 06:01:47
你错过了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())
https://stackoverflow.com/questions/43104909
复制相似问题