批改状态:合格
老师批语:
变量可以起作用的范围和区域
JavaScript分全局作用域、函数作用域、块作用域。
全局作用域
默认,在任何地方都可以访问
let name = '张三';console.log(name);
函数作用域
只可以在函数内部访问
function user() {let name = '李四';return name;}// 从全局无法获得console.log(name);// 需要使用函数调用console.log(user());
块作用域
在块内被访问被{}包裹的就是块。如if(){} while{}等
var不支持块作用域,需要用let
if (true) {let name = '张三';console.log(name);}// 外部无法访问到块内console.log(name);
作用域链
作用域可以根据代码层次分层,以便子作用域可以访问父作用域,通常是沿着链式的方式查找,先查找当前的作用域,当前没有,就向上找到父级的作用域,如果在父级的作用域中也找不到,就继续向上查找,直到window的作用域。如果在window中也找不到,就报错了
let a = 10;function first() {let b = 20;function second() {let c = 30;function three() {let d = 40;console.log('a:%d,b:%d,c:%d,d:%d', a, b, c, d);}three();}second();}first();
一个函数和对其周围状态(lexical environment,词法环境)的引用捆绑在一起(或者说函数被引用包围),这样的组合就是闭包(closure)。也就是说,闭包让你可以在一个内层函数中访问到其外层函数的作用域。在 JavaScript 中,每当创建一个函数,闭包就会在函数创建的同时被创建出来。
function user() {let name = '张三';function getName() {return name;}return getName;}let u = user();console.log(u());
类是用于创建对象的模板。
类的定义
使用带有
class关键字声明,类名首字母大写
类声明不会提升。你首先需要声明你的类,然后再使用它
class User {constructor(name,email) {this.name = name;this.email = email;}}类表达式是定义类的另一种方法
let User = class {constructor(name, email) {this.name = name;this.email = email;}}
类体和方法定义
一个类的类体是一对花括号/大括号 {} 中的部分。这是你定义类成员的位置,如构造函数,静态方法,原型方法,getter和setter等。
class User {// 构造方法:constructorconstructor(name,email) {this.name = name;this.email = email;}// 静态属性:staticstatic country = 'CN';// 私有属性,不能被子类继承:##age = 25;// 原型方法show() {return {name:this.name,email:this.email};}// 静态方法,不需要(new)实例化,直接用类名调用:staticstatic getCountry() {// 静态成员中的this表示的是当前类return this.country;}}// 类的实例化let user = new User('张三','tp@qq.com');console.dir(user);console.dir(User);
// 获取类的静态属性console.log(User.country);// 调用类的静态方法console.log(User.getCountry());
类的继承
使用
extends关键字在 类声明 或 类表达式 中用于创建一个类作为另一个类的一个子类
class User {}class Phper extends User {}console.dir(Phper);如果子类中定义了构造函数,那么它必须先调用
super()才能使用 this
// 父类class User {constructor(name, email) {this.name = name;this.email = email;}show() {return {name:this.name,email:this.email};}}// 子类class Phper extends User {constructor(name,email,gender) {super(name,email);this.gender = gender;}// 重写父类的方法show() {return {name:this.name,email:this.email,gender:this.gender};}}let u = new Phper('张三','tp@php.cn','男');console.dir(u);console.log(u.show());
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号