Skip to content

Instantly share code, notes, and snippets.

@kimhogeling
Last active August 14, 2016 08:11
Show Gist options
  • Save kimhogeling/80ab6c36e8da6e7e2d63ef2d9da21cb0 to your computer and use it in GitHub Desktop.
Save kimhogeling/80ab6c36e8da6e7e2d63ef2d9da21cb0 to your computer and use it in GitHub Desktop.
Example JS vs Java - ES5 has no multiple constructors
/**
* @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