Created
October 10, 2016 08:03
-
-
Save LightSpeedC/b0d30b5e42d6e6e7a52e87b6d43b9b96 to your computer and use it in GitHub Desktop.
argumentsオブジェクトを配列(Array)にする方法 ref: http://qiita.com/LightSpeedC/items/5cf07f94b051d0a8d4f1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.setPrototypeOf(arguments, Array.prototype); | |
// または | |
arguments.__proto__ = Array.prototype; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs() { | |
Object.setPrototypeOf(arguments, Array.prototype); | |
return arguments.sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs() { | |
return Array.from(arguments).sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = arguments.length === 1 ? [arguments[0]] : | |
Array.apply(null, arguments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = [].map.call(arguments, x => x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs() { | |
return [].map.call(arguments, x => x).sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = []; | |
[].push.apply(args, arguments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = [].concat.apply([], arguments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = []; | |
for (var i = 0; i < arguments.length; ++i) | |
args[i] = arguments[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = [].slice.call(arguments); | |
// または | |
var args = Array.prototype.slice.call(arguments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs() { | |
return [].slice.call(arguments).sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(...args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs(...args) { | |
return args.sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortArgs() { | |
return [...arguments].sort().join(','); | |
} | |
console.log(sortArgs(1, 3, 5, 2, 4, 6)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = Array.from(arguments); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment