Allow JS constructor function to return non-void

This commit is contained in:
Ron Buckton
2017-06-01 12:11:09 -07:00
parent 928da675ac
commit 3acc76cc8f
4 changed files with 160 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @filename: index.js
// @lib: es5, dom
var Person = function (firstNameOrPojo, lastName) {
if (typeof firstNameOrPojo === "string") {
this.firstName = firstNameOrPojo;
this.lastName = lastName;
} else {
return new Person(firstNameOrPojo.firstName, firstNameOrPojo.lastName);
}
};
Person.prototype.greet = function greet() {
return `Hello, I am ${this.firstName} ${this.lastName}.`;
};
var fred = new Person({ firstName: "Fred", lastName: "Flintstone" });
console.log(fred.greet());