Skip to content

Latest commit

 

History

History
92 lines (70 loc) · 3.94 KB

201-class06.md

File metadata and controls

92 lines (70 loc) · 3.94 KB

Class 201.06 Monday 17th July

Notes

  1. if I want to remove an object property:
person = {};
person.test = 'This is just a test';
delete person.test; // now it's gone!
  1. assuming this line of HTML code:
<h1 title="title">My Pokedex</h1>
  • then this:
let titleElement = document.getElementById('title');
console.log(titleElement.textContent);
    • t/his displays

Q&A's

Readings

This section covers the DOM which is a way to read/manipulate data to/from and HTML page.

Learn JS

  1. How would you describe an object to a non-technical friend you grew up with?

it's a thing with properties and methods attached to them.

it's like a standard lego kit that comes with similar items (people, blocks, etc), but each object (like a lego box), can have additional components that we can play with.

  1. What are some advantages to creating object literals?

Object Literatls are created with curly brackets (let object_name = {};) It is very common to create an object using an object literal when you want to transfer a series of structured, related data items in some manner, for example sending a request to the server to be put into a database. Sending a single object is much more efficient than sending several items individually, and it is easier to work with than an array, when you want to identify individual items by name.

  1. How do objects differ from arrays?

Objects are used to store data in key-value pairs, where each value is associated with a unique key, while arrays are used to store lists of values in a specific order, and each value in an array is accessed using an index.

  1. Give an example for when you would need to use bracket notation to access an object’s property instead of dot notation.

You would need to use bracket notation to access an object's property instead of dot notation in the following situations:

  1. Dynamic property access: If the property name is stored in a variable or is dynamically determined at runtime, bracket notation allows you to access the property using the variable value. Example:
var propertyName = "name";
var obj = { name: "John" };

console.log(obj[propertyName]); // Accessing property dynamically
  1. Property names with special characters or spaces: If the property name contains special characters, spaces, or starts with a number, bracket notation is necessary. For instance:
var obj = { "property name": "value" };

console.log(obj["property name"]); // Accessing property with special characters
  1. Reserved keywords as property names: If the property name is a reserved keyword in JavaScript, such as class or if, you must use bracket notation to access it. For example:
var obj = { class: "sample" };

console.log(obj["class"]); // Accessing property with reserved keyword as name
  1. Evaluate the code below. What does the term this refer to and what is the advantage to using this?
const dog = {
  name: 'Spot',
  age: 2,
  color: 'white with black spots',
  humanAge: function (){
    console.log(`${this.name} is ${this.age*7} in human years`);
  }
}

this is a self-referential object. It allows us to write methods where the object can refer to itself; we can use this instead of tracking the name of the object (which will frequently change)

DOM

  1. What is the DOM?

Document Object Model. It's basically the entire HTML page loaded into one object that we can manipulate.

  1. Briefly describe the relationship between the DOM and JavaScript.

DOM is the page, and JS allows us to read and write data to/from the DOM. This means reading what's on the page, and even dynamically updating its contents.

References

  • Google Bard and ChatGPT