標籤:
自訂方法名:
<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自訂方法名