JavaScript入门基础:变量作用域、函数重载与arguments对象详解
作者:小括号时间:2026-10-23 06:18:25浏览:354
本文梳理JavaScript入门核心要点,涵盖语言大小写敏感性、分号使用规范及变量命名规则。重点解析var声明的变量提升与无块级作用域特性,说明函数同名覆盖现象,并演示如何利用arguments对象处理不定参数。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>Document.writeln()方法</title>
<script language="javascript">
function createsummary()
{
win2=open("","window2")
//win2.document.open("text/plain")
win2.document.writeln("title"+document.title)
win2.document.close()
}
</script>
</head>
<body>
<a name="#top"></a>
<form>
<input type="button" name="help" value="help"onClick="createsummary()">
</form>
<body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>Document.writeln()方法</title>
<script language="javascript">
function createsummary()
{
win2=open("","window2")
//win2.document.open("text/plain")
win2.document.writeln("title"+document.title)
win2.document.close()
}
</script>
</head>
<body>
<a name="#top"></a>
<form>
<input type="button" name="help" value="help"onClick="createsummary()">
</form>
<body>
</html>
JavaScript基础语法与变量声明规范
梳理以下关键知识点:
1:JavaScript是区分大小写的编程语言。
2:建议每行代码末尾使用分号';'进行分隔。
3:变量或函数名的首字母必须是字母、下划线或'$'符号。
4:在JavaScript中声明变量需使用var关键字,且其作用域为函数级而非块级。
变量作用域与函数重载机制
5:JavaScript不存在块级作用域。例如:
Var scope="globle";
function f(){
alert(scope); //显示“underfined"而不是"globle"
Var scope="local";
alert(scope);
}
F();
function f(){
alert(scope); //显示“underfined"而不是"globle"
Var scope="local";
alert(scope);
}
F();
与下方的代码示例具有相同的执行逻辑
Var scope="globle";
function f(){
Var scope;
alert(scope); //显示“underfined"而不是"globle"
Var scope="local";
alert(scope);
}
F();
function f(){
Var scope;
alert(scope); //显示“underfined"而不是"globle"
Var scope="local";
alert(scope);
}
F();
建议:将所有变量声明集中放置在函数开头,这是一种良好的编程习惯。
6:根据ECMAScript标准,JavaScript中的函数不支持重载。例如:
function doadd(i){
alert(i);
}
function doadd(i){
alert(i);
}
doadd(10);
alert(i);
}
function doadd(i){
alert(i);
}
doadd(10);
实际执行的是后定义的函数,前一个同名函数被后者覆盖;
arguments对象与不定参数处理
7:关于arguments对象的使用。
function doAdd()
If(arguments.length==1){
alert(arguments[0]+10);
}
If(arguments.length==2){
alert(arguments[0]+arguments[0);
}
doAdd(10);
doAdd(10,20);
If(arguments.length==1){
alert(arguments[0]+10);
}
If(arguments.length==2){
alert(arguments[0]+arguments[0);
}
doAdd(10);
doAdd(10,20);
运行效果:略……
1
23下一页阅读全文
作者最新文章
JS语法错误会阻止加载吗?区分SyntaxError与资源加载失败
2026-11-05 16:08:46
JavaScript常见SyntaxError语法错误排查与修复方法
2026-11-05 13:58:44
JS中slice、substring和substr截取字符串的区别
2026-11-05 13:43:47
JS 转 TS 手动迁移最佳实践:从配置 tsconfig 到消除 any 类型
2026-11-05 11:53:32
为什么JS数组push后得到的是数字而不是数组?
2026-11-05 09:59:18
