全面理解Javascript中Function对象的属性和方法

函数是 javaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面咱们逐一来介绍这些属性和方法,这对于理解javascript的继承机制具备必定的帮助。javascript

 

属性(Properties)java

arguments数组

获取当前正在执行的 Function 对象的全部参数,是一个相似数组但不是数组的对象,说它相似数组是由于其具备数组同样的访问性质及方式,能够由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length。还有就是arguments对象存储的是实际传递给函数的参数,而不局限于函数声明所定义的参数列表(length),并且不能显式建立 arguments 对象。下面的Sample说明了这些性质。app

 

复制代码函数

function testArg(a, b) {this

    var actCount = arguments.length,spa

        expCount = testArg.length,prototype

        result;code

 

    result = "Expected arguments' count is " + expCount + ";<br/>";对象

    result += "Actual arguments' count is " + actCount + ".<br/>";

    result += "They are:<br/>";

    for (var i = 0; i < actCount; i++) {

        result += arguments[i] + ";<br/>";

    }

    if (arguments instanceof Array) {

        result += "arguments is an Array instance."

    } else if (arguments instanceof Object) {

        result += "arguments is an Object instance."

    }

    document.write(result);

}

testArg(1);

//output result is:

Expected arguments' count is 2;

Actual arguments' count is 1.

They are:

1;

arguments is an Object instance.

复制代码

length

获取函数定义的参数个数,

 

functionName.length

 

不一样于arguments.length,这点咱们在上面有介绍。由于Javascript调用函数时候对函数参数不做任何个数和类型检查,也就没有函数调用错误概念。可是咱们能够利用functionName.length和arguments.length的不一样,在函数调用内部来检测参数个数检测。

 

复制代码

function checkVarCount(a, b) {

    if (checkVarCount.length !== arguments.length) {

        alert("The count of the parameters you passed into the function doesn't match the function definition.");

    }

    alert("Successfully call the function");

}

checkVarCount(1, 2);

//Successfully call the function

checkVarCount(1);

//The count of the parameters you passed into the function doesn't match the function definition.

复制代码

caller

获取调用当前函数的函数。caller属性只有当函数正在执行时才被定义。

 

functionName.caller

 

若是函数是从 JavaScript 程序的顶层调用的,则caller包含null。若是在字符串上下文中使用 caller 属性,则其结果和 functionName.toString 相同,也就是说,将显示函数的反编译文本。

 

复制代码

function test() {

    if (test.caller == null) {

        document.write("test is called from the toppest level");

    } else {

        document.write("test is called from the function:<br/>");

        document.writeln(test.caller.toString());

    }

    document.write("<br />");

}

//call from the top level

test();

//output: test is called from the toppest level

 

function testOuter() {

    test();

}

 

//call from the function testOuter

testOuter();

//output:

//test is called from the function:

//function testOuter() { test(); }

复制代码

callee

返回正被执行的 Function 对象,即指定的 Function 对象的正文。

 

[functionName.]arguments.callee

 

callee 属性是 arguments 对象的一个成员,该属性仅当相关函数正在执行时才可用。一般这个属性被用来递归调用匿名函数。

 

复制代码

var fac = function(n){

  if (n <= 0)

     return 1;

  else

     return n * arguments.callee(n - 1);

}(4);

document.write(fac);//24

复制代码

constructor

获取建立某个对象的函数。constructor 属性是每一个具备原型的对象的原型成员。 这包括除 Global 和 Math 对象以外的全部内部 JavaScript 对象。 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.

复制代码

prototype

获取对象的原型。每个构造函数都有一个prototype属性,指向另外一个对象。这个对象的全部属性和方法,都会被构造函数的实例继承。这意味着,咱们能够把那些不变的属性和方法,直接定义在prototype对象上。

 

复制代码

function Man(name, age) {

    this.name = name;

    this.age = age;

}

Man.prototype.sex = "M";

Man.prototype.struggle = function () {

    alert("day day up!!!!");

}

var li = new Man("Leo", 10);

alert(li.sex);//M

li.struggle();//day day up

Man.prototype.isStrong = true;

alert(li.isStrong);//true

复制代码

这样咱们也能够向已定义好的对象(包括javascript提供的原生对象)中追加方法和属性,

 

复制代码

var aa = new Number(2);

alert(typeof (aa.add)); //undefined

Number.prototype.add = function (add1) {

    return this + add1;

}

alert(aa.add(1)); // 3

复制代码

方法

apply

调用函数,并用指定对象替换函数的this值,同时用指定数组替换函数的参数。

 

functionName.apply([thisObj[,argArray]])

 

若是argArray为无效值,则会抛出"Object expected"错误;若是thisObj和argArray都没有提供,则会使用当前this做为thisObj

 

复制代码

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]));

document.write("<br/>");

 

document.write("Function called with apply with invalid array: <br/>");

try{

    document.write(callMe.apply(3,2));

} catch (e) {

    document.write(e.message);

}

document.write("<br/><br/>");

 

document.write("Function called with apply without any argument: <br/>");

document.write(callMe.apply());

//Output result:

//Original function: 

//this value: [object Window]

//    arguments: 1

//    arguments: 2

 

//Function called with apply: 

//this value: 3

//    arguments: 4

//    arguments: 5

 

//Function called with apply with invalid array: 

//Function.prototype.apply: Arguments list has wrong type

 

//Function called with apply without any argument: 

//this value: [object Window]

复制代码

call

调用一个对象的方法,用另外一个对象替换当前对象。

 

call([thisObj[, arg1[, arg2[, [, argN]]]]])

 

它容许您将函数的 this 对象从初始上下文变为由 thisObj 指定的新对象。 若是没有提供 thisObj 参数,则 global 对象被用做 thisObj。与apply方法惟一不一样的地方是,apply的第二个参数类型必须是Array,而call方法是将全部的参数列举出来,用逗号分隔。

 

复制代码

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

对于给定函数,建立具备与原始函数相同的主体的绑定函数。 在绑定功能中,this对象解析为传入的对象。 该绑定函数具备指定的初始参数。

 

function.bind(thisArg[,arg1[,arg2[,argN]]])

 

其中function, thisArg为必选项。返回一个与 function 函数相同的新函数,只不过函数中的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

复制代码

如下代码演示如何使用 arg1[,arg2[,argN]]] 参数。 该绑定函数将 bind 方法中指定的参数用做第一个参数和第二个参数。 在调用该绑定函数时,指定的任何参数将用做第三个、第四个参数(依此类推)。

 

复制代码

// 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 

复制代码

toString

返回对象的字符串表示形式。

 

objectname.toString([radix])

 

objectname必需,指定须要获取字符串表示形式的对象。radix可选,为将数字值转换为字符串指定一个基数,此值仅用于数字。

 

toString 方法是一个全部内置的 JavaScript 对象的成员。 它的行为取决于对象的类型:

 

Object Behavior

Array 将 Array 的元素转换为字符串。 结果字符串被链接起来,用逗号分隔。

Boolean 若是布尔值为 true,则返回“true”。 不然返回“false”。

Date 返回日期的文本表示形式。

Error 返回一个包含相关错误信息的字符串。

Function 返回以下格式的字符串,其中 functionname 是一个函数的名称,此函数的 toString 方法被调用:

function functionname( ) { [native code] }

 

Number 返回数字的文字表示形式。

String 返回 String 对象的值。

Default 返回 "[object objectname]",其中 objectname 为对象类型的名称。

valueOf

返回对象的原生值。

 

object.valueOf( )

 

Javascript内部各个对象定义的valueOf不一样:

 

Object Return value

Array  返回数组实例。

 Boolean  布尔值。

 Date  从 UTC 1970 年 1 月 1 日午夜开始的存储的时间值(以毫秒为单位)。

 Function  函数自己。

 Number  数字值。

 Object  对象自己。 这是默认值。

 String  字符串值。

Math 和 Error 对象都没有 valueOf 方法。

相关文章
相关标签/搜索