JavaScript(3) Function 函数

Function is such a complex concept in javascript,it's necessary to be talked about in a whole chapter.

Object

Function is object.So function is not only can be used as arguments but also can be used as results returned by another function.Just as following:

//sum adds numbers of array
function sum(arr)
{
	var _res=0,i=0;
	while(arr[i]){_res+=arr[i++];}
	return _res;
}

//accepts other function name as argument,and returns another function as result
function CallOtherFunc(funcname,arr)
{
	return funcname(arr);
}
//show result
alert(CallOtherFunc(sum,[1,2,3]));//6

Function Internal

There special objects exist inside a function:arguments and this.

Arguments is an array-like object that contains all of the arguments passed into functions.

function sum(numa,numb)
{
	alert(arguments[0]+","+arguments[1]);
	return numa+numb;
}
sum(1,2);//alert msg:1,2
The arguments object also has a property named callee which is pointing to the function itself,with this property you can design recursive function without dependence with the function name.

function factorial(num){
if (num <= 1) {
	return 1;
	} else {
		return num * arguments.callee(num-1)//origin is:return num * factorial(num-1)
	}
}

' This' object is a reference to the now-in context.when a function is called in the global scope of a web page, the this object points to window.

window.color = "red";
var o = { color: "blue" };
function sayColor(){
	var color = "black";
	alert(this.color);
}
sayColor(); //”red”
o.sayColor = sayColor;
o.sayColor(); //”blue”

Function Properties and Methods

There are two properties in each function:length and prototype.

The length means the count of arguments passed in.

function sum(num1, num2){
return num1 + num2;
}
alert(sum.length);//2

Prototype is very interesting.As I know,it is a new concept of other program language,such as java or C#.Prototype is a kind of object with methods and properties which will be got by each object instance.Yes,instance of object share the methods and properties of prototype.

function Chinese(){}
Chinese.prototype.skinColor="yellow";
Chinese.prototype.language="Chinese";
Chinese.prototype.eat=function(){alert("use chopsticks")};

var chaohuren=new Chinese();

alert(chaohuren.skinColor);//yellow

There are two methods as well:apply() and call().

The apply method has two arguments:this and an array of arguments of function.

function sum(num1, num2){
return num1 + num2;
}
function callSum1(num1, num2){
return sum.apply(this, arguments); //passing in arguments object
}
function callSum2(num1, num2){
return sum.apply(this, [num1, num2]); //passing in array
}
alert(callSum1(10,10)); //20
alert(callSum2(10,10)); //20

Call do the same thing with apply,the first argument is this,the remains are the same with the function.

function sum(num1, num2){
return num1 + num2;
}
function callSum(num1, num2){
return sum.call(this, num1, num2);
}
alert(callSum(10,10)); //20

The true value of the two function is to switch the context:

window.color = "red";
var o = { color: "blue" };
function sayColor(){
alert(this.color);
}
sayColor(); //red
sayColor.apply(this); //red
sayColor.apply(window); //red
sayColor.apply(o); //blue

In this example,object o does not have a function named sayColor,but you can use it with apply or call.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值