reading-notes

reading notes for code fellows


Project maintained by dLeigh01 Hosted on GitHub Pages — Theme by mattgraham

Problem Domain, Objects, and the DOM

Understanding the Problem Domain is the Hardest Part of Programming

it is often difficult for beginners to learn code because the problem domain (whatever they are attempting to create) isn’t clear to them. Different views on the problem domain can yield different outcomes and make it harder to know what your code is meant to do. Once you understand the problem domain, coding becomes much easier if only because now you have a much clearer idea of what you actually need to accomplish, which means you can give yourself clearer steps to get there.

to get around this you can either attempt to make the problem domain easier, or you can get better at understanding it. Always make sure you know everything you can about what you are trying to do.

What’s the Difference Between Primitve Values and Object References in JavaScript

all data types in JavaScript can be classified as either primitve values or object references. A primitive value sets the variable directly to that values, while an object reference refers to when a variable is set to an object where the value is stored rather than the value itself.

one major difference between the two is that primitive values are immutable (they cannot be changed) and object values are mutable (they can be changed).

if you are using equality operators on objects, this can mess with the expected outcome, and to check equality you need to iterate through their contents instead of checking the references against each other.

Object Literals

objects group together a ser of variables and functions to create a model. Within an object, variables become known as properties and functions become known as methods. In an object, the name of properties and methods is called a key.

literal notation is the most popular way to create an object. ex:

var hotel = {
  name: 'Quay',
  room: 40,
  booked: 25,
  checkAvailability: function() {
    return this.rooms - this.booked;
  }
};

you can then access an object’s proerties using dot notation, ex(hotel.name;) or square brackets, ex(hotel['name']).

Document Object Model

as the browser loads a page, it creates a model called the DOM tree which consists of four types of nodes:

to update the DOM tree, you first have to access the elements.

once you’ve accessed the element you need, then you can work with is.

methods that find elements in the DOM tree are called DOM queries, and should be stored as a variable if you think you’ll need to use it more than once. Dom queries can return a single element or a NodeList (a collection of nodes) from which you can then select a single element using its index number like an array or by using item().

if you use innerHTML, you need to be wary of cross site scripting attacks (XSS - when someone places malicious code on your site) or an attacker could access your user’s accounts. To defend against XSS, you can validate input going to the server (only allow users to input necessary characters) and do not create any DOM fragments using code from untrusted sources.

modern browsers come with tools that allow you to look at the DOM tree

[< table of contents]

[< home]