This reading material is important because we start to learn about manipulating the DOM and outputting object name/value pairs onto an HTML using JavaScript.
You can thing of an object like a team roster. Its a list of similar things. example
const roster = {
playerName: 'John Doe',
position: ''
}
With objects, we can call on items within the object using the name which is from the name/value pair vs arrays where you would call on a specific position.
We use dot notation to speifcy the item we want to access within an object. We use bracket notation to access to a property within a property.
this
refer to and what is the advantage to using this
?
this
refers to the current object the code is being written in.Example This referes to the dog Object
DOM stands for Document Object Model - The DOM represents the html page that we can change the document structure, style, and content.
We can use JavaScript to access and manipulate the DOM but the DOM can be built for any language.
const person = {
name: ["Bob", "Smith"],
age: 32,
bio() {
console.log(\`${this.name[0]} ${this.name[1]} is ${this.age} years old.\`);
},
introduceSelf() {
console.log(\`Hi! I'm ${this.name[0]}.\`);
},
};
Object literal - object created with curly brackets
// array
let pokemon =["pikachu"];
pokemon[0] //returns pikachu
//object
let pokemon ={
name: "pikachu"
}
pokemon.name; //still returns pikachu
When to use array vs object:
let pokedex = ["pikachu", "charmander", "bulbasaur"];
let pokemon = {name:"pikachu", type:"electric"};
this is a keyword that changes depending on where in our code we are calling it
refers to the object surrounding the โthisโ keyword
object.property
- Dot notation uses the name of a property attached to the object, prepending by an aobject["property"]
- uses square brackets and reads from a string value; allows us to use variables; helpful when we want to read from a property that has a variableDOM is the virtual representation of the HTML Documentation.
let parentElement = document.getElementById('parentID')
let childElement = document.createElement('tagType');
childElement.textContent = object.property;
parentElement.appendChild(childElement);
//adds style
element.style="inline styles"