js面向对象高级编程
面向对象的组成
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- var arr = [];
- arr.number = 10; //对象下面的变量:叫做对象的属性
- //alert( arr.number );
- //alert( arr.length );
- arr.test = function(){ //对象下面的函数 : 叫做对象的方法
- alert(123);
- };
- arr.test();
- arr.push();
- arr.sort();
- </script>
- </head>
- <body>
- </body>
- </html>
创建第一个面向对象程序
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- //var obj = {};
- var obj = new Object(); //创建了一个空的对象
- obj.name = '小明'; //属性
- obj.showName = function(){ //方法
- alert(this.name);
- };
- obj.showName();
- var obj2 = new Object(); //创建了一个空的对象
- obj2.name = '小强'; //属性
- obj2.showName = function(){ //方法
- alert(this.name);
- };
- obj2.showName();
- </script>
- </head>
- <body>
- </body>
- </html>
工厂方式
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- //工厂方式 : 封装函数
- function createPerson(name){
- //1.原料
- var obj = new Object();
- //2.加工
- obj.name = name;
- obj.showName = function(){
- alert( this.name );
- };
- //3.出场
- return obj;
- }
- var p1 = createPerson('小明');
- p1.showName();
- var p2 = createPerson('小强');
- p2.showName();
- </script>
- </head>
- <body>
- </body>
- </html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- //当new去调用一个函数 : 这个时候函数中的this就是创建出来的对象,而且函数的的返回值直接就是this啦(隐式返回)
- //new后面调用的函数 : 叫做构造函数
- function CreatePerson(name){
- this.name = name;
- this.showName = function(){
- alert( this.name );
- };
- }
- var p1 = new CreatePerson('小明');
- //p1.showName();
- var p2 = new CreatePerson('小强');
- //p2.showName();
- alert( p1.showName == p2.showName ); //false
- var arr = new Array();
- var date = new Date();
- </script>
- </head>
- <body>
- </body>
- </html>
对象的引用
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- /*var a = [1,2,3];
- var b = [1,2,3];
- alert( a == b ); //false*/
- //var a = 5;
- //var b = a;
- //b += 3;
- ////alert(b); //8
- //alert(a); //5 基本类型 : 赋值的时候只是值的复制
- //var a = [1,2,3];
- //var b = a;
- //b.push(4);
- ////alert(b); //[1,2,3,4]
- //alert(a); //[1,2,3,4] 对象类型 : 赋值不仅是值的复制,而且也是引用的传递
- //var a = [1,2,3];
- //var b = a;
- //b = [1,2,3,4];
- ////alert(b); //[1,2,3,4]
- //alert(a); //[1,2,3]
- //var a = 5;
- //var b = 5;
- //
- //alert(a == b); //基本类型 : 值相同就可以
- //var a = [1,2,3];
- //var b = [1,2,3];
- //alert( a == b ); //false //对象类型 : 值和引用都相同才行
- var a = [1,2,3];
- var b = a;
- alert( a==b ); //true
- </script>
- </head>
- <body>
- </body>
- </html>
原型
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- //原型 : 去改写对象下面公用的方法或者属性 , 让公用的方法或者属性在内存中存在一份 ( 提高性能 )
- //原型 : CSS中的class
- //普通方法 : CSS中的style
- //原型 : prototype : 要写在构造函数的下面
- /*var arr = [1,2,3,4,5];
- var arr2 = [2,2,2,2,2];
- arr.sum = function(){
- var result = 0;
- for(var i=0;i<this.length;i++){
- result += this[i];
- }
- return result;
- };
- arr2.sum = function(){
- var result = 0;
- for(var i=0;i<this.length;i++){
- result += this[i];
- }
- return result;
- };
- //alert( arr.sum() ); //15
- //alert( arr2.sum() ); //10*/
- var arr = [1,2,3,4,5];
- var arr2 = [2,2,2,2,2];
- Array.prototype.sum = function(){
- var result = 0;
- for(var i=0;i<this.length;i++){
- result += this[i];
- }
- return result;
- };
- alert( arr.sum() ); //15
- alert( arr2.sum() ); //10
- </script>
- </head>
- <body>
- </body>
- </html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- var arr = [];
- //arr.number = 10;
- Array.prototype.number = 20;
- alert(arr.number);
- </script>
- </head>
- <body>
- </body>
- </html>
工厂方法之原型
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- //当new去调用一个函数 : 这个时候函数中的this就是创建出来的对象,而且函数的的返回值直接就是this啦(隐式返回)
- //new后面调用的函数 : 叫做构造函数
- function CreatePerson(name){
- this.name = name;
- }
- CreatePerson.prototype.showName = function(){
- alert( this.name );
- };
- var p1 = new CreatePerson('小明');
- //p1.showName();
- var p2 = new CreatePerson('小强');
- //p2.showName();
- alert( p1.showName == p2.showName ); //true
- var arr = new Array();
- var date = new Date();
- </script>
- </head>
- <body>
- </body>
- </html>
面向对象的写法
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script>
- function 构造函数(){
- this.属性
- }
- 构造函数.原型.方法 = function(){};
- var 对象1 = new 构造函数();
- 对象1.方法();
- </script>
- </head>
- <body>
- </body>
- </html>
面向对象的选项卡
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <style>
- #div1 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
- .active{ background:red;}
- </style>
- <script>
- /*window.onload = function(){
- var oParent = document.getElementById('div1');
- var aInput = oParent.getElementsByTagName('input');
- var aDiv = oParent.getElementsByTagName('div');
- for(var i=0;i<aInput.length;i++){
- aInput[i].index = i;
- aInput[i].onclick = function(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].className = '';
- aDiv[i].style.display = 'none';
- }
- this.className = 'active';
- aDiv[this.index].style.display = 'block';
- };
- }
- };*/
- //先变型:
- //尽量不要出现函数嵌套函数
- //可以有全局变量
- //把onload中不是赋值的语句放到单独函数中
- var oParent = null;
- var aInput = null;
- var aDiv = null;
- window.onload = function(){
- oParent = document.getElementById('div1');
- aInput = oParent.getElementsByTagName('input');
- aDiv = oParent.getElementsByTagName('div');
- init();
- };
- function init(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].index = i;
- aInput[i].onclick = change;
- }
- }
- function change(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].className = '';
- aDiv[i].style.display = 'none';
- }
- this.className = 'active';
- aDiv[this.index].style.display = 'block';
- }
- </script>
- </head>
- <body>
- <div id="div1">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- </body>
- </html>
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <style>
- #div1 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
- .active{ background:red;}
- </style>
- <script>
- /*window.onload = function(){
- var oParent = document.getElementById('div1');
- var aInput = oParent.getElementsByTagName('input');
- var aDiv = oParent.getElementsByTagName('div');
- for(var i=0;i<aInput.length;i++){
- aInput[i].index = i;
- aInput[i].onclick = function(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].className = '';
- aDiv[i].style.display = 'none';
- }
- this.className = 'active';
- aDiv[this.index].style.display = 'block';
- };
- }
- };*/
- //先变型:
- //尽量不要出现函数嵌套函数
- //可以有全局变量
- //把onload中不是赋值的语句放到单独函数中
- /*var oParent = null;
- var aInput = null;
- var aDiv = null;
- window.onload = function(){
- oParent = document.getElementById('div1');
- aInput = oParent.getElementsByTagName('input');
- aDiv = oParent.getElementsByTagName('div');
- init();
- };
- function init(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].index = i;
- aInput[i].onclick = change;
- }
- }
- function change(){
- for(var i=0;i<aInput.length;i++){
- aInput[i].className = '';
- aDiv[i].style.display = 'none';
- }
- this.className = 'active';
- aDiv[this.index].style.display = 'block';
- }*/
- //改成面向对象:
- //全局变量就是属性
- //函数就是方法
- //Onload中创建对象
- //改this指向问题 : 事件或者是定时器,尽量让面向对象中的this指向对象
- window.onload = function(){
- var t1 = new Tab();
- t1.init();
- };
- function Tab(){
- this.oParent = document.getElementById('div1');
- this.aInput = this.oParent.getElementsByTagName('input');
- this.aDiv = this.oParent.getElementsByTagName('div');
- }
- Tab.prototype.init = function(){
- var This = this;
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].index = i;
- this.aInput[i].onclick = function(){
- This.change(this);
- };
- }
- };
- Tab.prototype.change = function(obj){
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].className = '';
- this.aDiv[i].style.display = 'none';
- }
- obj.className = 'active';
- this.aDiv[obj.index].style.display = 'block';
- };
- </script>
- </head>
- <body>
- <div id="div1">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- </body>
- </html>
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <style>
- #div1 div,#div2 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
- .active{ background:red;}
- </style>
- <script>
- /*var arr = [4,7,1,3];
- arr.sort(); // 1 3 4 7
- var arr2 = [4,7,1,3];
- arr2.push(5);
- arr2.sort(); // 1 3 4 5 7
- */
- window.onload = function(){
- var t1 = new Tab('div1');
- t1.init();
- t1.autoPlay();
- var t2 = new Tab('div2');
- t2.init();
- t2.autoPlay();
- };
- function Tab(id){
- this.oParent = document.getElementById(id);
- this.aInput = this.oParent.getElementsByTagName('input');
- this.aDiv = this.oParent.getElementsByTagName('div');
- this.iNow = 0;
- }
- Tab.prototype.init = function(){
- var This = this;
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].index = i;
- this.aInput[i].onclick = function(){
- This.change(this);
- };
- }
- };
- Tab.prototype.change = function(obj){
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].className = '';
- this.aDiv[i].style.display = 'none';
- }
- obj.className = 'active';
- this.aDiv[obj.index].style.display = 'block';
- };
- Tab.prototype.autoPlay = function(){
- var This = this;
- setInterval(function(){
- if(This.iNow == This.aInput.length-1){
- This.iNow = 0;
- }
- else{
- This.iNow++;
- }
- for(var i=0;i<This.aInput.length;i++){
- This.aInput[i].className = '';
- This.aDiv[i].style.display = 'none';
- }
- This.aInput[This.iNow].className = 'active';
- This.aDiv[This.iNow].style.display = 'block';
- },2000);
- };
- </script>
- </head>
- <body>
- <div id="div1">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- <div id="div2">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- </body>
- </html>
面向对象的拖拽
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <style>
- #div1 div,#div2 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
- .active{ background:red;}
- </style>
- <script>
- /*var arr = [4,7,1,3];
- arr.sort(); // 1 3 4 7
- var arr2 = [4,7,1,3];
- arr2.push(5);
- arr2.sort(); // 1 3 4 5 7
- */
- window.onload = function(){
- var t1 = new Tab('div1');
- t1.init();
- t1.autoPlay();
- var t2 = new Tab('div2');
- t2.init();
- t2.autoPlay();
- };
- function Tab(id){
- this.oParent = document.getElementById(id);
- this.aInput = this.oParent.getElementsByTagName('input');
- this.aDiv = this.oParent.getElementsByTagName('div');
- this.iNow = 0;
- }
- Tab.prototype.init = function(){
- var This = this;
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].index = i;
- this.aInput[i].onclick = function(){
- This.change(this);
- };
- }
- };
- Tab.prototype.change = function(obj){
- for(var i=0;i<this.aInput.length;i++){
- this.aInput[i].className = '';
- this.aDiv[i].style.display = 'none';
- }
- obj.className = 'active';
- this.aDiv[obj.index].style.display = 'block';
- };
- Tab.prototype.autoPlay = function(){
- var This = this;
- setInterval(function(){
- if(This.iNow == This.aInput.length-1){
- This.iNow = 0;
- }
- else{
- This.iNow++;
- }
- for(var i=0;i<This.aInput.length;i++){
- This.aInput[i].className = '';
- This.aDiv[i].style.display = 'none';
- }
- This.aInput[This.iNow].className = 'active';
- This.aDiv[This.iNow].style.display = 'block';
- },2000);
- };
- </script>
- </head>
- <body>
- <div id="div1">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- <div id="div2">
- <input class="active" type="button" value="1">
- <input type="button" value="2">
- <input type="button" value="3">
- <div style="display:block">11111</div>
- <div>22222</div>
- <div>33333</div>
- </div>
- </body>
- </html>
包装对象
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- /*function Aaa(){
- this.name = '小明';
- }
- Aaa.prototype.showName = function(){
- alert( this.name );
- };
- var a1 = new Aaa();
- a1.showName();
- var arr = new Array();
- arr.push();
- arr.sort();
- //在JS源码 : 系统对象也是基于原型的程序
- function Array(){
- this.lenglth = 0;
- }
- Array.prototype.push = function(){};
- Array.prototype.sort = function(){};*/
- //尽量不要去修改或者添加系统对象下面的方法和属性
- var arr = [1,2,3];
- Array.prototype.push = function(){
- //this : 1,2,3
- //arguments : 4,5,6
- for(var i=0;i<arguments.length;i++){
- this[this.length] = arguments[i]
- }
- return this.length;
- };
- arr.push(4,5,6);
- alert( arr );
- //pop shift unshift splice sort
- </script>
- </head>
- <body>
- </body>
- </html>
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- /*var str = 'hello';
- alert( typeof str );
- str.charAt(0);
- str.indexOf('e');*/
- //null undefined
- //包装对象 : 基本类型都有自己对应的包装对象 : String Number Boolean
- /*var str = new String('hello');
- //alert( typeof str );
- alert(str.charAt(1));
- String.prototype.charAt = function(){};*/
- //var str = 'hello';
- //str.charAt(0); //基本类型会找到对应的包装对象类型,然后包装对象把所有的属性和方法给了基本类型,然后包装对象消失
- /*var str = 'hello';
- String.prototype.lastValue = function(){
- return this.charAt(this.length-1);
- };
- alert( str.lastValue() ); //o*/
- var str = 'hello';
- str.number = 10;
- alert( str.number ); //undefined
- </script>
- </head>
- <body>
- </body>
- </html>
原型链
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //原型链 : 实例对象与原型之间的连接,叫做原型链
- //原型链的最外层 : Object.prototype
- function Aaa(){
- //this.num = 20;
- }
- //Aaa.prototype.num = 10;
- Object.prototype.num = 30;
- var a1 = new Aaa();
- alert(a1.num);
- </script>
- </head>
- <body>
- </body>
- </html>
hasOwnProperty
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //hasOwnProperty : 看是不是对象自身下面的属性
- var arr = [];
- arr.num = 10;
- Array.prototype.num2 = 20;
- //alert( arr.hasOwnProperty('num') ); //true
- alert( arr.hasOwnProperty('num2') ); //false
- </script>
- </head>
- <body>
- </body>
- </html>
constructor
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //constructor : 查看对象的构造函数
- /*function Aaa(){
- }
- var a1 = new Aaa();
- alert( a1.constructor ); //Aaa
- var arr = [];
- alert( arr.constructor == Array ); //true*/
- /*function Aaa(){
- }
- //Aaa.prototype.constructor = Aaa; //每一个函数都会有的,都是自动生成的
- //Aaa.prototype.constructor = Array;
- var a1 = new Aaa();
- alert( a1.hasOwnProperty == Object.prototype.hasOwnProperty ); //true*/
- /*function Aaa(){
- }
- Aaa.prototype.name = '小明';
- Aaa.prototype.age = 20;
- Aaa.prototype = {
- constructor : Aaa,
- name : '小明',
- age : 20
- };
- var a1 = new Aaa();
- alert( a1.constructor );*/
- function Aaa(){
- }
- Aaa.prototype.name = 10;
- Aaa.prototype.constructor = Aaa;
- for( var attr in Aaa.prototype ){
- alert(attr);
- }
- </script>
- </head>
- <body>
- </body>
- </html>
instanceof
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //instanceof : 对象与构造函数在原型链上是否有关系
- function Aaa(){
- }
- var a1 = new Aaa();
- //alert( a1 instanceof Object ); //true
- var arr = [];
- alert( arr instanceof Array );
- </script>
- </head>
- <body>
- </body>
- </html>
toString
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //toString() : 系统对象下面都是自带的 , 自己写的对象都是通过原型链找object下面的
- /*var arr = [];
- alert( arr.toString == Object.prototype.toString ); //false*/
- /*function Aaa(){
- }
- var a1 = new Aaa();
- alert( a1.toString == Object.prototype.toString ); //true*/
- //toString() : 把对象转成字符串
- /*var arr = [1,2,3];
- Array.prototype.toString = function(){
- return this.join('+');
- };
- alert( arr.toString() ); //'1,2,3'*/
- //var num = 255;
- //alert( num.toString(16) ); //'ff'
- //利用toString做类型的判断 :
- /*var arr = [];
- alert( Object.prototype.toString.call(arr) == '[object Array]' ); */ //'[object Array]'
- window.onload = function(){
- var oF = document.createElement('iframe');
- document.body.appendChild( oF );
- var ifArray = window.frames[0].Array;
- var arr = new ifArray();
- //alert( arr.constructor == Array ); //false
- //alert( arr instanceof Array ); //false
- alert( Object.prototype.toString.call(arr) == '[object Array]' ); //true
- };
- </script>
- </head>
- <body>
- </body>
- </html>
继承
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //继承 : 子类不影响父类,子类可以继承父类的一些功能 ( 代码复用 )
- //属性的继承 : 调用父类的构造函数 call
- //方法的继承 : for in : 拷贝继承 (jquery也是采用拷贝继承extend)
- function CreatePerson(name,sex){ //父类
- this.name = name;
- this.sex = sex;
- }
- CreatePerson.prototype.showName = function(){
- alert( this.name );
- };
- var p1 = new CreatePerson('小明','男');
- //p1.showName();
- function CreateStar(name,sex,job){ //子类
- CreatePerson.call(this,name,sex);
- this.job = job;
- }
- //CreateStar.prototype = CreatePerson.prototype;
- extend( CreateStar.prototype , CreatePerson.prototype );
- CreateStar.prototype.showJob = function(){
- };
- var p2 = new CreateStar('黄晓明','男','演员');
- p2.showName();
- function extend(obj1,obj2){
- for(var attr in obj2){
- obj1[attr] = obj2[attr];
- }
- }
- </script>
- </head>
- <body>
- </body>
- </html>
对象的复制
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- /*var a = {
- name : '小明'
- };
- var b = a;
- b.name = '小强';
- alert( a.name );*/
- /*var a = {
- name : '小明'
- };
- //var b = a;
- var b = {};
- extend( b , a );
- b.name = '小强';
- alert( a.name );
- function extend(obj1,obj2){
- for(var attr in obj2){
- obj1[attr] = obj2[attr];
- }
- }*/
- var a = [1,2,3];
- var b = a;
- //b.push(4);
- b = [1,2,3,4];
- alert(a);
- </script>
- </head>
- <body>
- </body>
- </html>
继承的拖拽
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <style>
- #div1{ width:100px; height:100px; background:red; position:absolute;}
- #div2{ width:100px; height:100px; background:yellow; position:absolute; left:100px;}
- </style>
- <script>
- window.onload = function(){
- var d1 = new Drag('div1');
- d1.init();
- var d2 = new ChildDrag('div2');
- d2.init();
- };
- function Drag(id){ //父类
- this.obj = document.getElementById(id);
- this.disX = 0;
- this.disY = 0;
- }
- Drag.prototype.init = function(){
- var This = this;
- this.obj.onmousedown = function(ev){
- var ev = ev || window.event;
- This.fnDown(ev);
- document.onmousemove = function(ev){
- var ev = ev || window.event;
- This.fnMove(ev);
- };
- document.onmouseup = function(){
- This.fnUp();
- };
- return false;
- };
- };
- Drag.prototype.fnDown = function(ev){
- this.disX = ev.clientX - this.obj.offsetLeft;
- this.disY = ev.clientY - this.obj.offsetTop;
- };
- Drag.prototype.fnMove = function(ev){
- this.obj.style.left = ev.clientX - this.disX + 'px';
- this.obj.style.top = ev.clientY - this.disY + 'px';
- };
- Drag.prototype.fnUp = function(){
- document.onmousemove = null;
- document.onmouseup = null;
- };
- function ChildDrag(id){ //子类
- Drag.call(this,id);
- }
- extend( ChildDrag.prototype , Drag.prototype );
- ChildDrag.prototype.fnMove = function(ev){
- var L = ev.clientX - this.disX;
- var T = ev.clientY - this.disY;
- if(L<0){
- L = 0;
- }
- else if(L>document.documentElement.clientWidth - this.obj.offsetWidth){
- L = document.documentElement.clientWidth - this.obj.offsetWidth;
- }
- this.obj.style.left = L + 'px';
- this.obj.style.top = T + 'px';
- };
- function extend(obj1,obj2){
- for(var attr in obj2){
- obj1[attr] = obj2[attr];
- }
- }
- </script>
- </head>
- <body>
- <div id="div1"></div>
- <div id="div2"></div>
- </body>
- </html>
类式继承
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- //类 : JS是没有类的概念的 , 把JS中的构造函数看做的类
- //要做属性和方法继承的时候,要分开继承
- function Aaa(){ //父类
- this.name = [1,2,3];
- }
- Aaa.prototype.showName = function(){
- alert( this.name );
- };
- function Bbb(){ //子类
- Aaa.call(this);
- }
- var F = function(){};
- F.prototype = Aaa.prototype;
- Bbb.prototype = new F();
- Bbb.prototype.constructor = Bbb; //修正指向问题
- var b1 = new Bbb();
- //b1.showName();
- //alert( b1.name );
- //alert( b1.constructor );
- b1.name.push(4);
- var b2 = new Bbb();
- alert( b2.name );
- </script>
- </head>
- <body>
- </body>
- </html>
原型继承
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>无标题文档</title>
- <script>
- var a = {
- name : '小明'
- };
- var b = cloneObj(a);
- b.name = '小强';
- //alert( b.name );
- alert( a.name );
- function cloneObj(obj){
- var F = function(){};
- F.prototype = obj;
- return new F();
- }
- 拷贝继承: 通用型的 有new或无new的时候都可以
- 类式继承: new构造函数
- 原型继承: 无new的对象
- </script>
- </head>
- <body>
- </body>
- </html>
js面向对象高级编程的更多相关文章
- C++面向对象高级编程(九)Reference与重载operator new和operator delete
摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一 Reference 引用:之前提及过,他的主要作用就是取别名,与指针很相似,实现也是基于指针. 1.引用必须有初值,且不能引用nul ...
- C++面向对象高级编程(八)模板
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...
- C++面向对象高级编程(七)point-like classes和function-like classes
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.pointer-like class 类设计成指针那样,可以当做指针来用,指针有两个常用操作符(*和->),所以我们必须重载这两个操作 ...
- C++面向对象高级编程(六)转换函数与non-explicit one argument ctor
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.conversion function 转换函数 //1.转换函数 //conversion function //只要你认为合理 你可以任 ...
- C++面向对象高级编程(五)类与类之间的关系
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming OOP面向对象编 ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
- C++面向对象高级编程(三)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...
- C++面向对象高级编程(二)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 知识点1.重载成员函数 知识点2 . return by value, return by reference 知识点3 重载非成员函数 ...
- C++面向对象高级编程(一)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要: 知识点1 构造函数与析构函数 知识点2 参数与返回值 知识点3 const 知识点4 函数重载(要与重写区分开) 知识点5 友元 先以C ...
随机推荐
- 解决eclipse中Tomcat服务器的server location选项不能修改的问题
在Eclipse菜单栏中选择window — show view — server 可以看到服务的面板,服务面板中可看到已配置的Tomcat以及Tomcat下的项目 双击tomca进入设置界面,如果看 ...
- mysql 创建 mb4 字符集数据库
create database sina default character set utf8mb4 collate utf8mb4_unicode_ci; show variables like ' ...
- Opencv——相机标定
相机标定的目的:获取摄像机的内参和外参矩阵(同时也会得到每一幅标定图像的选择和平移矩阵),内参和外参系数可以对之后相机拍摄的图像就进行矫正,得到畸变相对很小的图像. 相机标定的输入:标定图像上所有内角 ...
- android的学习网站
1,Android系统简介2,ProGuard代码混淆3,讲讲Handler+Looper+MessageQueue关系4,Android图片加载库理解5,谈谈Android运行时权限理解6,Even ...
- Spark笔记-DataSet,DataFrame
DataSet:面向对象的,从JVM进行构建,或从其它格式进行转化 DataFrame:面向SQL查询,从多种数据源进行构建,或从其它格式进行转化 RDD DataSet DataFrame互转 1. ...
- FileShare枚举的使用(文件读写锁) - (转载)
开发过程中,我们往往需要大量与文件交互,但往往会出现很多令人措手不及的意外,所以对普通的C#文件操作做了一次总结,问题大部分如下: 写入一些内容到某个文件中,在另一个进程/线程/后续操作中要读取文件内 ...
- wpf 状态栏图标背景闪烁提醒 FlashWindowEx
原文:wpf 状态栏图标背景闪烁提醒 FlashWindowEx using System; using System.Runtime.InteropServices; using System.Wi ...
- Luogu P1966 火柴排队
这还是一道比较简单的题目,稍微想一下就可以解决.终于有NOIP难度的题目了 首先我们看那个∑(ai-bi)^2的式子,发现这个的最小值就是排序不等式 所以我们只需要改变第一组火柴的顺序,使它和第二组火 ...
- [Oracle]如何获得出现故障时,客户端的详细连接信息
[Oracle]如何获得出现故障时,客户端的详细连接信息 客户坚持说 只是在 每天早上5点才运行下面的语句: select / * + FULL (TAB001_TT01) * / 'TAB001_T ...
- OLED小记
1.点阵组成OLED,OLED中有一个GRAM区域,区域中的值直接刷新到屏幕上,对应关系是1bit对应一个像素点: 2.要点亮一个像素点,只需要将GRAM中的对应bit位写1即可.GRAM中是分页来管 ...