Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

JavaScript Objects and Classes

Date Created: 2021/03/15

Last Update: 2022/08/24

#JavaScript #general #notes

JavaScript Objects

objects have methods and properties.

let names = ["lucas", "luc", "James", "Rafael"]
// lenght method
console.log(name.length)
// 4

Primitives

Primitive do not have methods. But, javascript is intuitive. This means that JavaScript wraps primitives in objects which enables methods. Thus, making a given primitive to behave like an object.

  • booleans
  • strings
  • numbers

First Object

Encapsulation

let userOne = {
name: "ben",
}
// ^ this is called encapulation
console.log(userOne.name)
let userOne = {
name: "ben",
login() {
console.log(this.email, "has logged in")
},
logOut() {
console.log(this.email, "has logged out")
},
}
console.log(userOne.login())
// ben@earth has logged in

// We can change property values these values can be dynamic // We can add new methods to objects

// Creating multiple instances of the same type

Classes In ES6 and >

class User {
constructor(email, name) {
// properties
this.email = email
this.name = name
}
// methods
login() {
console.log(this.email + " just logged in")
}
}
var userOne = new User("[email protected]", "Jim")
var userTwo = new User("[email protected]", "Jeff")
// the 'new' keyword
// - creates a new empty object {}
// - sets the value of 'this' to be the new empty object
// - calls the constructor method

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth