1.OOP 指什么?有哪些特性
- OOP:Object-oriented programming,縮寫OOP,即面向?qū)ο蟪绦蛟O(shè)計,其中兩個最重要的概念就是類和對象。類只是具備了某些功能和屬性的抽象模型,而實際應(yīng)用中需要一個一個實體,也就是需要對類進行實例化,類在實例化之后就是對象。
對象:將類實例化
實例化:就是將類實現(xiàn)的過程
目的:
1.改善可讀性
2.提高重用性原則:開放封閉原則
1.開放:對模塊進行擴展
2.封閉:不必改動模塊的源代碼
- 特性:三要素繼承,封裝,多態(tài)
1.繼承性:子類自動繼承其父級類中的屬性和方法,并可以添加新的屬性和方法或者對部分屬性和方法進行重寫。繼承增加了代碼的可重用性。
2.多態(tài)性:子類繼承了來自父級類中的屬性和方法,并對其中部分方法進行重寫。
3.封裝性:將一個類的使用和實現(xiàn)分開,只保留部分接口和方法與外部聯(lián)系。
2.如何通過構(gòu)造函數(shù)的方式創(chuàng)建一個擁有屬性和方法的對象?
var Animal = function(){
this.name = '動物'
}//構(gòu)造函數(shù)
Animal.prototype.say =function(){
console.log(this.name+'叫')
}
var animal = new Animal()//實例化,得到一個對象
animal.say()

3. prototype 是什么?有什么特性
function定義的對象有一個prototype屬性,prototype屬性又指向了一個prototype對象,注意prototype屬性與prototype對象是兩個不同的東西,要注意區(qū)別。在prototype對象中又有一個constructor屬性,這個constructor屬性同樣指向一個constructor對象,而這個constructor對象恰恰就是這個function函數(shù)本身。

這個prototype到底有什么作用呢?看下面的例子:
function obj1(){}
obj1.prototype.name = "zyn";
var test = new obj1();
console.log(test.name)// zyn
奇怪吧,明明沒有為test設(shè)置name屬性,可是為什么會有值?
這就是prototype的功勞了,prototype屬性中的name對象,在new構(gòu)造函數(shù)之后,被繼承到了對象test的屬性中。接著看:
var name = "js";
function obj2(name){
console.log(this.name);//css
}
obj2.prototype.name = "css";
var test = new obj2();
console.log(test.name)//css
第一步是建立一個新對象(test)。
第二步將該對象(test)內(nèi)置的原型對象設(shè)置為構(gòu)造函數(shù)prototype 屬性引用的那個原型對象。
第三步就是將該對象(test)作為this 參數(shù)調(diào)用構(gòu)造函數(shù),完成成員設(shè)置等初始化工作。
其中第二步中出現(xiàn)了一個新名詞就是內(nèi)置的原型對象,注意這個新名詞跟prototype對象不是一回事, 為了區(qū)別我叫它inobj,inobj就指向了函數(shù)的prototype對象。在prototype對象中出現(xiàn)的任何屬性或者函數(shù)都可以在test對象中直接使用,這個就是JS中的原型繼承了。
通常,這樣創(chuàng)建一個對象:
function person(name){
this.name = name
this.sayHi = function(){
console.log('Hi' +' '+ this.name)
}
}
var z= new person('zyn')
z.sayHi()//Hi zyn
以上,使用new關(guān)鍵字,通過對象(函數(shù)也是特殊對象)創(chuàng)建一個對象實例。
在基于類的語言中,屬性或字段通常都是在類中事先定義好了,但在Javascript中,在創(chuàng)建對象之后還可以為類添加字段。
var Animal =function(){}
var cat = new Animal();
cat.color = "green";
以上,color這個字段只屬于當前的cat實例。
對于后加的字段,如果想讓animal的所有實例都擁有呢?
使用prototype
var Animal =function(){}
Animal.prototype.color= "green";
var cat = new Animal();
var dog = new Animal();
console.log(cat.color);//green
console.log(dog.color);//green
通過Prototype不僅可以添加字段,還可以添加方法。
var Animal =function(){}
Animal.prototype.color= "green";
var cat = new Animal();
var dog = new Animal();
console.log(cat.color);//green
console.log(dog.color);//green
Animal.prototype.run= function(){
console.log("run")
}
dog.run();
原來通過prototype屬性,在創(chuàng)建對象之后還可以改變對象的行為。
比如,可以為數(shù)組這個特殊對象添加一個方法。
Array.prototype.remove = function(elem){//elem=2
var index = this.indexOf(elem);//index=1
if(index >= 0){
this.splice(index, 1);
}
}
var arr = [1, 2, 3] ;
arr.remove(2);
//原數(shù)組變?yōu)閍rr[1,3]
除了通過prototype為對象定義屬性或方法,還可以通過對象的構(gòu)造函數(shù)來定義類的屬性或方法。
var Animal = function(){
this.color = "green";
this.run = function(){
console.log("run");
}
}
var mouse = new Animal();
mouse.run();
以上做法的也可以讓所有的Animal實例共享所有的字段和方法。并且還有一個好處是可以在構(gòu)造函數(shù)中使用類的局部變量。
var Animal = function (){
var runAlready = false;
this.color = "green";
this.run = function(){
if(!runAlreadh){
console.log("start running");
} else {
console.log("already running")
}
}
}
其實,一個更加實際的做法是把通過構(gòu)造函數(shù)結(jié)合通過prototype定義一個類的字段和行為。
ar Animal = function (){
var runAlready = false;
this.color = "green";
this.run = function(){
if(!runAlready){
console.log("start running");
} else {
console.log("already running")
}
}
}
Animal.prototype.color = '';
Animal.prototype.hide = function(){
console.log("111");
}
var horse = new Animal();
horse.run(); //start running
horse.hide();//111
Prototype允許我們在創(chuàng)建對象之后來改變對象或類的行為,并且這些通過prototype屬性添加的字段或方法所有對象實例是共享的。
4.畫出如下代碼的原型圖
function People (name){
this.name = name;
this.sayName = function(){
console.log('my name is:' + this.name);
}
}
People.prototype.walk = function(){
console.log(this.name + ' is walking');
}
var p1 = new People('zyn');
var p2 = new People('JL');

5. 創(chuàng)建一個 Car 對象,擁有屬性name、color、status;擁有方法run,stop,getStatus
var Car = function(name,color,status){
this.name =name;
this.color =color;
this.status = status;
}
Car.prototype.run =function(){
console.log('running')
}
Car.prototype.stop =function(){
console.log('stop')
}
Car.prototype.getStatus =function(){
console.log(this.status)
}
var Ben = new Car ('Ben','black',120)

6.創(chuàng)建一個 GoTop 對象,當 new 一個 GotTop 對象則會在頁面上創(chuàng)建一個回到頂部的元素,點擊頁面滾動到頂部。

//主要代碼
var GoTop = function(ct,target){
this.ct = ct;
this.target = $('<div class="goTop">回到頂部</div>')
this.target.css({
position:'fixed',
right:'100px',
bottom:'50px',
display:'none',
padding:'8px',
cursor:'pointer',
border:'1px solid',
borderRadius:'4px'
})
}
GoTop.prototype.creatNode = function(){
this.ct.append(this.target);
}
GoTop.prototype.bindEvent = function(){
var _this = this;
var $window = $(window);
$window.on('scroll',function(){
var $top = $window.scrollTop()
if($top>100){
_this.target.css('display','block')
}else{
_this.target.css('display','none')
}
})
this.target.on('click',function(){
_this.ct.animate({
scrollTop : 0
})
})
}
var Gotop =new GoTop($('body'))
Gotop.creatNode();
Gotop.bindEvent();
效果
希望對給位朋友有所幫助~~
版權(quán)歸饑人谷--楠柒所有如有轉(zhuǎn)發(fā)請注明出處 謝謝~~