Javascript 基础知识 - 函数
来源:互联网 发布:大津阈值算法 编辑:程序博客网 时间:2024/06/05 16:52
Javascript - 函数
1. 函数的arguments变量存储函数被调用时传过来的参数列表。Function.length 是指函数被定义时的参数个数。如果传的参数多于定义,多的会被忽略;如果传的参数少于定义,少的会边成undefined。function test(one,two){
console.log(arguments.length);
console.log(one + " " + two);
}
test.length;// 2, Function.length only care the number of the parameter, it will not change
test(); //arguments.length is 0, arguments is [], console is "undefined undefined " - less than declared
test(1,2);//arguments.length is 2 , arguments is [1,2]
test(1,2,3);//arguments.length is 3 , arguments is [1,2,3], console is "1 2" - more than declared , ignore
function test(){};
var a = test();
console.log(a);// console undefined
3. 三种方式去定义函数
1) 由关键字function来定义.这种方式定义,无论你定义的位置前后,都可以被引用
function test1(p1, p2, p3){
}
2) 由函数直接量来定义.这种方式定义,引用必须在定义之后
var test2 = function(param){
}
3) 由函数构造器来定义.这种方式决定以,最后一个参数就是函数体,不建议用这种方式定义
// new Function ([arg1[, arg2[, ... argN]],] functionBody)
var test3 = new Function("a", "b", "return a + b");
4. 调用函数的方式
1) 作为函数直接调用,函数里面的this指全局范围
var number = "global number";
function test(){
console.log(this.number);
}
test(); //console "global number"
2) 作为对象的某个方法来访调用,函数里面的this指对象
var object = {number : "object number"};
object.test1 = test;
object.test1(); //console "object number"
3) 作为new构造方法来调用,函数里面的this指构造函数
var instance = new test();// console "undefined"
4) Javascript 的原生方法call和apply,它们的第一个参数都是上下文对象(this 就指这个上下文对象);call 的后面的参数都是被传的参数列表;apply 的第二个参数是数组,用来传送被传的参数列表
call(content, parameter1 ,parameter2 ,...)
appy (content, [parameter1,parameter2,...]
实例:
function method (p1,p2){
console.log("arguments are " + showArguments(arguments));
console.log("number is " + this.number);
}
method.call({number : "call number"},1,2);//number is "call number"
method.apply({number : "apply number"},[3,4]);//number is "apply number"
function showArguments(args){
var i = 0, len = args.length, parameters = [];
while(i < len){
parameters.push(args[i++]);
}
console.log("arguments : ",parameters.join(" , "));
}
5. Javascript 函数是没有重载的概念
function test() {}
function test(a) {}
function test(i, v) {}
console.log("The number of arguments expected by the 'test' function is " + test.length);// test.length is 2
console.log("test : ", test.toString()) // log function test(i,v){}
事实上,上面代码与以下代码是同等的
var test = function (){};
test = function (a){};
test = function (i, v){};
6. 其实函数是特殊的对象,所以它也有自己的属性和方法(call 和apply, 上面已经说明)
1)callee 属性 : 用来指定当前函数本身,相当于 this
var a = function(x){
if(x<=1) return 1 ;
return x * arguments.callee(x-1);//引用当前函数
}
console.log(a(5)); // 5*4*3*2*1=120
2)length 属性 : arguments.length
提示: 所以arguments不是数组,其实是对象
0 0
- Javascript 基础知识 - 函数
- JavaScript函数基础知识
- JavaScript函数基础知识
- javascript基础知识复习3-javascript的函数
- Javascript基础知识盲点总结——函数
- javascript基础知识
- JavaScript基础知识
- javascript基础知识
- javascript基础知识
- JavaScript基础知识
- JavaScript基础知识
- JavaScript基础知识
- JavaScript基础知识
- javascript基础知识
- javascript基础知识
- javascript基础知识
- javascript基础知识
- Javascript基础知识
- 队列基本操作及银行简易排号的实现
- linux用户和用户组管理
- android存取数据(基础)
- BZOJ 3438 小M的作物 最大权闭合图
- vi配置
- Javascript 基础知识 - 函数
- postDelayed方法和removeCallbacks方法的使用(1)
- uboot 用 ramdisk 引导linux命令行,u-boot,bootargs,arm
- HDU—— 1157 Who's in the Middle
- CentOS 6.3下FTP安装及配置
- postDelayed方法和removeCallbacks方法的使用(2)
- if Tests and Syntax Rules--Learning Python chapter12
- 我的U3D之旅第二天
- weak 和 strong 的区别