JavaScript 函数Function 基础

函数是JS的引用数据类型

匿名函数
function() {
//Code here
}


给一个函数名
function foo() {
//code here
}

或者
var foo = function() {
//Code here
}


执行一个匿名函数
(function() {
//code here
})();

When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.

如果函数被调用时,所给的参数不够,缺少的参数将传递undefined

// Append the names of the enumerable properties of object o to the
// array a, and return a. If a is omitted or null, create and return
// a new array
function copyPropertyNamesToArray(o, /* optional */ a) {
if (!a) a = []; // If undefined or null, use a blank array
for(var property in o) a.push(property);
return a;
}


以上这个函数,我们在调用的时候有很灵活的方式

// Get property names of objects o and p
var a = copyPropertyNamesToArray(o); // Get o's properties into a new array
copyPropertyNamesToArray(p,a); // append p's properties to that array


若提供第二参数,则直接在第二个参数的数组中操作


之前那段判断是否为空的语句可以简化成:
a = a || [];
//it returns a if a is defined and non-null, even if a is empty. Otherwise, it returns a new, empty array.


aruguments对象
[quote]Within the body of a function, the identifier arguments has special meaning. arguments is a special property that refers to an object known as the Arguments object.

The Arguments object allows full access to these argument values, even when some or all are unnamed.
Furthermore, like true arrays, arguments has a length property that specifies the number of elements it contains.

the Arguments object defines a callee property that refers to the function that is currently being executed.
This property is rarely useful, but it can be used to allow unnamed functions to invoke themselves recursively. [/quote]

arguments对象包含函数调用时传递的所有参数;
aruguments对象像数组一样,包含length属性
aruguments的callee属性持有对当前函数的引用

function(x) {
if (x <= 1) return 1;
return x * arguments.callee(x-1);
}


Function Properties and Methods
函数的属性和方法

The length Property
[quote]The length property of the Function object specifies exactly how many declared parameters a function has. Note that unlike arguments.length, this length property is available both inside and outside of the function body.[/quote]

length属性可以告诉你这个函数定了多少参数,与arguments.length不同,它在函数内外都有用

The apply() and call() Methods
[quote]These methods allow you to invoke a function as if it were a method of some other object. [/quote]

f.call(o, 1, 2);

和一下代码等效:
o.m = f;
o.m(1,2);
delete o.m;


apply()和call()类似,只是apply()第二个参数是传递给函数的参数数组

Javascript 1.2实现apply()了,但直到Javascript 1.5才实现call()

JSGD里面给出了一些对象的工具函数

// Return an array that holds the names of the enumerable properties of o
//返回包含对象中所有属性名的数组
function getPropertyNames(/* object */o) {
var r = [];
for(name in o) r.push(name);
return r;
}

// Copy the enumerable properties of the object from to the object to.
// If to is null, a new object is created. The function returns to or the
// newly created object.
// 将一个对象的属性复制到另一个对象中
function copyProperties(/* object */ from, /* optional object */ to) {
if (!to) to = {};
for(p in from) to[p] = from[p];
return to;
}

// Copy the enumerable properties of the object from to the object to,
// but only the ones that are not already defined by to.
// This is useful, for example, when from contains default values that
// we want to use if they are not already defined in to.
// 求两个对象的并集,且赋给to对象
function copyUndefinedProperties(/* object */ from, /* object */ to) {
for(p in from) {
if (!p in to) to[p] = from[p];
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值