js面向對象高級編程
面向對象的組成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; //對象下面的變量:叫作對象的屬性
-
- //alert( arr.number );
- //alert( arr.length );
-
- arr.test = function(){ //對象下面的函數 : 叫作對象的方法
- alert(123);
- };
-
- arr.test();
-
- arr.push();
- arr.sort();
-
- </script>
- </head>
-
- <body>
- </body>
- </html>
建立第一個面向對象程序jquery
- <!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>
工廠方式app
- <!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>
工廠方法之原型ui
- <!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>
面向對象的寫法this
- <!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>
面向對象的選項卡spa
- <!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>
面向對象的拖拽.net
包裝對象prototype
- <!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>
歡迎關注本站公眾號,獲取更多信息