淺談javascript對象模型和function對象
javascript中,函數(shù)就是對象
<html>
<head>
<script type="text/javascript">
function add(number){
alert(number+20);
}
var add=function(number){
alert(number+20);
}
function add(number,number1){
alert(number+30);
}
var add=function(number){
alert(number+90);
}
add(10);
</script>
</head>
<body>
</body>
</html>
add是引用,function是對象.
跟java不一樣的地方:javascript中沒有方法重載的概念。方法可以有n個參數(shù),而傳參數(shù)時可以只傳1個參數(shù)。
數(shù)據(jù)類型 Undefined--類型 undefined--值
在JavaScript中有一個Function對象,所有自定義的函數(shù)都是Function對象類型的。
Function對象接收所有參數(shù)都為字符串類型的,其中最后一個參數(shù)是函數(shù)體,而前面的參數(shù)則是函數(shù)真正 需要接收的參數(shù)。
<html>
<head>
<script type="text/javascript">
var add =new Function("number","alert(number+20);");
add(10);
</script>
</head>
<body>
</body>
</html>
在javascript中,每一個Function對象都有一個 隱含的對象arguments,表示給函數(shù)實際傳遞的參數(shù).
<html>
<head>
<script type="text/javascript">
function add(){
alert(arguments.length);
alert(arguments[0]);
alert(arguments[1]);
}
add(10,20);
</script>
</head>
<body>
</body>
</html>
java中的方法重載,javascript中相對的也可以靠arguments來實現(xiàn)。
<html>
<head>
<script type="text/javascript">
function add(){
if(1==arguments.length){
alert(arguments[0]);
}else if(2==arguments.length){
alert(arguments[0]+arguments[1]);
}else if(3==arguments.length){
alert(arguments[0]+arguments[1]+arguments[2]);
}
}
add(2);
add(2,3);
add(2,3,4);
</script>
</head>
<body>
</body>
</html>
以上就是本文全部內(nèi)容了,小伙伴們是否了解了javascript對象模型和function對象了呢,有疑問請留言,大家共同進(jìn)步。
- Javascript Function對象擴(kuò)展之延時執(zhí)行函數(shù)
- 關(guān)于javascript function對象那些迷惑分析
- JavaScript學(xué)習(xí)筆記之Function對象
- JavaScript學(xué)習(xí)小結(jié)(一)——JavaScript入門基礎(chǔ)
- 淺談Javascript中Object與Function對象
- 每天一篇javascript學(xué)習(xí)小結(jié)(基礎(chǔ)知識)
- 每天一篇javascript學(xué)習(xí)小結(jié)(Array數(shù)組)
- 每天一篇javascript學(xué)習(xí)小結(jié)(Boolean對象)
- 每天一篇javascript學(xué)習(xí)小結(jié)(Date對象)
- 每天一篇javascript學(xué)習(xí)小結(jié)(Function對象)
相關(guān)文章
ZeroClipboard插件實現(xiàn)多瀏覽器復(fù)制功能(支持firefox、chrome、ie6)
Zero Clipboard 利用透明的Flash讓其漂浮在復(fù)制按鈕之上,這樣其實點(diǎn)擊的不是按鈕而是Flash ,這樣將需要的內(nèi)容傳入Flash,再通過Flash的復(fù)制功能把傳入的內(nèi)容復(fù)制到剪貼板2014-08-08