函数是JS的引用数据类型
匿名函数
给一个函数名
或者
执行一个匿名函数
(function() {
//code here
})();
如果函数被调用时,所给的参数不够,缺少的参数将传递undefined
以上这个函数,我们在调用的时候有很灵活的方式
若提供第二参数,则直接在第二个参数的数组中操作
之前那段判断是否为空的语句可以简化成:
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 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]
和一下代码等效:
apply()和call()类似,只是apply()第二个参数是传递给函数的参数数组
Javascript 1.2实现apply()了,但直到Javascript 1.5才实现call()
JSGD里面给出了一些对象的工具函数
匿名函数
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];
}
}