一、什么是对象?
万物皆是对象,对象就是现实世界中的一个个的实体,也就是说对象就是实例,是一个有具体的概念来的,简单来说,你不需要知道这个对象是怎么构造,你只需要会使用这个对象就可以了,而所谓当中的类就是具有相同特征对象的一个集合。
二、什么是面向对象?
在编程中,面向过程是函数的调用,是自由的,而面向对象就是也是函数的调用,只不过面向对象要实现多态、继承、封装,要自己构造函数,是有主的,面向对象有自己的属性、构造方法。例如系统的面向对象,new Date( )、new Array(),自定义对象,就是自己创建对象。
三、js是面向对象语言?
js是不是面向对象的语言,有人说是,也有人说不是,js是基于面向对象的,js这款语言很灵活,用来编写对象,有点让人难以适应。
var arr=[4,5,6];
arr.a=55;//属性
arr.sum=function(){ //方法
alert(this.a); //第二个含义:使用该方法的对象,弹出该对象
}
arr.sum();
四、面向对象的特性是什么,有什么作用?
面向对象的特性是:抽象、封装、继承
1、抽象:重点在于抽,将对象属性中最核心的部分抽出来使用。
2、封装:重点在于会使用即可,不需要考虑其内容是怎么构造的。
3、继承:就是能过继承父类的功能,也能够拥有自己的功能。
五、使用面向对象编写幻灯片和选项卡。
1、幻灯片demo
<script type="text/javascript">
window.onload=function(){
new slider('list','l','r');
}
function slider(list,l,r){
var _this=this;
this.olist=document.getElementById(list);
this.ali=this.olist.getElementsByTagName('li');
this.ol=document.getElementById(l);
this.or=document.getElementById(r);
this.now=0;
this.timer=null;
this.timer=setInterval(function(){
_this.fnnext();
},1000)
this.ol.onclick=function(){
_this.fnl();
}
this.or.onclick=function(){
_this.fnr();
}
}
slider.prototype.fnnext=function(){
var _this=this;
this.now++;
_this.fntab();
}
slider.prototype.fntab=function(){
var _this=this;
if(this.now<0){
this.now=this.ali.length-1;
}
if(this.now>this.ali.length-1){
this.now=0;
}
this.olist.style.transform='translate('+(-400*this.now)+'px)'
this.olist.style.transition='0.5s';
}
slider.prototype.fnl=function(){
var _this=this;
clearInterval(this.timer);
_this.fnnext();
}
slider.prototype.fnr=function(){
var _this=this;
clearInterval(this.timer);
this.now--;
_this.fntab();
}
</script>
2、选项卡demo
<script type="text/javascript">
window.onload=function(){
// function $(id){
new show('body','list');
}
function show(id,id1){
var _this=this;//this=window
this.obody=document.getElementById(id);
this.olist=document.getElementById(id1);
this.adiv=this.obody.getElementsByClassName('box');
this.ali=this.olist.getElementsByTagName('li');
// console.log(ali);
// console.log(adiv);
//
for(var i=0;i<this.ali.length;i++){
this.ali[i].index=i;
this.ali[i].onclick=function(){
_this.aclick(this)
}
}
}
show.prototype.aclick=function(obody){
// var _this=this;
for(var i=0;i<this.ali.length;i++){
this.ali[i].className='';
this.adiv[i].style.display='none';
}
obody.className='specail';
this.adiv[obody.index].style.display='block';
// console.log(adiv[this.index])
}
</script>
3、拖拽demo
<script type="text/javascript">
window.onload=function(){
new drag('box');
}
function drag(id){
var _this=this;
this.obox=document.getElementById(id);
this.obox.onmousedown=function(ev){
_this.fndown(ev)
}
}
drag.prototype.fndown=function(ev){
var _this=this;
var ev=ev||event;
this.disx=ev.clientX-this.obox.offsetLeft;
this.disy=ev.clientY-this.obox.offsetTop;
document.onmousemove=function(ev){
_this.fnmove(ev)
}
this.obox.onmouseup=fnup;
}
drag.prototype.fnmove=function(ev){
var _this=this;
var ev=ev||event;
this.obox.style.left=ev.clientX-this.disx+'px';
this.obox.style.top=ev.clientY-this.disy+'px';
}
function fnup(){
document.onmousemove=null;
}
</script>