在JavaScript的ES6版本之后
/*聲明變量的關(guān)鍵字新增let绞呈,除了可以通過var定義一個(gè)對(duì)象乘客,
也能使用let定義一個(gè)變量*/
let obj = new Object();
console.log(obj, typeof obj);
let obj = {}
console.log(obj, typeof obj);
-
“類”的含義在JavaScript中正式出現(xiàn),與構(gòu)造函數(shù)一樣,它也是專門用于對(duì)象的創(chuàng)建的
-
實(shí)例屬性與實(shí)例方法绞惦,寫在constructor函數(shù)內(nèi)部
-
靜態(tài)屬性書寫在constructor函數(shù)外部,class的【{}】里面
-
靜態(tài)方法和靜態(tài)屬性通過static關(guān)鍵字聲明洋措,靜態(tài)方法不在使用【:】【=】進(jìn)行函數(shù)名稱的賦值
// 定義一個(gè)類
/*格式
class 類的名稱 {
constructor (形參列表) {
// 實(shí)例屬性與實(shí)例方法
this.屬性名稱 = 形參值;
this.方法名稱 = 函數(shù);
}
// 靜態(tài)方法
static 方法名稱 () {
方法代碼
}
}*/
class Person{
constructor (myName, myAge) {
// 實(shí)例屬性與實(shí)例方法
this.name = myName;
this.age = myAge;
this.say = function () {
console.log(this.name + this.age + "歲济蝉,對(duì)你說你好");
}
}
// 靜態(tài)屬性
static num = 666;
// 靜態(tài)方法
static run () {
console.log("我是靜態(tài)方法");
}
}
// 對(duì)象創(chuàng)建成功
let obj = new Person("zs", 26);
console.log(obj);
// 實(shí)例方法可以使用
obj.say();
// 靜態(tài)屬性可以通過類名獲取
console.log(Person.num);
// 靜態(tài)方法可以調(diào)用
Person.run();
/*不通過static關(guān)鍵字聲明,直接以函數(shù)表達(dá)式的形式書寫在constructor函數(shù)外部菠发,
那么該方法會(huì)自動(dòng)添加到該類的原型上*/
class Person{
constructor (myName, myAge) {
// 實(shí)例屬性與實(shí)例方法
this.name = myName;
this.age = myAge;
this.say = function () {
console.log(this.name + this.age + "歲王滤,對(duì)你說你好");
}
}
// 公共方法
test () {
console.log(this.name + "上廁所");
}
// 靜態(tài)屬性
static num = 666;
// 靜態(tài)方法
static run () {
console.log("我是靜態(tài)方法");
}
}
// 對(duì)象創(chuàng)建成功
let obj = new Person("zs", 26);
console.log(obj);
// 實(shí)例方法可以使用
obj.say();
// 靜態(tài)屬性可以通過類名獲取
console.log(Person.num);
// 靜態(tài)方法可以調(diào)用
Person.run();
// 公共方法可以通過實(shí)例對(duì)象調(diào)用
obj.test();
-
Class定義的類的原型對(duì)象注意點(diǎn)
// 通過Class定義的類不能自定義(重構(gòu))原型對(duì)象
class Person{
constructor (myName, myAge) {
this.name = myName;
this.age = myAge;
this.say = function () {
console.log("我是類的方法");
}
}
}
Person.prototype = {
value: 1,
test: function () {
console.log("我是類的原型方法");
}
}
console.log(Person.prototype);
// Person的原型對(duì)象里沒有自定義的屬性和方法
// 通過Class定義類只能動(dòng)態(tài)的往原型對(duì)象的添加屬性和方法
class Person{
constructor (myName, myAge) {
this.name = myName;
this.age = myAge;
this.say = function () {
console.log("我是類的方法");
}
}
}
Person.prototype.value = 1;
Person.prototype.test = function () {
console.log("我是類的原型方法");
};
console.log(Person.prototype);
// Person的原型對(duì)象里有自定義的屬性和方法
// 通過extends關(guān)鍵字
/*格式: Student extends Person
Person類的方法和屬性被Student繼承*/
// 含義:相當(dāng) Person.call(this)
// 通過super(傳遞參數(shù))
class Person {
constructor (myName, myAge) {
this.name = myName;
this.age = myAge;
this.say = function () {
console.log("Person的實(shí)例方法");
}
}
run () {
console.log("我是Person的公共方法");
}
}
class Student extends Person {
constructor (myName, myAge, myValue) {
super(myName, myAge);
this.value = myValue;
this.test = function () {
console.log("student的方法");
}
}
test () {
this.say();
}
}
let stu = new Student("zs", 18, 85);
console.log(stu);
console.log(stu.value);
stu.test();
stu.run();
// 不用修改Student的原型指向也能訪問Person的公共方法