批改状态:合格
老师批语:
1. 作用域
1). 全局作用域,默认的,不可删除,如果在是浏览器中运行js,那么全局对象就是windowlet site = “google”;
console.log(site);
console.log(window.site);
作用域链, 自身查找,如果没有就返回上一级
function getSite() {
let domain = google.com
return `${site} [${domain}];
console.log(getSite());
3). 块作用域,包在大括号里的语句就是块
{var a = 1;
var B = 2;}
console.log(a, B);
能够访问自由变量的函数在函数A内部有个函数B,函数B可以访问函数A中的变量,函数B就是闭包通过闭包来访问内部的私有变量
function sum(a, b) {return a + b + c;}console.log(sum(4, 5));function Hello() {let email = “abc@abc.com”;return function d(){return email;},};console.log(Hello());
constructor是类的构造函数 用与传递参数 返回实例对象,通过new生成实例时自动调用该方法.
<script>function User(name, email) {this.name = name;this.email = email;this.show = function () {return { name: this.name, email: this.email };};}const user = new User(“Peter”, “abc@abc.com”);console.log(user);</script>
原型共享方法,通过对象来调用
<script>class User1 {constructor(name, email) {this.name = name;this.email = email;}show() {return { name: this.name, email: this.email };}}const user1 = new User1(“Bill”, “ab@ab.com”);console.log(user1);console.log(user1.show());</script>
class Child extends User1 constructor(name, email, gender) {super(name, email);this.gender = gender;}show() {return { name: this.name, email: this.email, gender: this.gender };}}const child = new Child("Bob", "oy@qq.com", "male");console.log(child.show());
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号