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,2The 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.
- JavaScript(3) Function 函数
- JavaScript语法学习3--函数(Function)
- javascript的函数function
- javascript 函数(function)
- JavaScript中的Function函数
- javascript (function(){})()匿名函数
- JavaScript function 函数
- javascript中的函数Function
- javascript中的函数Function
- javascript中的函数Function
- JavaScript function函数种类
- Javascript中的function函数
- 【JavaScript】Function函数
- javascript 匿名函数 -- (function(){ ... })();
- JavaScript函数-function总结
- javaScript函数Function类型
- javascript中的函数(Function)
- JavaScript function函数种类
- xcode6使用矢量图
- make bzImage时出现错误gcc: error: elf_i386/elf_x86_64: No such file or directory
- Java内存管理及垃圾回收总结
- java nio详解。。
- QStyleFactory
- JavaScript(3) Function 函数
- 基于ARM处理器的TSC2046触摸屏控制器的应用
- 英语四级听力训练有方
- java swing中实现动画的两种方式。
- 跳跃表
- opencv程序三:canny边缘检测算法
- 如何安装cocoapods
- Linux中的set和env命令
- 那些不容错过的Xcode插件