前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Javascript Function 对象 属性和方法

Javascript Function 对象 属性和方法

作者头像
亦山
发布2019-05-25 09:49:31
8280
发布2019-05-25 09:49:31
举报

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1432216

Attribute

Usage

explaination

example

arguments

function.arguments[0|1|2|...|n]

当前正在运行的函数的参数

func.arguments0,对参数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 (" ");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 += " ";    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 = this0;    for (i = 1; i < this.length; i++)    {    if (max < thisi)    max = thisi;    }    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 += " ";    for (i in callMe.arguments) {        s += "arguments: " + callMe.argumentsi;        s += " ";    }    return s;}document.write("Original function: ");document.write(callMe(1, 2));document.write(" ");document.write("Function called with apply: ");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 += " ";    for (i in callMe.arguments) {        s += "arguments: " + callMe.argumentsi;        s += " ";    }    return s;}document.write("Original function: ");document.write(callMe(1, 2));document.write(" ");document.write("Function called with call: ");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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年02月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档