Class and Objects

Classes in JavaScript before ES 6

function myClass(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
myClass.prototype.addFriends = function(newFriend){
this.friends.push(newFriend)
}
let myObject = new myClass('John', 18);console.log(myObject); // myClass { name: 'John', age: 18, friends: [] }myObject.addFriends("Helen") ;
myObject.addFriends("Harry") ;
myObject.addFriends("Qazi") ;
console.log(myObject);
/* myClass {
name: 'John',
age: 18,
friends: [ 'Helen', 'Harry', 'Qazi' ]
} */

what is a prototype?

Constructor

Classes in JavaScript after ES 6

class myClass {
constructor(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
addFriends(newFriend) {
this.friends.push(newFriend);
}
}
let myObject = new myClass('John', 19);console.log(myObject); // myClass { name: 'John', age: 19, friends: [] }myObject.addFriends('Hellen');
myObject.addFriends('Harry');
myObject.addFriends('Peter');
console.log(myObject);
/*
myClass {
name: 'John',
age: 19,
friends: [ 'Hellen', 'Harry', 'Peter' ]
}
*/
console.log(typeof myClass); 
//function

extends

class classRoom {
constructor(name,age){
this.name = name ;
this.age = age ;
}
}
class Student extends classRoom {
constructor(name,age,major){
super(name,age) ;
this.major = major ;
}
}
let newStudent = new Student("John",18,"Computer Sciencs")console.log(newStudent)
// Student { name: 'John', age: 18, major: 'Computer Sciencs' }

Static Methods

class classRoom {
constructor(name, age) {
this.name = name;
this.age = age;
classRoom.totalStudents++;
}
static getTotalStudent() {
console.log(`${classRoom.totalStudents} students`);
}
}
classRoom.totalStudents = 0;class Student extends classRoom {
constructor(name, age, major) {
super(name, age);
this.major = major;
}
}
let John = new Student('John', 18, 'Computer Sciencs');
let Harry = new Student('Harry', 18, 'Computer Sciencs');
let Peter = new Student('Peter', 18, 'Computer Sciencs');
// console.log(newStudent);
classRoom.getTotalStudent(); // 3 students

Classes in JavaScript

Class and Objects

Classes in JavaScript before ES 6

function myClass(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
myClass.prototype.addFriends = function(newFriend){
this.friends.push(newFriend)
}
let myObject = new myClass('John', 18);console.log(myObject); // myClass { name: 'John', age: 18, friends: [] }myObject.addFriends("Helen") ;
myObject.addFriends("Harry") ;
myObject.addFriends("Qazi") ;
console.log(myObject);
/* myClass {
name: 'John',
age: 18,
friends: [ 'Helen', 'Harry', 'Qazi' ]
} */

what is prototype ?

Constructor

Classes in JavaScript after ES 6

class myClass {
constructor(name, age) {
this.name = name;
this.age = age;
this.friends = [];
}
addFriends(newFriend) {
this.friends.push(newFriend);
}
}
let myObject = new myClass('John', 19);console.log(myObject); // myClass { name: 'John', age: 19, friends: [] }myObject.addFriends('Hellen');
myObject.addFriends('Harry');
myObject.addFriends('Peter');
console.log(myObject);
/*
myClass {
name: 'John',
age: 19,
friends: [ 'Hellen', 'Harry', 'Peter' ]
}
*/
console.log(typeof myClass); 
//function

extends

class classRoom {
constructor(name,age){
this.name = name ;
this.age = age ;
}
}
class Student extends classRoom {
constructor(name,age,major){
super(name,age) ;
this.major = major ;
}
}
let newStudent = new Student("John",18,"Computer Sciencs")console.log(newStudent)
// Student { name: 'John', age: 18, major: 'Computer Sciencs' }

Static Methods

class classRoom {
constructor(name, age) {
this.name = name;
this.age = age;
classRoom.totalStudents++;
}
static getTotalStudent() {
console.log(`${classRoom.totalStudents} students`);
}
}
classRoom.totalStudents = 0;class Student extends classRoom {
constructor(name, age, major) {
super(name, age);
this.major = major;
}
}
let John = new Student('John', 18, 'Computer Sciencs');
let Harry = new Student('Harry', 18, 'Computer Sciencs');
let Peter = new Student('Peter', 18, 'Computer Sciencs');
// console.log(newStudent);
classRoom.getTotalStudent(); // 3 students