JavaScript(3) Function 函数

来源:互联网 发布:女生衣服搭配知乎 编辑:程序博客网 时间:2024/05/04 22:19

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 arrayfunction 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 resultfunction CallOtherFunc(funcname,arr){return funcname(arr);}//show resultalert(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)); //20alert(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(); //redsayColor.apply(this); //redsayColor.apply(window); //redsayColor.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
原创粉丝点击