function getIndexToIns(arr, num) {
var args = Array.prototype.join.call(arguments);
function compareNumbers(a, b) {
return a - b;
}
args.sort(compareNumbers);
return args;
}
getIndexToIns([40, 60], 50);
最佳答案
错误的抛出仅仅是因为Array.prototype.join
返回了一个字符串。为了将类数组对象转换为数组,您需要改用Array.prototype.slice.call
。
替换
var args = Array.prototype.join.call(arguments);
有了这个
var args = Array.prototype.slice.call(arguments);
var args = Array.apply(null, arguments);
或使用 Array.from
var args = Array.from(arguments);
最干净的解决方案是 ES6 rest parameters
function getIndexToIns(...args) {
function compareNumbers(a, b) {
return a - b;
}
args.sort(compareNumbers);
return args;
}
getIndexToIns([40, 60], 50);
关于javascript - 为什么我不断收到 TypeError : args. sort is not a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396651/