国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

JavaScript面向對象繼承原理與實現(xiàn)方法分析

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:10:30
文檔

JavaScript面向對象繼承原理與實現(xiàn)方法分析

JavaScript面向對象繼承原理與實現(xiàn)方法分析:本文實例講述了JavaScript面向對象繼承原理與實現(xiàn)方法。分享給大家供大家參考,具體如下: 1、構造函數(shù)、原型和實例的關系 構造函數(shù)有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數(shù)的指針constructor 。 實例包含一個指向原型對
推薦度:
導讀JavaScript面向對象繼承原理與實現(xiàn)方法分析:本文實例講述了JavaScript面向對象繼承原理與實現(xiàn)方法。分享給大家供大家參考,具體如下: 1、構造函數(shù)、原型和實例的關系 構造函數(shù)有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數(shù)的指針constructor 。 實例包含一個指向原型對

本文實例講述了JavaScript面向對象繼承原理與實現(xiàn)方法。分享給大家供大家參考,具體如下:

1、構造函數(shù)、原型和實例的關系

構造函數(shù)有一個原型屬性prototype指向一個原型對象。

原型對象包含一個指向構造函數(shù)的指針constructor

實例包含一個指向原型對象的內部指針[[prototype]]

2、通過原型鏈實現(xiàn)繼承

基本思想:利用原型讓一個引用類型繼承另一個引用類型的屬性和方法,子類型可以訪問超類型的所有屬性和方法。原型鏈的構建是將一個類型的實例賦值給另一個構造函數(shù)的原型實現(xiàn)的。實現(xiàn)的本質是重寫原型對象,代之以一個新類型的實例。

function Person(name) {
 this.name = name;
}
Person.prototype.sayHello = function() {
 alert("Hello, " + this.name);
}
var person = new Person("Alice");
person.sayHello(); // Hello, Alice
function Student() {
}
Student.prototype = new Person("Bruce");
Student.prototype.id = 16;
Student.prototype.showId = function() {
 alert(this.id);
}
var student = new Student();
student.sayHello(); // Hello, Bruce
student.showId(); // 16

注意:不能用對象字面量創(chuàng)建原型方法,這樣會重寫原型鏈,導致繼承無效。

function Person(name) {
 this.name = name;
}
Person.prototype.sayHello = function() {
 alert("Hello, " + this.name);
}
var person = new Person("Alice");
person.sayHello(); // Hello, Alice
function Student() {
}
Student.prototype = new Person("Bruce");
Student.prototype.id = 16;
Student.prototype = {
 showId: function() {
 alert(this.id);
 }
};
var student = new Student();
student.sayHello(); // 報錯:student.sayHello is not a function
student.showId(); // 16

student指向Student的原型,Student的原型又指向Person的原型。

student.sayHello()原型鏈搜索機制:

1)搜索student實例中是否有sayHello()

2)搜索Student.prototype是否有sayHello()

3)搜索Person.prototype是否有sayHello()

子類型有時候需要覆蓋超類型的某個方法,或者需要添加超類型中不存在的某個方法。

function Person(name) {
 this.name = name;
}
Person.prototype.sayHello = function() {
 alert("Hello, " + this.name);
}
var person = new Person("Alice");
person.sayHello(); // Hello, Alice
function Student() {
}
Student.prototype = new Person("Bruce");
Student.prototype.id = 16;
Student.prototype.showId = function() {
alert(this.id);
}
Student.prototype.sayHello = function() {
 alert("Hi, " + this.name);
}
var student = new Student();
student.sayHello(); //Hi, Bruce
student.showId(); // 16

注意:給原型覆蓋或添加方法的代碼一定要放在替換原型的語句之后。

function Person(name) {
 this.name = name;
}
Person.prototype.sayHello = function() {
 alert("Hello, " + this.name);
}
var person = new Person("Alice");
person.sayHello(); // Hello, Alice
function Student() {
}
Student.prototype.sayHello = function() {
 alert("Hi, " + this.name);
}
Student.prototype = new Person("Bruce");
Student.prototype.id = 16;
Student.prototype.showId = function() {
alert(this.id);
}
var student = new Student();
student.sayHello(); // Hello, Bruce
student.showId(); // 16

確定實例和原型的關系:

(1)instanceof

alert(student instanceof Object); // true
alert(student instanceof Student); // true
alert(student instanceof Person); // true

(2)isProtptypeOf

alert(Object.prototype.isPrototypeOf(student)); // true
alert(Student.prototype.isPrototypeOf(student)); // true
alert(Person.prototype.isPrototypeOf(student)); // true

(3)getPrototypeOf

Object.getPrototypeOf(student1) == Student.prototype

使用原型鏈實現(xiàn)繼承的問題:

(1)引用類型的屬性會被實例共享,原型實現(xiàn)繼承時,原型會變成另外一個類型的實例,實例的屬性則變成了現(xiàn)在的原型屬性,從而被共享。

function Person(name, age) {
 this.friends = ["Cindy","David"];
}
function Student() {
}
Student.prototype = new Person();
var student1 = new Student();
student1.friends.push("Bruce");
alert(student1.friends); // "Cindy","David","Bruce"
var student2 = new Student();
alert(student1.friends); // "Cindy","David","Bruce"

(2)在創(chuàng)建子類型的實例時,不能向超類型的構造函數(shù)中傳遞參數(shù),實際上,應該是沒有辦法在不影響所有對象實例的情況下,給超類型的構造函數(shù)傳遞參數(shù)。

實際中很少單獨使用原型鏈實現(xiàn)繼承。

3、通過構造函數(shù)實現(xiàn)繼承

基本思想:在子類型構造函數(shù)的內部調用超類型構造函數(shù)。通過使用apply()call()方法也可以在新創(chuàng)建的對象上執(zhí)行構造函數(shù)。

function Person(name, age) {
 this.name = name;
 this.age = age;
}
function Student() {
 Person.call(this,"Alice",22); // 繼承了構造函數(shù)Person,同時還傳遞了參數(shù)
 this.id = 16; // 實例屬性
}
var student = new Student();
alert(student.name); // "Alice"
alert(student.age); // 22
alert(student.id); // 16

使用構造函數(shù)實現(xiàn)繼承的問題:

(1)在超類型的原型中定義的方法,對子類型而言是不可見的,結果所有類型都只能使用構造函數(shù)模式。

(2)要想子類能夠訪問超類定義的方法,方法只能在構造函數(shù)中定義,但方法在構造函數(shù)中定義時,函數(shù)復用無從談起。

function Person(name, age) {
 this.name = name;
 this.age = age;
}
Person.prototype.showName = function() {
 alert(this.name);
};
function Student() {
 Person.call(this,"Alice",22);
 this.id = 16;
}
var student = new Student();
alert(student.showName()); // 報錯:student.showName is not a function

實際中很少單獨使用使用構造函數(shù)實現(xiàn)繼承。

4、組合使用原型鏈和構造函數(shù)實現(xiàn)繼承

思路:使用原型鏈繼承共享的屬性和方法,使用構造函數(shù)繼承實例屬性。

效果:既通過在原型上定義方法實現(xiàn)了函數(shù)復用,又能夠保證每個實例都有自己的屬性。

function Person(name, age) {
 this.name = name;
 this.age = age;
 this.friends = ["Cindy","David"];
}
Person.prototype.sayHello = function() {
 alert("Hello, " + this.name);
}
function Student(name, age, id) {
 Person.call(this, name, age);
 this.id = id;
}
Student.prototype = new Person();
Student.prototype.showId = function() {
 alert(this.id);
}
var student1 = new Student("Alice", 22, 16);
student1.friends.push("Emy");
alert(student1.friends); // "Cindy","David","Emy"
student1.sayHello(); // Hello, Alice
student1.showId(); // 16
var student2 = new Student("Bruce", 23, 17);
alert(student2.friends); // "Cindy","David"
student2.sayHello(); // Hello, Bruce
student2.showId(); // 17

更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數(shù)學運算用法總結》

希望本文所述對大家JavaScript程序設計有所幫助。

聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

JavaScript面向對象繼承原理與實現(xiàn)方法分析

JavaScript面向對象繼承原理與實現(xiàn)方法分析:本文實例講述了JavaScript面向對象繼承原理與實現(xiàn)方法。分享給大家供大家參考,具體如下: 1、構造函數(shù)、原型和實例的關系 構造函數(shù)有一個原型屬性prototype指向一個原型對象。 原型對象包含一個指向構造函數(shù)的指針constructor 。 實例包含一個指向原型對
推薦度:
標簽: 原理 js 方式
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产成人h福利小视频在线观看 | 多人伦精品一区二区三区视频 | 午夜视频在线观看国产 | 欧美wwww | 欧美 日韩 国产 色 欧美 日韩 中文 | 亚洲欧美在线观看视频 | 国产成人高清一区二区私人 | 国产成人久久蜜一区二区 | 日韩欧美高清一区 | 在线播放一区二区精品产 | 国产亚洲精品一品区99热 | 久久亚洲精品国产精品婷婷 | 美女洗澡一级毛片 | 日韩在线欧美高清一区 | 国产精品成人一区二区 | 国产在线观看精品 | 国产精品久久久久久久久99热 | 欧美日本在线视频 | 免费国产小视频在线观看 | 午夜精品一区二区三区在线视 | 国产高清在线 | 欧美亚洲欧美 | 欧美多p| 极品久久| 尤物视频黄 | 欧美日韩国产一区二区三区播放 | 一区二区三区成人 | 国产精品永久免费自在线观看 | 日韩欧美亚洲国产高清在线 | 欧美日韩精 | 韩国理论三级在线观看视频 | 日韩亚洲欧美综合 | 国模吧双双大尺度炮交gogo | 日韩欧美视频 | 精品欧美一区二区三区免费观看 | 欧美综合一区 | 国产成人亚洲综合一区 | 91精品啪国产在线观看免费牛牛 | 国产aⅴ一区二区三区 | 一区二区久久 | 亚洲欧美色欧另类欧 |