一切皆對象之兩個方法概括js,無函數簽名(無多態),原型,閉包,封裝,參考型別,繼承……,js

來源:互聯網
上載者:User

一切皆對象之兩個方法概括js,無函數簽名(無多態),原型,閉包,封裝,參考型別,繼承……,js

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><script type="text/javascript">    var num = [88, 1151, 1];    (function() {        /**         * 對於n1,n2,n3;如果其中任意兩個分別乘以[1, 100]範圍內的任意兩個整數x,y的和加2或者減2等於         * 另外第三個數,找出x和y.         * @type {Number}         */        function MioFun () {            this.a = arguments[0][0];            this.b = arguments[0][1];            this.c = arguments[0][2];        }        MioFun.prototype = {            constructor : MioFun,            findMax: function() {                var temp = 0;                if(this.a < this.b) {                    temp = this.a;                    this.a = this.b;                    this.b = temp;                }                if(this.a < this.c) {                    temp = this.a;                    this.a = this.c;                    this.c = temp;                }                return this;            },            calMain: function() {                var i, j;                if(this.a < this.b + this.c) return null;                for(i = 1; i <= 100; i++) {                    for(j = 1; j <= 100; j++) {                        this.calFun(i, j);                    }                }            },            calFun: function(x, y) {                if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {                    console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));                    console.log("////////////////////////////////////////////////////////////////////////////");                }                        }        }        var mio = new MioFun(arguments[0]);        mio.findMax().calMain();            })(num);    var arg = [88, 1152, 102];    (function() {        /**         * 對於n1,n2,n3;如果其中任意兩個分別乘以[1, 100]範圍內的任意兩個整數x,y的和加2或者減2等於         * 另外第三個數,找出x和y.         * @type {Number}         */        function SuperFun () { //父類成員屬性            if(arguments[0]) {                this.a = arguments[0][0];                this.b = arguments[0][1];            }        }        SuperFun.prototype.findMax = function() { //父類成員方法findMax            var temp = 0;            if(this.a < this.b) {                temp = this.a;                this.a = this.b;                this.b = temp;            }            if(this.a < this.c) {                temp = this.a;                this.a = this.c;                this.c = temp;            }            return this;        };        SuperFun.prototype.calFun = function(x, y) { //父類成員方法calFun            if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {                console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));                console.log("////////////////////////////////////////////////////////////////////////////");            }                    };        function SubFun () {            SuperFun.call(this, arguments[0]); //繼承父類SuperFun屬性            this.c = arguments[0][2]; //子類偽私人成員屬性        }        SubFun.prototype = new SuperFun();  //繼承父類SuperFun方法        SubFun.prototype.constructor = SubFun;        SubFun.prototype.calMain =  function() { //子類偽私人成員方法            var i, j;            if(this.a < this.b + this.c) return null;            for(i = 1; i <= 100; i++) {                for(j = 1; j <= 100; j++) {                    this.calFun(i, j);                }            }        };               var mio = new SubFun(arguments[0]);        mio.findMax().calMain();            })(arg);</script></head><body></body></html>


javascript 閉包

這裡一般很少 有人解釋 的就算解釋也不是一下解釋清楚的
要學可以去編程論壇多學習。網站去大點人氣高的網站。
 
什是JavaScript裡的閉包?

1、何謂閉包

JavaScript例子:

function GISMapPanel() {
this.mapPanel = document.getElementById("mapDiv");
this.width;
this.height;

this.showMap = function() {
alert(this.mapPanel); //tip:[object]
this.mapPanel.onmousedown = this.mouseDownEventProcess();
}

this.mouseDownEventProcess = function( ){
var _this = this;
return function() {
alert(this.mapPanel); //tip:undefined,why;
_this.width = 400;
_this.height = 300;
//other codes
}
}
}

閉包簡單的解釋是,ECMAScript允許inner functions(嵌套函數):函數可以定義在另外一個函數裡面。這些內部的函數可以訪問outer function(父函數)的local變數,參數,其它內建函式。當內建函式被構造,並可以在函數外被獲得(函數當成傳回值),這個內建函式在 outer function返回後被執行(在outer函數外執行),那一個閉包形成了。

閉包特徵:A、作為一個函數變數的一個引用,當函數返回時,其處於啟用狀態;B、一個閉包就是當一個函數返回時,一個沒有釋放資源的棧區。

2、 閉包隱喻

function就像房子內的居室(也是房子),可以租給給甲居住,甲就是居室(function)的主人(this),也可以租給乙住,乙就成為居室(function)的主人(this),甲原來的一切則被甲帶走,乙用不到任何甲的東西。如果甲是房子(外層function)的主人,並把某居室(內層function)租給乙,乙具有居室內的一切,而乙也可以使用甲的衛生間、客廳(外層函數變數,如_this)。閉包變數就是外層房子內變數,包括居室(function)、衛生間、客廳(外層函數變數)。同JavaScript閉包,居室可以是一局、兩局、多局,當然一局一般比較好租,JavaScript閉包一般也是一局。

3、閉包作用

物件導向編程的成功和流行,使不支援面向的對象編程的語言也盡量往這個方向靠,如C語言可以通過struct及function指標,類比物件導向編程,以期達到物件導向的封裝、多態;JavaScript則通過基於對象編製以期達到封裝性。如上例所示,把地圖(div)相關的事件處理代碼、相關屬性、相關方法集中到GISMapPanel對象中。但由於事件發生的實體是地圖對應的div對象,事件函數調用時房子是由div對象居住的,也就是說this是div元素,因此,會出現alert(this.mapPanel); //tip:undefined,div元素沒有屬性mapPanel;如果想要訪問原GISMapPanel對象中的屬性,就要象例子所示,把GISMapPanel對象當作一個客廳來處理,從而可以......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.