1. function arguments: arguments 表示函数的参数列表数组。
2. argument.callee property: 表示函数本身:
function(x) {
if (x <= 1) return 1;
return x * arguments.callee(x-1);
}
3. Defining Your Own Function Properties:
// Create and initialize the "static" variable.
// Function declarations are processed before code is executed, so
// we really can do this assignment before the function declaration.
uniqueInteger.counter = 0;
// Here's the function. It returns a different value each time
// it is called and uses a "static" property of itself to keep track
// of the last value it returned.
function uniqueInteger() {
// Increment and return our "static" variable
return uniqueInteger.counter++;
}
4.Utility Function Examples
// 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.
function copyUndefinedProperties(/* object */ from, /* object */ to) {
for(p in from) {
if (!p in to) to[p] = from[p];
}
}
5. a class example:
// We begin with the constructor
function Circle(radius) {
// r is an instance property, defined and initialized in the constructor.
this.r = radius;
}
// Circle.PI is a class propertyit is a property of the constructor function.
Circle.PI = 3.14159;
// Here is an instance method that computes a circle's area.
Circle.prototype.area = function( ) { return Circle.PI * this.r * this.r; }
// This class method takes two Circle objects and returns the
// one that has the larger radius.
Circle.max = function(a,b) {
if (a.r > b.r) return a;
else return b;
}
// Here is some code that uses each of these fields:
var c = new Circle(1.0); // Create an instance of the Circle class
c.r = 2.2; // Set the r instance property
var a = c.area( ); // Invoke the area( ) instance method
var x = Math.exp(Circle.PI); // Use the PI class property in our own computation
var d = new Circle(1.2); // Create another Circle instance
var bigger = Circle.max(c,d); // Use the max( ) class method
本文深入探讨了JavaScript中的一些高级特性,包括使用arguments对象处理函数参数、实现类的静态属性和方法、以及实用工具函数的编写。通过具体示例介绍了如何定义自定义函数属性,如递归调用自身的方法,创建唯一整数生成器,并提供了获取对象属性名、复制对象属性等实用函数。
1566

被折叠的 条评论
为什么被折叠?



