本章主要参考来源:
======================11.01=============================
- document.write()
<script>document.write(5 + 6);</script>
The document.write() method should only be used for testing. because if you use document.write() after an HTML document is fully loaded, it will delete all existing HTML. - console.log()
active the browser with F12.
======================11.03=============================
- Variables
declare a JavaScript variable with the var keyword
======================11.04=============================
- Type Operator
//Type operators are described in the JS Type Conversion chapter.
- typeof
Returns the type of a variable - instanceof
Returns true if an object is an instance of an object type
- assignment | 赋值?
=、+=、-=、*=、/= - data type
numbers, strings, arrays, objects and more(more?) - function
a block of code designed to perform a particular task.
======================11.05=============================
- object(被定义的对象)
- Scope | 作用域?
** the set of variables, objects, and functions you have access to**
<pre>// code here can not use carName
function myFunction() {var carName = "Volvo"; // code here can use carName}</pre> - events
<button onclick="this.innerHTML=Date()">The time is?</button> - strings
- Numbers
var x = 123;
(100 + 23).valueOf(); // returns 123 from expression 100 + 23
<br />
x = "10 20"
Number(x); // returns NaN
======================11.06=============================
- math
- math.
document.getElementById("demo").innerHTML = Math.PI; // returns 3.141592653589793 - Math.round(x) returns the value of x rounded to its nearest integer:
- Math.pow(x,y) returns the value of x to the power of y:
- Math.sqrt(x) returns the square root of x:
- Math.abs(x) returns the absolute (positive) value of x:
- Math.ceil(x) returns the value of x rounded up to its nearest integer:
- Math.floor(x) returns the value of x rounded down to its nearest integer:
- Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
- Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments:
- random
Math.random(); // returns a random number - date
<pre>new Date() // current date and time
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)</pre>
======================11.07=============================
-
array
var cars = ["Saab", "Volvo", "BMW"];var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString(); //The toString() method returns an array as a comma separated string.
Booleans
- Comparisons
- Conditions
======================11.08=============================
- Switch (like c++)
switch (new Date().getDay()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";} - loop
- for
- while
======================11.09=============================
- RegExp(正则表达式)
- errors
try { Block of code to try}
catch(err) { Block of code to handle errors}
======================11.10=============================
-
Hoisting(变量声明提升)
var x; // Declare x
x = 5; // Assign 5 to xelem = document.getElementById("demo"); // Find an element elem.innerHTML = x; // Display x in the element
Strict Mode
"use strict";
before the script statement
======================11.13=============================Automatic Type Conversions?
document.getElementById("demo").innerHTML = "Hello" - "Dolly";// returns NaN
======================11.15=============================
- JSON——JavaScript Object Notation
The JSON syntax is derived from JavaScript object notation syntax;
JSON format is text only;
Code for reading and generating JSON data can be written in any programming language.