OOP in JavaScript

OOP in JavaScript

Classes and objects

Everything in JS comes from the Object Prototype

Prototypes

JavaScript is based on Prototypes
You can access all the __prototype methods from the object instance

Object literal

The literal objects are instances of the Object prototype

let user1 = {
	name: "Fernando",
	surname: "fernando",
	fullName: `${this.name} ${this.surname}`,
	//sayHi: ()=>  {}
	//sayHi() {}
	sayHi: function () {
		console.log(`${this.name} says HI`)
	}
}

Classes

// Extends
class Student extends User {
let name;
// Call constructor / you can set your own properties here
	constructor(name,props){
		self.name = name
		super(props)
	}
// O
	publishComment(commentContent) {
		const comment = new Comment({
			content: commentContent,
			role: "Student"
		})
				
		this.comments.push(comment)	
	}
}

Relates to

References