無意間建立的一個閉包,以及解決辦法,建立解決辦法

來源:互聯網
上載者:User

無意間建立的一個閉包,以及解決辦法,建立解決辦法

閉包的本質是範圍鏈,我們在工作中經常無意間就會建立一個閉包,比如:

<input type="button" id="id1" value="1"></input><input type="button" id="id2" value="2"></input><input type="button" id="id3" value="3"></input><input type="button" id="id4" value="4"></input><input type="button" id="id5" value="5"></input><input type="button" id="id6" value="6"></input><input type="button" id="id7" value="7"></input><input type="button" id="id8" value="8"></input><input type="button" id="id9" value="9"></input><input type="button" id="id10" value="10"></input><script type="text/javascript">for (var i = 1; i <= 10; i++){   document.getElementById("id"+i).onclick = function ()     {        alert(i);    }}</script>

運行之後,會發現每個onclick時間執行時彈出的都是11!

這是因為,onclick 函數是在 全域範圍裡面被定義的,被定義的時候,會產生一個對象,這個對象繼承了當前執行環境的範圍鏈。也就是說,這個函數執行體裡的 i ,引用的是全域範圍裡的i。

因為 for 迴圈執行完以後,全域範圍下的 i 的值變為了 11,所以才會每個onclick函數都彈出11。

為瞭解決這個問題,我們需要為每個 onclick 函數的產生建立一個單獨的範圍,然後 onclick 函數彈出這個範圍裡面的 局部變數:

<input type="button" id="id1" value="1"></input><input type="button" id="id2" value="2"></input><input type="button" id="id3" value="3"></input><input type="button" id="id4" value="4"></input><input type="button" id="id5" value="5"></input><input type="button" id="id6" value="6"></input><input type="button" id="id7" value="7"></input><input type="button" id="id8" value="8"></input><input type="button" id="id9" value="9"></input><input type="button" id="id10" value="10"></input><script type="text/javascript">for (var i = 1; i <= 10; i++){    (function () {        var m;        m = i;        document.getElementById("id"+m).onclick = function ()         {            alert(m);        }    })();}</script>



怎證明一個集合A的閉包的閉包仍然是集合A的閉包

設A的閉包是B,A的閉包的閉包是C。用反證法,如果命題不成立,則B的某一個內點或聚點不是A的內點或聚點。顯然,B的每個元素都在A的閉包裡。所以只要證B的每個聚點都在A的閉包裡。假設B的某個聚點P不在A的閉包裡,則P是A的外點。所以P的某個鄰域U與A的交集是空集。而U內必定有B的元素。設Q為U與B的交集的某個元素。在Q的某個鄰域內沒有A的元素,即Q是A的外點。但既然Q是B的元素,則Q必定是A的內點或聚點。所以矛盾。原命題成立。
 
(離散數學)輸入一個關係矩陣,用C語言編程出它的自反閉包,對稱閉包與傳遞閉包

我用物件導向寫的 自己改一下

#include<iostream.h>
template<class T>
void Warshall( T *a , int m , int n )
{
int i = 0,j = 0;
for( i = 0 ; i < n ; i++ )
{
for( j = 0 ; j < m ; j++ )
{
if( a[j][i] == 1 )
{
int k = 0;
for( int x = 0 ; x < n ; x++ )
{
a[j][k] = a[j][k] || a[i][k];
k++;
}
}
}

}
for( i = 0 ; i < m ; i++ )
{
for( j = 0 ; j < n ; j++ )
{
cout << a[i][j] << '\t';
}
cout<<endl;
}
}
void main()
{
int ai[4][4] = { {0,1,0,0} , {1,0,1,0} , {0,0,0,1} , {0,0,0,0} };
Warshall(ai,4,4);
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.