
批改狀態(tài):合格
老師批語:
<script>
let a = 1;
console.log(a);
//1
</script>
let a = 1;
//console.log(a);
function foo() {
let a = 5;
return a;
}
console.log(foo());
//5
{
let m = 8;
const M = 9;
}
console.log(M);
console.log(m);
//均會報錯
function foo() {
let a = 0;
return function f(params) {
return a;
}
}
let f = foo();
console.log(f());
const arr = ["cat", "dog", "pig"];
let i = 0;
while (i < arr.length) {
console.log(`animal: ${arr[i]}`);
i += 1;
}
//animal: cat
//animal: dog
//animal: pig
let i = 1;
do {
i += 1;
console.log(`animal: ${arr[i]}`);
} while (i < arr.length - 1);
//animal: pig
const stu = {
id: 1,
name: "Jack",
gender: "male",
graduate: false
}
for (const key in stu) {
console.log("%c%s", "color:green", stu[key]);
}
//1
//Jack
//male
//false
for (const item of arr) {
console.log(item);
}
//animal: cat
//animal: dog
//animal: pig
JavaScript 常被描述為一種基于原型的語言 (prototype-based language)——每個對象擁有一個原型對象,對象以其原型為模板、從原型繼承方法和屬性。原型對象也可能擁有原型,并從中繼承方法和屬性,一層一層、以此類推。這種關(guān)系常被稱為原型鏈 (prototype chain),它解釋了為何一個對象會擁有定義在其他對象中的屬性和方法。
準(zhǔn)確地說,這些屬性和方法定義在Object的構(gòu)造器函數(shù)(constructor functions)之上的prototype屬性上,而非對象實例本身。
function User(name, age) {
this.name = name;
this.age = age;
this.info = function () {
return { name: this.name, age: this.age };
}
}
function User(params) {
this.p = params;
}
const user = new User("hehe");
console.log(user);
//user對象的原型
console.log(user.__proto__);
//user構(gòu)造函數(shù)的原型
console.log(User.prototype);
console.log(user.__proto__ === User.prototype);
原型鏈
常見定義做法:
function User(name, age) {
this.name = name;
this.age = age;
// this.info = function () {
// return { name: this.name, age: this.age };
// }
}
//將info()放到原型prototype屬性中
User.prototype.info = function () {
return { name: this.name, age: this.age };
};
//生成對象
const user = new User("Tom", 23);
console.log(user);
//User {name: "Tom", age: 23}
console.log(user.info());
//{name: "Tom", age: 23}
class Animal {
//構(gòu)造方法
constructor(name, leg) {
this.name = name;
this.leg = leg;
}
//原型方法
info() {
return { name: this.name, leg: this.leg, isPet: this.#isPet };
}
//靜態(tài)方法
static eat() {
return "food";
}
//靜態(tài)屬性
static nature = "creature";
//私有屬性
#isPet = false;
//訪問器方法 getter, setter
get isPet() {
return this.#isPet;
}
set isPet(value) {
this.#isPet = value;
}
}
const cat = new Animal("Cat", 4);
const bird = new Animal("Bird", 2);
console.log(cat.info());
//{name: "Cat", leg: 4, isPet: false}
console.log(bird.info());
//{name: "Bird", leg: 2, isPet: false}
console.log(`動物吃:${Animal.eat()}`);
//動物吃:food
console.log(`動物本身是:${Animal.nature}`);
//動物本身是:creature
console.log(`貓是寵物嗎? ${cat.isPet}`);
//貓是寵物嗎? false
cat.isPet = true;
console.log(`貓是寵物嗎? ${cat.isPet}`);
//貓是寵物嗎? true
//繼承
class Dog extends Animal {
//繼承
//第一步必須執(zhí)行父類構(gòu)造方法,否則無法使用this
constructor(name, type) {
super(name, 4);
//新成員初始化
this.type = type;
}
info() {
return { name: this.name, leg: this.leg, type: this.type };
}
//父類的私有屬性不會被繼承
}
const dog = new Dog("Dog", "哈士奇");
console.log(dog.info());
//{name: "Dog", leg: 4, type: "哈士奇"}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號