Javascript Function 对象 属性和方法
2013-11-02 15:37
232 查看
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. var checkNumericRange = function (value) { if (typeof value !== 'number') return false; else return value >= this.minimum && value <= this.maximum; } // The range object will become the this value in the callback function. var range = { minimum: 10, maximum: 20 }; // Bind the checkNumericRange function. var boundCheckNumericRange = checkNumericRange.bind(range); // Use the new function to check whether 12 is in the numeric range. var result = boundCheckNumericRange (12); document.write(result); // Output: true // Define the original function with four parameters. var displayArgs = function (val1, val2, val3, val4) { document.write(val1 + " " + val2 + " " + val3 + " " + val4); } var emptyObject = {}; // Create a new function that uses the 12 and "a" parameters // as the first and second parameters. var displayArgs2 = displayArgs.bind(emptyObject, 12, "a"); // Call the new function. The "b" and "c" parameters are used // as the third and fourth parameters. displayArgs2("b", "c"); // Output: 12 a b c |
相关文章推荐
- TypeError: Error #1009: 无法访问空对象引用的属性或方法。
- XMLHttpRequest对象的属性与方法
- 几个很实用的BOM属性对象方法
- JavaScript原生对象之Number对象的属性和方法详解
- 常见错误:JavaScript 提交form 的时候,Error:对象不支持此属性或方法
- DOM和XMLHttpRequest对象的属性和方法整理
- Jquery jqXHR对象的属性和方法
- JavaScript原生对象属性和方法详解——Date对象
- 关于变量的作用域04想要改变一个对象属性的正确方法
- 判断JS对象是否拥有某属性的方法推荐
- JavaScript对象、事件、属性及方法参考大全
- 给对象添加方法和属性
- Java中通过注解+反射拿到对象的属性和方法
- ADO三大对象的属性、方法、事件及常数[ZT]
- 【Extjs】ExtJS Bug之IE8对象不支持此属性或方法
- HTML DOM Element 对象的全部属性和方法
- [反射]通用方法 命名空间,类,对象,属性
- 对象属性(状态等)位控制方法
- javascript封装判断全数据类型方法, toString方法, 对象的枚举(in, instanceof, hasOwnProperty的用法), arguments类数组属性