Last active
August 14, 2016 08:11
-
-
Save kimhogeling/80ab6c36e8da6e7e2d63ef2d9da21cb0 to your computer and use it in GitHub Desktop.
Example JS vs Java - ES5 has no multiple constructors
This file contains hidden or 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
/** | |
* @param {Number} id | |
* @returns {Person} | |
*/ | |
function Person (id) { | |
this.id = id; | |
} | |
/** | |
* @param {Number} id | |
* @param {String} name | |
* @param {Number} age | |
* @returns {Person} | |
*/ | |
function Person (id, name, age) { | |
this.id = id; | |
this.name = name; | |
this.age = age; | |
} | |
new Person(123, "foo", 50); | |
// Person { id: 123, name: 'foo', age: 50 } | |
new Person(456); // <-- Passing one argument | |
// Person { id: 456, name: undefined, age: undefined } <-- Remaining expected arguments fallback to undefined | |
/************************** | |
* The `arguments` object * | |
**************************/ | |
/** | |
* @returns {Person} | |
*/ | |
function Person() { // <-- No arguments expected | |
console.log(arguments); | |
} | |
new Person(1, 2, 3, 4, 5); // <-- Passing five | |
// { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 } <-- All five are available in `arguments` | |
Person {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment