Javascript Function 对象 属性和方法
| Attribute | Usage | explaination | example |
| arguments | [function.]arguments[[0|1|2|...|n]] | 当前正在运行的函数的参数 | func.arguments[0],对参数0 的引用 |
| arguments.callee | [function.]arguments.callee |
当前在正在执行的函数引用,可用于函数的递归。 该属性仅当相关函数正在执行时才可用。 |
function factorial(n){ if (n <= 0) return 1; else return n * arguments.callee(n - 1) } document.write(factorial(4)); |
| caller | functionName.caller | 获取调用当前函数的函数。 | function CallLevel(){ if (CallLevel.caller == null) return("CallLevel was called from the top level."); else return("CallLevel was called by another function."); } document.write(CallLevel()); // Output: CallLevel was called from the top level. |
| constructor | object.constructor | 指定创建一个对象的函数. | // A constructor function. function MyObj() { this.number = 1; } var x = new String("Hi"); if (x.constructor == String) document.write("Object is a String."); document.write ("<br />"); var y = new MyObj; if (y.constructor == MyObj) document.write("Object constructor is MyObj."); // Output: // Object is a String. // Object constructor is MyObj. |
| length | functionName.length |
创建函数的实例后,脚本引擎将该函数的 length 属性初始化 为该函数定义中的参数数量。 |
function ArgTest(a, b){ var s = ""; s += "Expected Arguments: " + ArgTest.length; s += "<br />"; s += "Passed Arguments: " + arguments.length; return s; } document.write(ArgTest(1, 2)); // Output: // Expected Arguments: 2 // Passed Arguments: 2 |
| prototype | objectName.prototype |
所有内部 JavaScript 对象都有一个只读的 prototype 属性。 可将属性和方法添加到原型中,但不能为 对象分配其他原型。 但是,可以向用户定义的对象分配新的原型。 |
function array_max( ){ var i, max = this[0]; for (i = 1; i < this.length; i++) { if (max < this[i]) max = this[i]; } return max; } Array.prototype.max = array_max; var myArray = new Array(7, 1, 3, 11, 25, 9 ); document.write(myArray.max()); // Output: // 25 |
| apply() | apply([thisObj[,argArray]]) |
调用函数,并用指定对象替换函数的 this 值, 同时用指定数组替换函数的参数。 |
function callMe(arg1, arg2){ var s = ""; s += "this value: " + this; s += "<br />"; for (i in callMe.arguments) { s += "arguments: " + callMe.arguments[i]; s += "<br />"; } return s; } document.write("Original function: <br/>"); document.write(callMe(1, 2)); document.write("<br/>"); document.write("Function called with apply: <br/>"); document.write(callMe.apply(3, [ 4, 5 ])); // Output: // Original function: // this value: [object Window] // arguments: 1 // arguments: 2 // Function called with apply: // this value: 3 // arguments: 4 // arguments: 5 |
| call() | call([thisObj[, arg1[, arg2[, [, argN]]]]]) |
调用一个对象的方法,用另一个对象 替换当前对象。 |
function callMe(arg1, arg2){ var s = ""; s += "this value: " + this; s += "<br />"; for (i in callMe.arguments) { s += "arguments: " + callMe.arguments[i]; s += "<br />"; } return s; } document.write("Original function: <br/>"); document.write(callMe(1, 2)); document.write("<br/>"); document.write("Function called with call: <br/>"); document.write(callMe.call(3, 4, 5)); // Output: // Original function: // this value: [object Window] // arguments: 1 // arguments: 2 // Function called with call: // this value: 3 // arguments: 4 // arguments: 5 |
| bind() |
function.bind(thisArg[,arg1[,arg2[,argN]]])
返回值 与 function 函数相同的新函数, thisArg 对象和初始参数除外 |
对于给定函数,创建具有与原始函数相同的 主体的绑定函数。 在绑定功能中,this 对象解析为传入的对象。 该绑定函数具有指定的初始参数。 |
// Define the original function. // Define the original function with four parameters. |

浙公网安备 33010602011771号