Skip to content

Instantly share code, notes, and snippets.

@peketamin
Created July 8, 2019 08:11
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 peketamin/0ad458c048771959db78d1e67186c24d to your computer and use it in GitHub Desktop.
Save peketamin/0ad458c048771959db78d1e67186c24d to your computer and use it in GitHub Desktop.
Lightweight ES5 class like object definition
var Person = {
init: function() {
this.first_name = '';
this.last_name = '';
return this;
},
getFullName: function() {
return this.first_name + ' ' + this.last_name;
},
};
p = Person.init()
// {init: ƒ, getFullName: ƒ, first_name: "", last_name: ""}
p.first_name = 'Yuki';
// "Yuki"
p.last_name = 'Yokoyama';
// "Yokoyama"
p.getFullName()
// "Yuki Yokoyama"
@peketamin
Copy link
Author

peketamin commented Jul 8, 2019

Oh...this is not correct way...then I got this error.

p instanceof Person
VM200:1 Uncaught TypeError: Right-hand side of 'instanceof' is not callable
    at <anonymous>:1:3

@peketamin
Copy link
Author

function Product(first_name, last_name) {
  this.first_name = first_name;
  this.first_name = last_name;
}
pr = new Product('Yuuki', 'Yokoyama')
pr instanceof Product
true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment