function functionName(arg){
//函数体
}
helloworld(); //在代码执行之前会先读取函数声明
function helloworld(){
console.log("hello world");
}
var functionName=function(arg){
//函数体
}
helloworld(); //错误,还未赋值,函数不存在
var helloworld=function(){
console.log("hello world");
}
var helloworld; //声明
if(condition){ //条件
helloworld=function(){ //赋值
console.log("hello world");
}
}
else{
helloworld=function(){ //赋值
console.log("你好,世界");
}
}
function foo(n) {
if (n <= 0)
return 0;
else if (n > 0 && n <= 2)
return 1;
else
return foo(n - 1) + foo(n - 2);
}
var foo1 = foo;
foo = null;
console.log(foo1(34));
function foo(n) {
if (n <= 0)
return 0;
else if (n > 0 && n <= 2)
return 1;
else
return arguments.callee(n - 1) + arguments.callee(n - 2);
}
var foo = (function f(n) {
if (n <= 0)
return 0;
else if (n > 0 && n <= 2)
return 1;
else
return f(n - 1) + f(n - 2);
});
var n = 100; //定义一个全局变量
function fn() {
console.log(n); //函数内部直接读取全局变量
}
fn();
function fn() {
var n = 100;
}
console.log(n); //n is not defined
function fn() {
n = 100;
}
fn();
console.log(n); //100
function fn() {
n = 100;
function fn1() {
console.log(n);
}
fn1();
}
fn(); //100
function fn() {
n = 100;
function fn1() {
console.log(n);
}
return fn1;
}
var result=fn();
result(); //100
function fn() {
n = 100;
nadd = function () {
n += 1;
}
function fn1() {
console.log(n);
}
return fn1;
}
var result = fn();
result(); //100
nadd();
result(); //101
(function(){
//块级作用域
})();
(function () {
var now = new Date();
if (now.getMonth() == 0 && now.getDate() == 1) {
alert("新年快乐");
}
})();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有