全面理解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 方法。

相關文章
相關標籤/搜索