Skip to content

Instantly share code, notes, and snippets.

@LightSpeedC
Created October 10, 2016 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LightSpeedC/b0d30b5e42d6e6e7a52e87b6d43b9b96 to your computer and use it in GitHub Desktop.
Save LightSpeedC/b0d30b5e42d6e6e7a52e87b6d43b9b96 to your computer and use it in GitHub Desktop.
argumentsオブジェクトを配列(Array)にする方法 ref: http://qiita.com/LightSpeedC/items/5cf07f94b051d0a8d4f1
Object.setPrototypeOf(arguments, Array.prototype);
// または
arguments.__proto__ = Array.prototype;
function sortArgs() {
Object.setPrototypeOf(arguments, Array.prototype);
return arguments.sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
function sortArgs() {
return Array.from(arguments).sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
var args = arguments.length === 1 ? [arguments[0]] :
Array.apply(null, arguments);
function sortArgs() {
var args = arguments.length === 1 ?
[arguments[0]] : Array.apply(null, arguments);
return args.sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
var args = [].map.call(arguments, x => x);
function sortArgs() {
return [].map.call(arguments, x => x).sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
var args = [];
[].push.apply(args, arguments);
var args = [].concat.apply([], arguments);
var args = [];
for (var i = 0; i < arguments.length; ++i)
args[i] = arguments[i];
function sortArgs() {
var args = new Array(arguments.length);
for (var i = 0; i < arguments.length; ++i)
args[i] = arguments[i];
return args.sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
var args = [].slice.call(arguments);
// または
var args = Array.prototype.slice.call(arguments);
function sortArgs() {
return [].slice.call(arguments).sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
function sortArgs(...args) {
return args.sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
function sortArgs() {
return [...arguments].sort().join(',');
}
console.log(sortArgs(1, 3, 5, 2, 4, 6));
var args = Array.from(arguments);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment