bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

JS怎么實現面向對象繼承

這篇文章給大家分享的是有關JS怎么實現面向對象繼承的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創新互聯專注于婁星企業網站建設,響應式網站,成都商城網站開發。婁星網站建設公司,為婁星等地區提供建站服務。全流程按需規劃網站,專業設計,全程項目跟蹤,創新互聯專業和態度為您提供的服務

JS是什么

JS是JavaScript的簡稱,它是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,主要用于web的開發,可以給網站添加各種各樣的動態效果,讓網頁更加美觀。

本文實例講述了JS實現面向對象繼承的5種方式。分享給大家供大家參考,具體如下:

js是門靈活的語言,實現一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現的,根據js語言的本身的特性,js實現繼承有以下通用的幾種方式

1. 使用對象冒充實現繼承(該種實現方式可以實現多繼承)

實現原理:讓父類的構造函數成為子類的方法,然后調用該子類的方法,通過this關鍵字給所有的屬性和方法賦值

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.parent=Parent;
  this.parent(firstname);
  delete this.parent;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
var mychild=new Child("李");
mychild.saySomeThing();

2. 采用call方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)

實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
  this.getName=function()
  {
    return firstname;
  }
}
var child=new Child("張");
Parent.call(child,child.getName());
child.saySomeThing();

3. 采用Apply方法改變函數上下文實現繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)

實現原理:改變函數內部的函數上下文this,使它指向傳入函數的具體對象

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
  this.getName=function()
  {
    return firstname;
  }
}
var child=new Child("張");
Parent.apply(child,[child.getName()]);
child.saySomeThing();

4. 采用原型鏈的方式實現繼承

實現原理:使子類原型對象指向父類的實例以實現繼承,即重寫類的原型,弊端是不能直接實現多繼承

function Parent()
{
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
Child.prototype=new Parent();
var child=new Child("張");
child.saySomeThing();

5. 采用混合模式實現繼承

function Parent()
{
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
Parent.prototype.sayParent=function()
{
  alert("this is parentmethod!!!");
}
function Child(firstname)
{
  Parent.call(this);
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
Child.prototype=new Parent();
var child=new Child("張");
child.saySomeThing();
child.sayParent();

感謝各位的閱讀!關于“JS怎么實現面向對象繼承”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網頁題目:JS怎么實現面向對象繼承
當前路徑:http://vcdvsql.cn/article20/pdssjo.html

成都網站建設公司_創新互聯,為您提供網站排名關鍵詞優化微信公眾號手機網站建設搜索引擎優化網站導航

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都做網站