一、問答
(一 )、OOP 指什么?有哪些特性
指的是面向?qū)ο缶幊蹋ㄈQ是Object Oriented Programming,OOP),它是一種計算機(jī)編程架構(gòu)。面向?qū)ο蟪绦蛟O(shè)計可以看作一種在程序中包含各種獨(dú)立而又互相調(diào)用的對象的思想。它具有如下特性:
1、封裝性;
2、繼承性;
3、多態(tài)性;
4、抽象性;
(二 )、如何通過構(gòu)造函數(shù)的方式創(chuàng)建一個擁有屬性和方法的對象?
可通過如下形式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>trigger</title>
</head>
<body>
<button class="btn">trigger</button>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// $(".btn").on("click",function(){
// var i=0;
// i++;
// console.log(i)
// })
// setInterval (function(){
// $(".btn").trigger("click")
// },2000)
function People(name,age){
this.name=name;
this.age=age;
console.log("我是: "+this.name+",我的年齡是:"+this.age)
}
People.prototype.sayName=function(){
console.log("hello")
}
var p1=new People("licai",24),
p2=new People("zhangsan",20);
</script>
</body>
</html>

(三 )、prototype 是什么?有什么特性?
每個函數(shù)都有一個prototype屬性,prototype里默認(rèn)有兩個屬性一個是constructor,另一個是__proto__ (不同瀏覽器命名可能不同),constructor其實(shí)指向該函數(shù)本身,而__proto__ 則指向window下的Object.prototype,也就是說該函數(shù)的創(chuàng)建者是Object People.prototype instanceof Object運(yùn)行結(jié)果是ture,即People.prototype是Object的實(shí)例。
特性:通過構(gòu)造函數(shù)方式新建一個對象時,新建的對象會從prototype屬性上繼承屬性和方法。舉個例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>trigger</title>
</head>
<body>
<button class="btn">trigger</button>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function People(name,age){
this.name=name;
this.age=age;
console.log("我是: "+this.name+",我的年齡是:"+this.age)
}
People.prototype.sayName=function(){
console.log("hello,my name is:"+this.name)
}
People.prototype.hobby="code"
var p1=new People("licai",24),
p2=new People("zhangsan",20);
</script>
</body>
</html>

我們并沒有在p1中定義sayName方法及hobby屬性而是在People.prototype上定義了,但是卻可以在p1上運(yùn)行sayName()及hobby屬性,其實(shí)是p1繼承了People.prototype的sayName方法及hobby屬性。
(四)、畫出如下代碼的原型圖
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('饑人谷');
var p2 = new People('前端');

(五 )、以下代碼中的變量age有什么區(qū)別
function People (){
var age = 1 // 在函數(shù)里定義個一個局部變量age
this.age = 10; //將10賦給相應(yīng)對象的age,
}
People.age = 20; //給函數(shù)綁定一個值為20的age屬性
People.prototype.age = 30;// 在函數(shù)的原型上綁定一個值為30的age屬性

二、代碼
(一 )、創(chuàng)建一個 Car 對象,擁有屬性name、color、status;擁有方法run,stop,getStatus ;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Car(name,color,status) {
this.name=name;
this.color=color;
this.status=status;
}
Car.prototype.run=function () {
this.status='running';
console.log('The car is running!')
};
Car.prototype.stop=function () {
this.status='stopping';
console.log('The car is stopping!')
};
Car.prototype.getStatus=function () {
console.log('The car is '+this.status+'!')
};
var car1=new Car('baoma','black','stopping')
</script>
</body>
</html>

在線預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu35/renwu35-1.html
(二 )、創(chuàng)建一個 GoTop 對象,當(dāng) new 一個 GotTop 對象則會在頁面上創(chuàng)建一個回到頂部的元素,點(diǎn)擊頁面滾動到頂部。擁有以下屬性和方法
ct屬性,GoTop 對應(yīng)的 DOM 元素的容器
target屬性, GoTop 對應(yīng)的 DOM 元素
bindEvent 方法, 用于綁定事件
createNode 方法, 用于在容器內(nèi)創(chuàng)建節(jié)點(diǎn)
方法一、代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div.goToTop{
position: fixed;
top: 560px;
left: 1200px;
border: 1px solid red;
border-radius: 3px;
padding: 10px 20px;
cursor: pointer;
display: none;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<div class="ct">
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
</div>
<script>
// function goTop(ct) {
// this.ct=ct;
// this.target=$('<div class="goToTop">回到頂部</div>');
// var goTopCt=this.ct,
// goTopTr=this.target;
//
// function canShow() {
// var windowH=$(window).height(),
// scrollH=$(window).scrollTop();
// if (scrollH>windowH){
// return true;
// }
// else {return false}
// }
// this.createNode=(function (){
// $(goTopCt).append(goTopTr)
// }());
// this.bindEvent=(function () {
// $(window).on("scroll",function () {
// if (canShow()){
// goTopTr.show()
// }
// else {goTopTr.hide()}
// }) ;
// goTopTr.on('click',function () {
// $(window).scrollTop(0)
// });
// }());
// }
function goTop(ct) {
this.ct=ct;
this.target=$('<div class="goToTop">回到頂部</div>');
var goTopCt=this.ct,
goTopTr=this.target;
function canShow() {
var windowH=$(window).height(),
scrollH=$(window).scrollTop();
if (scrollH>windowH){
return true;
}
else {return false}
}
this.createNode=function (){
$(goTopCt).append(goTopTr)
};
this.createNode();
this.bindEvent=function () {
$(window).on("scroll",function () {
if (canShow()){
goTopTr.show()
}
else {goTopTr.hide()}
}) ;
goTopTr.on('click',function () {
$(window).scrollTop(0)
// 或者使用動畫效果慢慢往上升$('html,body').animate({"scrollTop":"0px"},800)
});
};
this.bindEvent();
}
var GoTop1= new goTop('.ct');
</script>
</body>
</html>
在線預(yù)覽地址 https://github.com/have-not-BUG/task/blob/master/renwu/renwu35/renwu35-2A.html
方法二、
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div.goToTop{
position: fixed;
top: 560px;
left: 1200px;
border: 1px solid red;
border-radius: 3px;
padding: 10px 20px;
cursor: pointer;
display: none;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<div class="ct">
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
</div>
<script>
function goTop(ct) {
this.ct=ct;
this.target=$('<div class="goToTop">回到頂部</div>');
goTopCt=this.ct;
goTopTr=this.target;
this.createNode();
this.bindEvent();
}
goTop.prototype={
bindEvent:function () {
$(window).on("scroll",function () {
function canShow() {
var windowH=$(window).height(),
scrollH=$(window).scrollTop();
if (scrollH>windowH){
return true;
}
else {return false}
}
if (canShow()){
goTopTr.show()
}
else {goTopTr.hide()}
}) ;
goTopTr.on('click',function () {
$('html,body').animate({"scrollTop":"0px"},800)
});
},
createNode:function (){
$(goTopCt).append(goTopTr)
}
};
var GoTop1= new goTop('.ct');
</script>
</body>
</html>
在線預(yù)覽地址:
https://github.com/have-not-BUG/task/blob/master/renwu/renwu35/renwu35-2B.html
(三 )、使用構(gòu)造函數(shù)創(chuàng)建對象的方式完成輪播功能( 查看demo ),使用如下調(diào)用方式
function Carousel($node){
//todo...
}
Carousel.prototype = {
//todo ..
};
var $node1 = $('.ct').eq(0);
var $node2 = $('.ct').eq(1);
var carousel1 = new Carousel($node1);
var carousel2 = new Carousel($node2);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<style>
ul,li,body,html{
margin: 0;
padding: 0;
list-style: none;
}
.img-ct>li{
float: left;
width: 310px;
}
.img-ct{
position: absolute;
}
.clearfix:after{
content: "";
display: block;
clear: both;
}
.lunbo{
position: relative;
width: 310px;
height: 206px;
overflow: hidden;
}
a{
text-decoration: none;
color: #fff;
}
.arrow{
width: 20px;
padding: 10px;
background-color: #4E443C;
border-radius: 20px;
text-align: center;
font-weight: bolder;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.pre{
left: 10px;
}
.next{
right: 10px;
}
</style>
</head>
<body>
<div id="c1" class="lunbo">
<ul class="img-ct clearfix">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
<a class="pre arrow" href="#"><</a>
<a class="next arrow" href="#">></a>
</div>
<div id="c2" class="lunbo">
<ul class="img-ct">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
<a class="pre arrow" href="#"><</a>
<a class="next arrow" href="#">></a>
</div>
<script>
function Carousel($node){
this.$node=$node;
var $ct=this.$ct=$node.find(".img-ct");
var $liImg=$ct.children();
this.imgNum=$liImg.length;
this.imgWidth=$ct.children("li").width();
$ct.width(this.imgWidth*this.imgNum);
this.$next=$node.find(".next");
this.$pre=$node.find(".pre");
this.showMe();
}
Carousel.prototype={
showMe: function () {
var _this=this;
console.log(this);
this.$pre.on('click',function (e) {
e.preventDefault();
_this.playPre();
});
this.$next.on('click',function (e) {
e.preventDefault();
_this.playNext();
});
},
playNext: function (){
var $ct=this.$ct;
$ct.animate({"left":0-this.imgWidth},600,function () {
$ct.append($ct.children().first());
$ct.css("left",0);
});
},
playPre: function () {
var $ct=this.$ct;
$ct.prepend($ct.children().last());
$ct.css("left",0-this.imgWidth);
$ct.animate({"left":0},600);
}
};
// 新建對象注意應(yīng)該寫在后面,不能提前寫!
var c1 = new Carousel($('#c1'));
var c2 = new Carousel($('#c2'));
// 或者使用遍歷的方法
// $(".lunbo").each(function () {
// new Carousel($(this))
//
// })
</script>
<!--<script>-->
<!--var $liImg=$('.img-ct').children(),-->
<!--imgNum=$liImg.length;-->
<!--$('.img-ct').append($liImg.first().clone());-->
<!--$('.img-ct').prepend($liImg.last().clone());-->
<!--var trueImgNum=$('.img-ct').children().size(),-->
<!--imgWidth=$('.img-ct li').width();-->
<!--$('.img-ct').width(imgWidth*trueImgNum);-->
<!--$('.img-ct').css({"left":0-imgWidth});-->
<!--var i=1,-->
<!--clock=false;-->
<!--$(".next").on('click',function () {-->
<!--if (clock){return}-->
<!--clock=true;-->
<!--playNext();-->
<!--setTimeout(function () {-->
<!--clock=false;-->
<!--},600)-->
<!--});-->
<!--function playNext() {-->
<!--if (i===imgNum+1){-->
<!--$('.img-ct').css("left",0-imgWidth);-->
<!--i=1;-->
<!--}-->
<!--$('.img-ct').animate({"left":0-imgWidth*(i+1)},600);-->
<!--i=i+1;-->
<!--console.log("我是playNext"+i);-->
<!--}-->
<!--function playPre() {-->
<!--i = i - 1;-->
<!--if (i === -1) {-->
<!--$('.img-ct').css("left", 0 - imgWidth * imgNum);-->
<!--i = imgNum - 1;-->
<!--}-->
<!--$('.img-ct').animate({"left":0-imgWidth*i},600);-->
<!--console.log("我是playPre"+i);-->
<!--}-->
<!--$(".pre").on('click',function () {-->
<!--if (clock){return}-->
<!--clock=true;-->
<!--playPre();-->
<!--setTimeout(function () {-->
<!--clock=false;-->
<!--},600)-->
<!--});-->
<!--</script>-->
</body>
</html>
在線預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu35/renwu35-3.html
(四 )、使用構(gòu)造函數(shù)創(chuàng)建對象的方式實(shí)現(xiàn) Tab 切換功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>測試</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul,li{
display: inline-block;
vertical-align: middle;
font-size: 0;
}
li{
list-style: none;
border: 1px solid #1a1a1a;
font-size: 20px;
padding: 10px 10px;
width: 200px;
text-align: center;
cursor: pointer;
}
.down{
border: 1px solid #1a1a1a;
width: 600px;
height: 200px;
}
.content{
display: none;
}
.down>.active{
display: block;
}
.ctTab>.active{
background-color: #ccc;
}
</style>
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div id="wrap1">
<ul class="ctTab">
<li class="tab active">tab1</li>
<li class="tab">tab2</li>
<li class="tab">tab3</li>
</ul>
<div class="down">
<div class="content active">內(nèi)容1</div>
<div class="content">內(nèi)容2</div>
<div class="content">內(nèi)容3</div>
</div>
</div>
<div id="wrap2">
<ul class="ctTab">
<li class="tab active">tab1</li>
<li class="tab">tab2</li>
<li class="tab">tab3</li>
</ul>
<div class="down">
<div class="content active">內(nèi)容1</div>
<div class="content">內(nèi)容2</div>
<div class="content">內(nèi)容3</div>
</div>
</div>
<script>
// 總體思路:點(diǎn)擊處 獲取index 鄰居刪除active 自己加上active
// 詳細(xì)思路
// $(".ctTab li").on("click",function () {
// var $this=$(this);
// var i=$(".ctTab li").index($this);
// $(".ctTab").children().eq(i).siblings().removeClass("active");
// $(".ctTab").children().eq(i).addClass("active");
// $(".down").children().eq(i).siblings().removeClass("active");
// $(".down").children().eq(i).addClass("active");
// })
function changTab($node) {
this.$node=$($node);
this.$navLi=$($node).find(".ctTab li");
this.$ctTab=$($node).find(".ctTab");
this.$down=$($node).find(".down");
this.bind();
}
// 下面這樣也可以
// function changTab($node) {
// this.$node=$($node);
// this.$navLi=this.$node.find(".ctTab li");
// this.$ctTab=this.$node.find(".ctTab");
// this.$down=this.$node.find(".down");
// this.bind();
// }
changTab.prototype={
bind:function () {
var _this=this;
this.$navLi.on("click",function () {
var $this=$(this);
var i=_this.$navLi.index($this);
_this.$ctTab.children().eq(i).siblings().removeClass("active");
_this.$ctTab.children().eq(i).addClass("active");
_this.$down.children().eq(i).siblings().removeClass("active");
_this.$down.children().eq(i).addClass("active");
})
}
};
var changtab1=new changTab("#wrap1");
var changtab2=new changTab("#wrap2");
</script>
</body>
</html>
在線預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu35/renwu35-4.html
**本文版權(quán)歸本人即簡書筆名:該賬戶已被查封 所有,如需轉(zhuǎn)載請注明出處。謝謝! *