亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解JS面向?qū)ο缶幊?/h1>
 更新時(shí)間:2016年01月24日 10:25:30   投稿:lijiao  
這篇文章主要為大家介紹了js面向?qū)ο缶幊?,感興趣的小伙伴們可以參考一下

因?yàn)镴avaScript是基于原型(prototype)的,沒(méi)有類(lèi)的概念(ES6有了,這個(gè)暫且不談),我們能接觸到的都是對(duì)象,真正做到了一切皆為對(duì)象

所以我們?cè)僬f(shuō)對(duì)象就有些模糊了,很多同學(xué)會(huì)搞混類(lèi)型的對(duì)象和對(duì)象本身這個(gè)概念,我們?cè)诮酉聛?lái)的術(shù)語(yǔ)中不提對(duì)象,我們使用和Java類(lèi)似的方式,方便理解

方式一

類(lèi)(函數(shù)模擬)

function Person(name,id){
 //實(shí)例變量可以被繼承
 this.name = name;
 //私有變量無(wú)法被繼承
 var id = id;
 //私有函數(shù)無(wú)法被繼承
 function speak(){
  alert("person1");
 }
}
//靜態(tài)變量,無(wú)法被繼承
Person.age = 18;
//公有函數(shù)可以被繼承
Person.prototype.say = function(){
 alert("person2");
}

繼承,并調(diào)用父類(lèi)方法

function Person(name,id){
 //實(shí)例變量可以被繼承
 this.name = name;
 //私有變量無(wú)法被繼承
 var id = id;
 //私有函數(shù)無(wú)法被繼承
 function speak(){
  alert("person1");
 }
}
//靜態(tài)變量,無(wú)法被繼承
Person.age = 18;
//公有靜態(tài)成員可被繼承
Person.prototype.sex = "男";
//公有靜態(tài)函數(shù)可以被繼承
Person.prototype.say = function(){
 alert("person2");
}
//創(chuàng)建子類(lèi)
function Student(){
}
//繼承person
Student.prototype = new Person("iwen",1);
//修改因繼承導(dǎo)致的constructor變化
Student.prototype.constructor = Student;
var s = new Student();
alert(s.name);//iwen
alert(s instanceof Person);//true
s.say();//person2

繼承,復(fù)寫(xiě)父類(lèi)方法且實(shí)現(xiàn)super()

function Person(name,id){
 //實(shí)例變量可以被繼承
 this.name = name;
 //私有變量無(wú)法被繼承
 var id = id;
 //私有函數(shù)無(wú)法被繼承
 function speak(){
  alert("person1");
 }
}
//靜態(tài)變量,無(wú)法被繼承
Person.age = 18;
//公有靜態(tài)成員可被繼承
Person.prototype.sex = "男";
//公有靜態(tài)函數(shù)可以被繼承
Person.prototype.say = function(){
 alert("person2");
}
//創(chuàng)建子類(lèi)
function Student(){
}
//繼承person
Student.prototype = new Person("iwen",1);
//修改因繼承導(dǎo)致的constructor變化
Student.prototype.constructor = Student;
//保存父類(lèi)的引用
var superPerson = Student.prototype.say;
//復(fù)寫(xiě)父類(lèi)的方法
Student.prototype.say = function(){
 //調(diào)用父類(lèi)的方法
 superPerson.call(this);
 alert("Student");
}
//創(chuàng)建實(shí)例測(cè)試
var s = new Student();
alert(s instanceof Person);//true
s.say();//person2 student

繼承的封裝函數(shù)

function extend(Child, Parent) {

    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }

uber意思是為子對(duì)象設(shè)一個(gè)uber屬性,這個(gè)屬性直接指向父對(duì)象的prototype屬性。(uber是一個(gè)德語(yǔ)詞,意思是”向上”、”上一層”。)這等于在子對(duì)象上打開(kāi)一條通道,可以直接調(diào)用父對(duì)象的方法。這一行放在這里,只是為了實(shí)現(xiàn)繼承的完備性,純屬備用性質(zhì)。

方式二

相當(dāng)于拷貝,通過(guò)定義的_this對(duì)象來(lái)承載想要被繼承的對(duì)象,這樣的話(huà)通過(guò)傳遞_this就可以實(shí)現(xiàn)繼承,比上面那種好理解些

//創(chuàng)建父類(lèi)
function Person(name,id){
 //創(chuàng)建一個(gè)對(duì)象來(lái)承載父類(lèi)所有公有東西
 //也就是說(shuō)_this承載的對(duì)象才會(huì)被傳遞給子類(lèi)
 var _this = {};
 _this.name = name;
 //這樣的是不會(huì)傳遞下去的
 this.id = id;
 //承載方法
 _this.say = function(){
  alert("Person");
 }
 //返回_this對(duì)象
 return _this;
}
//子類(lèi)
function Student(){
 //獲取person的_this對(duì)象,從而模仿繼承
 var _this = Person("iwne",1);
 //保存父類(lèi)的_this引用
 var superPerson = _this.say;
 //復(fù)寫(xiě)父類(lèi)的方法
 _this.say = function(){
  //執(zhí)行父類(lèi)的say
  superPerson.call(_this);
  alert("Student");
 }
 return _this;
}
var s = new Student();
s.say();

希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論