-
1. 在ES6之前如何定義一個(gè)類?
-
通過構(gòu)造函數(shù)來定義一個(gè)類
function Person(myName, myAge) { // 實(shí)例屬性 // this.name = "lnj"; // this.age = 34; this.name = myName; this.age = myAge; // 實(shí)例方法 this.say = function () { console.log(this.name, this.age); } // 靜態(tài)屬性 Person.num = 666; // 靜態(tài)方法 Person.run = function () { console.log("run"); } } // let p = new Person(); // p.say(); // lnj 34 let p = new Person("zs", 18); p.say(); // zs 18 console.log(Person.num); Person.run(); // run
-
-
2. 在ES6開始如何定義一個(gè)類?
-
從ES6開始系統(tǒng)提供了一個(gè)名稱叫做 class 的關(guān)鍵字, 這個(gè)關(guān)鍵字就是專門用于定義類的
class Person{ // 當(dāng)我們通過new創(chuàng)建對(duì)象的時(shí)候, 系統(tǒng)會(huì)自動(dòng)調(diào)用constructor constructor(myName, myAge){ // 構(gòu)造方法 this.name = myName; this.age = myAge; } // 實(shí)例屬性 // name = "lnj"; // age = 34; // 實(shí)例方法 say(){ console.log(this.name, this.age); } // 靜態(tài)屬性 static num = 666; // 靜態(tài)方法 static run() { console.log("run"); } } // let p = new Person(); // p.say(); // lnj 34 let p = new Person("zs", 18); p.say(); // zs 18 // console.log(Person.num); // Person.run(); // run
-
-
3. 類和對(duì)象注意點(diǎn)
-
注意點(diǎn)1: 在ES6標(biāo)準(zhǔn)中添加實(shí)例屬性都需要在constructor中添加
class Person{ // 注意點(diǎn)1:以下定義"實(shí)例屬性"的方式并不是ES6正版標(biāo)準(zhǔn)中的寫法, 大部分瀏覽器不支持 // 在ES6標(biāo)準(zhǔn)中添加實(shí)例屬性都需要在constructor中添加 // 實(shí)例屬性 // name = "lnj"; // age = 34; constructor(){ this.name = "lnj"; this.age = 34; } let p = new Person(); console.log(p); // Person {name: "lnj", age: 34}
-
注意點(diǎn)2: 在ES6標(biāo)準(zhǔn)中static只支持定義靜態(tài)方法不支持定義靜態(tài)變量
class Person{ // 注意點(diǎn)2: 以下定義"靜態(tài)屬性"的方式并不是ES6正式版標(biāo)準(zhǔn)中的寫法, 大部分的瀏覽器不支持 // 在ES標(biāo)準(zhǔn)中static只支持定義靜態(tài)方法不支持定義靜態(tài)變量 // 靜態(tài)屬性 // static num = 666; // 這樣定義靜態(tài)屬性在有些瀏覽器中會(huì)報(bào)錯(cuò) // 靜態(tài)方法 static run(){ console.log("run"); } } // 必須通過外部動(dòng)態(tài)添加靜態(tài)屬性 Person.num = 666; let p = new Person(); console.log(p); // Person {}
-
注意點(diǎn)3: 在ES6中如果想給實(shí)例對(duì)象添加實(shí)例方法, 那么必須將實(shí)例方法寫到constructor構(gòu)造方法中
class Person{ constructor(myName, myAge){ // 這里的代碼相當(dāng)于ES6之前在構(gòu)造函數(shù)中的代碼 this.name = myName; this.age = myAge; // 實(shí)例方法 this.hi = function () { console.log("hi"); } } // 在這里寫方法會(huì)默認(rèn)保存到原型對(duì)象中 say(){ // 這里的代碼相當(dāng)于ES6之前給原型對(duì)象添加的代碼 console.log("say"); } } let p = new Person("lnj", 34); console.log(p); // Person {name: "lnj", age: 34, hi: ?}
-
4. 如何在原型對(duì)象中保存屬性和方法
-
在ES6之前如何在原型對(duì)象中保存屬性和方法
-
- 動(dòng)態(tài)的給原型對(duì)象添加屬性和方法
function Person(myName, myAge) { // 實(shí)例屬性 this.name = myName; this.age = myAge; // 實(shí)例方法 this.hi = function () { console.log("hi"); } } // 動(dòng)態(tài)的給原型對(duì)象添加屬性和方法 Person.prototype.type = "人"; Person.prototype.say = function () { console.log(this.name, this.age); } let p = new Person("lnj", 34); console.log(Person.prototype); // {constructor: ?, type: "人", say: ?}
-
- 自定義原型對(duì)象
function Person(myName, myAge) { // 實(shí)例屬性 this.name = myName; this.age = myAge; // 實(shí)例方法 this.hi = function () { console.log("hi"); } } // 2.自定義原型對(duì)象 Person.prototype = { constructor: Person, type: "人", say: function () { console.log(this.name, this.age); } } let p = new Person("lnj", 34); console.log(Person.prototype); // {constructor: ?, type: "人", say: ?}
-
-
在ES6開始如何在原型對(duì)象中保存屬性和方法
-
- 動(dòng)態(tài)的給原型對(duì)象添加屬性和方法
class Person{ constructor(myName, myAge){ this.name = myName; this.age = myAge; this.hi = function () { console.log("hi"); } } } // 1.動(dòng)態(tài)的給原型對(duì)象添加屬性和方法 Person.prototype.type = "人"; Person.prototype.say = function () { console.log(this.name, this.age); } let p = new Person("lnj", 34); console.log(Person.prototype); // {constructor: ?, type: "人", say: ?}
-
ES6中如果是通過class創(chuàng)建的類, 不能自定義原型對(duì)象
class Person{ constructor(myName, myAge){ this.name = myName; this.age = myAge; this.hi = function () { console.log("hi"); } } } // 2.ES6中如果是通過class創(chuàng)建的類, 不能自定義原型對(duì)象 let obj = { constructor: Person, type:"人", say: function () { console.log(this.name, this.age); } } Person.prototype = obj; console.log(Person.prototype); // {constructor: ?}
-
-
在ES6之前如何在原型對(duì)象中保存屬性和方法