闭包
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// let a = 89;
// let b = 86;
// let c = 90;
// function sum(b, a) {
// //内部变量
// return b + a + c;
// }
// console.log(sum(29, 344));
function demo1() {
let email = "80883341@qq.com";
return function child() {
return email;
};
}
alert(demo1()());
// function demo2() {
// let a = "2121";
// }
// alert(demo2);
</script>
</body>
</html>
遍历数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
let colors = ["red", "blue", "green"];
for (i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
</script>
</body>
</html>
静态方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// 无需实例化(new)直接使用类名.方法调用
class Users {
constructor(name, email) {
this.name = name;
this.email = email;
}
show() {
return {
names: this.name,
emails: this.email,
age: this.#age,
};
}
static fetch() {
return this.UserName;
return this;
return "static+++++function";
}
//私有成员
#age = 50;
// 静态变量
static UserName = "灭绝诗美";
// 访问器属性:伪装成属性的方法 有get set
get age() {
return this.#age;
}
}
const Account = new Users();
console.log(Account.show());
console.log(Users.#age);
console.log(Users.age);
// console.log(Users.fetch());
// console.log(Users.UserName);
</script>
</body>
</html>
类与继承
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// function User(name, pass) {
// this.name = name;
// this.pass = pass;
// this.show = function () {
// return { name: this.name, email: this.email };
// };
// }
// const user = new User("kowal", "kowalpass");
// console.log(user);
function User(name, pass) {
this.name = name;
this.pass = pass;
// this.show = function () {
// return {
// name: this.name,
// email: this.email,
// };
// };
}
User.prototype.show = function () {
return { name: this.name, pass: this.pass };
};
const user = new User("kowal", "mima");
console.log(user);
console.log(user.show());
const user1 = new User("pter", "jmvnnnnn");
console.log(user1);
console.log(user1.show());
</script>
</body>
</html>
原型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// function f1() { // return "eeee"; // } alert(f1());
// 任何一个函数都是对象,都有一个共通属性【prototype】
// 何为原型:原型=父盒,提供原始参数,子元素可以继承父元素的原型属性
//函数作为基本函数使用时 prototype属性基本无用,但依然作为函数的初始化属性
// 当一个函数作为构造函数使用时,必须使用“new”关键字进行区别
// 通过构造函数创建对象的过程为 对象实例化
// 对象实例化后的对象可以看作为一个类
//建立函数
function User() {
name = "kowal";
ID = 80887765;
pass = '90872udhn"';
}
// 当成普通函数进行调用;
const user = User();
// console.log(user);
console.dir(user);
// *******??当成构造函数调用 此时需用new关键字********
const xcv = new User();
console.dir(xcv);
</script>
</body>
</html>
对象
<!-- <!DOCTYPE html> -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
class User1 {
constructor(name, email) {
this.name = name;
this.email = email;
}
show() {
return {
name_show: this.name,
email_show: this.email,
};
}
get age() {
return this.age;
}
#age = 50;
}
const Account1 = new User1("天彭老师", "80883341@qq.com");
const Account2 = new User1("kowal", "5677765@qq.com");
// console.log(Account);
// console.log(typeof Account);
// console.log(typeof User1);
// console.log(Account.show());
console.log(Account2.age());
// console.log(Account2.show());
// console.log(Account1.show());
</script>
</body>
</html>
函数作用域
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// 函数作用域
let site = "PHPCN"; //全局site变量
function getSite() {
let site = "JDSC"; //函数内site变量
return site;
// 函数作用域查询的过程中,就拿site变量为例,查询中先以自身作用域内做检查,如果用site变量则返回该值,如果检查不到该变量则继续向外查询
// 如果最后查不到该site变量那么提示undefind 未定义 这个过程叫做作用域链
let domain = "baidu.com"; //私有变量,domain变量为内部变量,仅限于作用域内访问外部不可调用
}
// alert(getSite());
alert(getSite());
</script>
</body>
</html>
while循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// const colors = ["red", "grenn", "blue"];
// let j = 0;
// while (j < colors.length) {
// j++;
// console.log(colors[j]);
// }
// 对象遍历 For-IN
// const OBJEC = {
// name: "kowal",
// phone: "13082899878",
// shooe: "Nike",
// ID: 988898,
// };
// for (let xh in OBJEC) {
// console.log(OBJEC[xh]);
// }
for (let i = 0; i <= 100; ) {
console.log(i);
i++;
}
</script>
</body>
</html>