Skip to content

Instantly share code, notes, and snippets.

@akaomy
Last active February 28, 2019 06:58
Show Gist options
  • Save akaomy/a1c29a1b92ab13abccf3d32c08c9f074 to your computer and use it in GitHub Desktop.
Save akaomy/a1c29a1b92ab13abccf3d32c08c9f074 to your computer and use it in GitHub Desktop.
cheatsheet of OOP JS BASICS
//To create an object(3):
//1)
var obj = new Object();
//2)
var obj = {};
//3)
obj = {
name: {
first: “Roy”,
second: “Lee”
},
address: “Some street”
}
//To declare a class:
function Book(title, pages) {
this.title = title;
this.pages = pages;
}
//To create and object based on this class (instantiate this class):
var book1 = new Book(’M.L.Dock’, ’33’);
//To access and update attributes of this object:
book.title
book.title = “new title”;
//To declare and use function of the created class Book:
Book.prototype.printTitile = function() {
console.log(this.title);
}
book.printTitle
//==> new title
//To declare a function inside a class Book:
function Book(title, pages) {
this.title = title;
this.pages = pages;
this.printTitle = function() {
console.log(this.title)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment