什么是js
- 编程语言,在网页上实现功能、交互
- 解释性语言
- 运行在客户端
- 面向对象编程
添加js
内部的js (推荐)
1 2 3
| <script> // Your JavaScript <script>
|
外部的js (推荐)
1
| <script src="script.js"></script>
|
内联js (不推荐)
1 2 3 4
| <button onclick="createParagraph()"> Click me </button>
|
数据类型
数字
只用浮点型 1 2 3 4 5 6 7
| console.log(2);
console.log(2.5);
console.log(2 + 2);
console.log(0.2 + 0.3);
|
字符串
1 2 3 4 5 6 7 8 9 10 11
| console.log("hello");
console.log("hello" + " world");
console.log("hello".length);
console.log("hello".charAt(0));
console.log("hello, world!".replace("hello", "goodbye"));
console.log("hello, world!".toUpperCase());
|
布尔
1 2 3 4 5 6 7 8 9 10 11
| console.log(1==1);
console.log(1>2);
console.log(1>=2);
console.log(true && false);
console.log(true || false);
console.log(!true);
|
Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| var obj = new Object(): var obj1 = {};
obj = { name: "MinuteSheep", age: "20", email: "minutesheep@163.com", contact: { phone: "1234567", telegram: "@minutesheep" } } console.log(obj);
console.log(obj.name); console.log(obj.contact.phone);
console.log(obj["name"]); console.log(obj["contact"]["phone"]);
obj.age = 35; console.log(obj);
obj.contact.wechat = "12345678"; console.log(obj);
|
Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| var a = new Array(); var a1 = [];
a[0] = "dog"; a[1] = "cat"; console.log(a); console.log(a.length);
a[5] = "tiger"; console.log(a.length); console.log(a[3]);
a.push("sheep"); console.log(a); a.unshift("lion"); console.log(a);
a.pop(); console.log(a); a.shift(); console.log(a);
a.reverse(); console.log(a);
a1 = ["dog", "cat", "tiger"]; console.log(b);
|
变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| var name="MinuteSheep"; console.log(name);
let a = 1; console.log(a);
name = "MinuteWolf"; console.log(name);
a = 1+1; console.log(a);
const PI = 3.14; console.log(PI);
|
运算符
1 2 3 4 5 6 7 8 9
| var x; x = "3" + 4 + 5; console.log(x);
x = 3 + 4 + "5"; console.log(x);
console.log(123 == "123"); console.log(123 === "123");
|
结构语句
选择语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| var name = "minutesheep"; var age = 20;
if (name == "minutesheep") { console.log("true"); } else { console.log("false"); }
if (name == "minutesheep" && age == 20) { console.log("true"); } else { console.log("false"); }
if (name == "minutesheep" && age == 20) { console.log("true1"); } else if(name == "john" || age == 35) { console.log("true2"); } else { console.log("stranger"); }
var allowed = (age >= 18) ? "Yes" : "No"; console.log(allowed);
switch (name) { case "minutesheep": console.log("minutesheep"); break; case "john": console.log("john"); break; default: console.log("stranger"); }
|
循环语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var name = "minutesheep"; var age = 20; var animals = ["cat", "dog", "tiger"];
while (true) { console.log("true"); }
do { age++; console.log(age); } while (name == "minutesheep");
for (let i=0; i<5; i++) { age++; console.log(age); }
for (let i in animals) { console.log(animals[i]); }
|
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| let a = 1;
function add(x) { a += x; }
console.log(a); add(20); console.log(a);
function sum() { let sum = 0;
for (let i = 0, j = arguments.length; i<j; i++) { sum += arguments[i]; }
return sum; }
let sum = sum(1,2,3,4,5,6,7); console.log(sum);
|
闭包
1 2 3 4 5 6 7 8 9
| function makeAdder(a) { return function(b) { return a + b; }; }
var x = makeAdder(5); var sum = x(8); console.log(sum);
|