js函数:
定义方法:
静态方法:
function 函数名(虚参数){
}
动态匿名方法:
var 函数名=new function([虚参数],"函数体“)
直接量方法:
var 函数名=new function(虚参数){
}
调用方法:
直接调用:函数名;
在链接中调用:<a href="javascript:函数名()">test</a>
在事件中调用:onclick="函数名()"
递归调用:定义:在函数体内部调用函数自身;
function p(i){return i<2?1:i+p(i-1);}
方法:
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);//==add(3,1);
function Animal(){
this.name = "Animal";
this.showName = function(){
alert(this.name);
}
}
function Cat(){
this.name = "Cat";
}
var animal = new Animal();
var cat = new Cat();
animal.showName.call(cat,","); //cat
//animal.showName.apply(cat,[]);
单一继承:
function Animal(){
this.name="fanzaijun";
this.shwoName=function(
return this.name;
)
}
function Cat(name){
Animal.call(this,name);
}
var cat=new Cat('FAFD');
cat.showName();
myfunc.call(func,"var"," fun");// "func" "var fun" ,var,fun是给myfunc的实参。
myfunc.apply(func,["var"," fun"]);// "func" "var fun" var,fun是给myfunc的实参。
apply:参数是字符串
call:参数是数组形式的字符串。
toString:
arguments对象:
在javascript中,不需要明确指出参数名,就能访问它们:
function hi(){
if(arguments[0]=='andy'){
return;
}
alert(arguments[0]);
}
arguments.length获取函数实参的函数。
arguments.callee返回当前正在指向的函数。
arguments.caller返回当前正在执行的函数名。
函数参数:
指正标识:
this:
callee:引用当前正在执行的函数。
prototype:
contructor: