国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

老生常談JS中的繼承及實現(xiàn)代碼

2024-05-06 15:32:51
字體:
供稿:網(wǎng)友

JS雖然不像是JAVA那種強類型的語言,但也有著與JAVA類型的繼承屬性,那么JS中的繼承是如何實現(xiàn)的呢?

一、構(gòu)造函數(shù)繼承

在構(gòu)造函數(shù)中,同樣屬于兩個新創(chuàng)建的函數(shù),也是不相等的 function Fn(name){  this.name = name;  this.show = function(){   alert(this.name);  } } var obj1 = new Fn("AAA"); var obj2 = new Fn("BBB"); console.log(obj1.show==obj2.show);  //false 此時可以看出構(gòu)造函數(shù)的多次創(chuàng)建會產(chǎn)生多個相同函數(shù),造成冗余太多。 利用原型prototype解決。首先觀察prototype是什么東西 function Fn(){} console.log(Fn.prototype); //constructor表示當(dāng)前的函數(shù)屬于誰 //__proto__ == [[prototype]],書面用語,表示原型指針 var fn1 = new Fn(); var fn2 = new Fn(); Fn.prototype.show = function(){  alert(1); } console.log(fn1.show==fn2.show);  //ture 此時,任何一個對象的原型上都有了show方法,由此得出,構(gòu)造函數(shù)Fn.prototype身上的添加的方法,相當(dāng)于添加到了所有的Fn身上。

二、call和applay繼承

function Father(skill){  this.skill = skill;  this.show = function(){   alert("我會"+this.skill);  } } var father = new Father("絕世木匠"); function Son(abc){  //這里的this指向函數(shù)Son的實例化對象  //將Father里面的this改變成指向Son的實例化對象,當(dāng)相遇將father里面所有的屬性和方法都復(fù)制到了son身上  //Father.call(this,abc);//繼承結(jié)束,call適合固定參數(shù)的繼承  //Father.apply(this,arguments);//繼承結(jié)束,apply適合不定參數(shù)的繼承 } father.show() var son = new Son("一般木匠"); son.show();

三、原型鏈繼承(demo)

這個的么實現(xiàn)一個一個簡單的拖拽,a->b的一個繼承。把a的功能繼承給b。

HTML:

 <div id="drag1"></div> <div id="drag2"></div>

CSS:

*{margin: 0;padding: 0;} #drag1{width: 100px;height: 100px;background: red;position: absolute;} #drag2{width: 100px;height: 100px;background: black;position: absolute;left: 500px;}

 JS:  

function Drag(){}  Drag.prototype={   constructor:Drag,   init:function(id){    this.ele=document.getElementById(id);    this.cliW=document.documentElement.clientWidth||document.body.clientWidth;    this.cliH=document.documentElement.clientHeight||document.body.clientHeight;    var that=this;    this.ele.onmousedown=function(e){     var e=event||window.event;     that.disX=e.offsetX;     that.disY=e.offsetY;     document.onmousemove=function(e){      var e=event||window.event;      that.move(e);     }     that.ele.onmouseup=function(){      document.onmousemove=null;     }    }     },   move:function(e){    this.x=e.clientX-this.disX;    this.y=e.clientY-this.disY;    this.x=this.x<0?this.x=0:this.x;    this.y=this.y<0?this.y=0:this.y;    this.x=this.x>this.cliW-this.ele.offsetWidth?this.x=this.cliW-this.ele.offsetWidth:this.x;    this.y=this.y>this.cliH-this.ele.offsetHeight?this.y=this.cliH-this.ele.offsetHeight:this.y;    this.ele.style.left=this.x+'px';    this.ele.style.top=this.y+'px';   }  }  new Drag().init('drag1')  function ChidrenDrag(){}  ChidrenDrag.prototype=new Drag()  new ChidrenDrag().init('drag2')            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 香港| 通州区| 营口市| 醴陵市| 桃园县| 阿巴嘎旗| 高雄县| 奇台县| 通山县| 大名县| 南安市| 开江县| 松滋市| 宁南县| 株洲市| 林口县| 台安县| 西城区| 正安县| 同德县| 婺源县| 青铜峡市| 佳木斯市| 南京市| 汶上县| 彭山县| 哈密市| 青海省| 太保市| 加查县| 巴林左旗| 瓦房店市| 平舆县| 三亚市| 沂南县| 凤城市| 韶关市| 龙游县| 遂溪县| 聂荣县| 天祝|