您的位置:首页 > Web前端 > JavaScript

Javascript Function 对象 属性和方法

2014-02-09 23:56 309 查看
                   Attribute                        Usage                                explainationexample
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));
callerfunctionName.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.
constructorobject.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.
lengthfunctionName.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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: