JavaScript Objects
objects have methods and properties.
let names = ["lucas", "luc", "James", "Rafael"]// lenght methodconsole.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 encapulationconsole.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) {// propertiesthis.email = emailthis.name = name}// methodslogin() {console.log(this.email + " just logged in")}}// the 'new' keyword// - creates a new empty object {}// - sets the value of 'this' to be the new empty object// - calls the constructor method