1015 Head First HTML5 Programming — Study Log #3
Since I didn’t completely understand it last time, today I plan to try Chapter 2 once again.
Chapter 2. Introducing JavaScript and the DOM: A Little Code (2)
- Boolean value(布尔值)是编程和逻辑中最基本的数据类型之一,只有两个可能的值:
- true
- false
- Variables hold things. With JavaScript they can hold lots of different things.
-
Variables are containers for holding values. JavaScript variables don’t have strict types, so any variable can hold a number, a string or a boolean.
var winners = 2; //integer numeric values 整数 var boilingPt = 212.0; //floating point numeric values 浮动小数 var name = "Dr. Evil"; //strings of characters 字符串 var isEligible = false; //boolean value: true or false - Three steps of creating a variable:
- The first step is to declare your variable
- Next we need a value to put in the variable
- Finally, we have a variable and we have a value, and all we need to do is assign the value to the variable
Syntax
- Each statement ends in a semicolon.
x = x + 1;- A single line comment begins with two forward slashes.
// I'm a comment- White space doesn’t matter.
- Surround strings of characters with double quotes.
"You rule!"- Variables are declared using var and a name.
var width;- Don’t use quotes around the boolean values true and false.
rockin = true;- Variables don’t have to be given a value when they are declared:
var width;
❓How to name your variables
Rule#1: Start your variables with a letter, an underscore or a dollar sign
Numbers, strings and booleans are all known as primitive types in JavaScript.
Rule #2: Then you can use any number of letters, numeric digits, underscores or dollar signs
Rule #3: Make sure you avoid all of JavaScript’s reserved words
- Webville Guide to Better Naming
- Choose names that mean something.
- Use “camel case” when creating multiword variable names. Just use camel case, in which you capitalize the first letter of each word (other than the first): twoHeadedDragonWithFire.
- Use variables that begin with _ and $ only with very good reason.
- JavaScript and HTML are two different things. HTML is markup and JavaScript is code.
-
DOM (Document Object Model)
- The DOM can do
- Get elements from the DOM.
document.getElementById - Create or add elements to the DOM.
- Remove elements from the DOM.
- Get and set the attributes of elements.
- Get elements from the DOM.
- the Array
- Math is a built-in JavaScript library that has a bunch of math-related functions in it. Math.random generates a random number between 0 and 1.
-
An array is a special kind of object that’s built into JavaScript. It’s special because you can use numerical indexes to access the values stored in the array, something you can’t do with other (non-array) objects, or objects that you create yourself.
- When the browser loads a web page, it creates a Document Object Model (DOM), which is an internal representation of the web page.
- You make your web pages interactive by examining and changing the DOM using JavaScript.
- Get access to an element in your web page using
document.getElementById. document.getElementByIduses the id of an element to find the element in the DOM.- Use the
innerHTMLproperty of an element to change the element’s content. - Use an array to store more than one value.
- Math is a JavaScript library with several math-related functions in it.
- Math.random returns a floating point number between 0 and 1 (but never 1 precisely).
- Math.floor converts a floating point number to an integer by dropping all the digits after the decimal point.