js自訂方法名

來源:互聯網
上載者:User

標籤:

自訂方法名:

<script language="javascript" type="text/javascript">
window.onload = function(){ init( ); }

function init()
{
var TestStrA = "abc";
var TestStrB = "def";
var TestStrC = TestStrA + TestStrB;
alert(TestStrC);
}
</script>

init 為自訂的方法名,從字面理解一般用於對頁面變數初始化。你上面的代碼意思就是在當前網頁裝載完畢後執行初始化方法(當瀏覽器開啟某個網頁完畢後,會觸發window對象的 onload方法,以你上面的代碼就會執行 以 init 命名的初始化方法)。

其實下面這種寫法也是可以的,這樣你就更容易理解(也稱匿名方法,所謂的匿名方法就是沒有方法名的。):

<script language="javascript" type="text/javascript">
window.onload = function(){
var TestStrA = "abc";
var TestStrB = "def";
var TestStrC = TestStrA + TestStrB;
alert(TestStrC);
}
</script>


/**
* 方法一:初始化一個方法可以用閉包寫;
*/
//$(function(){
// window.onload = function(){
// init();
// }
//});

/**
* 方法二:初始化一個方法可以直接window.onload
*/
window.onload = function(){
init();
}
function init() {
var TestStrA = "abc";
var TestStrB = "def";
var TestStrC = TestStrA + TestStrB;
alert(TestStrC);
}

/**
* 不去初始化,直接window.onload = 一個函數;
*/
//window.onload = function(){
// var TestStrA = "abc";
// var TestStrB = "def";
// var TestStrC = TestStrA + TestStrB;
// alert(TestStrC);
//}
來源:http://zhidao.baidu.com/link?url=EbQrgACvZCrpqiZVFoiy25Ugxn-vFE8ix9NQjbG2delw3EpBo4G4pPAipz-lZjU2zAlJMm3EsINasMf1L55Cy_

js自訂方法名

聯繫我們

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