JS函数的表达式
//函数的表达式
var c=(function(a,b){console.log( a+b);})
var f=function ff(){};
var f=function fact(x){
if(x<=1){
return 1;
}else{
return x * fact(x-1);
}
}
console.log(f(5));
var f=function (x){
if(x<=1){
return 1;
}else{
return x * arguments.callee(x-1);
}
}
console.log(f(5));
function f1(xingcan){
//如果函数没有返回值、那么返回undefined
console.log(xingcan);
console.log(xingcan());
}
//f2()是函数声明
function f2(){
console.log("I am f2()");
return "f2 say hello1";
}
f1(f2);
JS函数的调用
<script>
//函数的调用
//1.函数的调用方式
(function (){console.log(12);})();
//函数立即执行
(function (x){ console.log(x+"喜欢美女");})("东哥");
(function f(x){
console.log(x); return x;
})(4);
(function(y){
console.log(y); return y;
}(6));
function add(a,b){
return a+b;
}
console.log(add(1,1));
//2.函数做为方法调用
var obj={
add:function(a,b){
return a+b;
}
}
var obj1={};
obj1.add=function(a,b){
return a+b;
}
var obj2={};
function add(a,b){
return a+b;
}
obj2.add=add;
console.log(obj.add(1,2));
console.log(obj1['add'](1,3));
console.log(obj2.add(1,4));
//3.构造函数
var arr= new Array();
//跟PHP一样实例化一个对象会自动调用构造函数
//4.间接调用
//间接调用和直接调用,唯一的区别就是可以绑定this指针
//如果不考虑this,这三种调用方式完全一样
function hypotenuse(a,b){
return Math.sqrt(a *a + b*b);
}
console.log(this.hypotenuse(3,4));
hypotenuse.call(this,3,4);
hypotenuse.apply(this,[3,4]);
</script>
立即执行函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="http://www.bjpowernode.com/Public/home/public/header/js/jquery.min.js?v=1.12.2"></script>
<script>
var clueContactRepeatIds = new Array();
var a=5;
(function ($,b,c,d) {
console.log("我是 $ 开始 ");
console.log($);
console.log("我是 $ 结束 ");
console.log("clueContactRepeatIds 等于 b ?" +(clueContactRepeatIds==b));
console.log(c);
console.log(d);
})(jQuery,clueContactRepeatIds,a);
</script>
</body>
</html>
<script>
var clueContactRepeatIds = new Array();
var a=5;
(function ($,b,c,d) {
console.log("我是 $ 开始 ");
console.log($);
console.log("我是 $ 结束 ");
console.log("clueContactRepeatIds 等于 b ?" +(clueContactRepeatIds==b));
console.log(c);
console.log(d);
function sayHello() {alert("hello world");}
function sayByeBye(){alert("Bye Bye~!");} //弹出bye bye。
sayByeBye();
})(jQuery,clueContactRepeatIds,a);
sayHello(); //没有被弹出、立即执行函数、执行过后函数内被摧毁。所以sayHello不会出现
</script>