Objects and prototypes

Introduction

Introduction

iterator stuff: + "yield" in functions + generators + "next"

Object literals

can define object literals

const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

const person = {};
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";

Access

objectName.property
objectName["property"]

can iterate over properties in object for (let variable in object) // code to be executed

Object.getOwnPropertyNames()

Object.values(myObj); gets values

Deleting parts of object

can delete

delete person.age;
delete person["age"];

Nested objects

can nest objects

myObj = {
  name:"John",
  age:30,
  cars: {
    car1:"Ford",
    car2:"BMW",
    car3:"Fiat"
  }
}

Can access nested parts of objects.


myObj.cars.car2;
myObj["cars"]["car2"];

Printing objects

JSON.stringify(myObj); can print

Getters and settings

getters and setters for object. can access and update values in a more flexible way.

const person = {
  firstName: "John",
  lastName: "Doe",
  language: "en",
  get lang() {
    return this.language.toUpperCase();
  }
};
person.lang; will retrun En

const person = firstName: "John", lastName: "Doe", language: "", set lang(lang) this.language = lang.toUpperCase(); ;

person.lang = "en"; will make language be "En"

methods in object literals

const person = {
  firstName: "John",
  lastName: "Doe",
  id: 5566,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

constructors to make objects

function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eye;
}

primatives can be done as objects

new String()    // A new String object
new Number()    // A new Number object
new Boolean()   // A new Boolean object
new Object()    // A new Object object
new Array()     // A new Array object
new RegExp()    // A new RegExp object
new Function()  // A new Function object
new Date()      // A new Date object 

but primatives faster than doing this.

can add things to constructors using prototype

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";

const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

making an object iterable: @@iterator method use of

thing?

is generator stuff?


function* my_function(){

arrays and sets

+ JavaScript this

Inheritance

Inheritance

Multiple inheritance