1). 全局作用域,默认的,不可删除,如果在是浏览器中运行js,那么全局对象就是window
let site = “google”;
console.log(site);
console.log(window.site);
2). 函数作用域, 仅限在当前作用域内访问, 外部不可见,否则会报错。
3). 块作用域,包在大括号里的语句就是块
{var a = 1;
var B = 2;}
console.log(a, B);
function Hello() {
let email = “abc@abc.com”;
return function d(){
return email;
},
};
console.log(Hello());
<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号