ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年2月15日 星期日
javascript memo: function
//javascript 也可以這樣用
a = a || []
for(var property in o) a.push(property)
Arguments
function f(x, y, z) {
// First, verify that the right number of arguments was passed
if (arguments.length != 3) {
throw new Error("function f called with " + arguments.length +
"arguments, but it expects 3 arguments.");
}
// Now do the actual function...
}
// built-in function Math.max(), which behaves the same way
function max(/* ... */) {
var m = Number.NEGATIVE_INFINITY;
// Loop through all the arguments,
looking for, and
// remembering, the biggest
for(var i = 0; i < arguments.length; i++)
if (arguments[i] > m) m = arguments[i];
// Return the biggest
return m;
}
var largest = max(1, 10, 100, 2, 3, 1000, 4, 5, 10000, 6);
callee:
The Arguments object defines a callee property that refers to the function that is currently being executed.
function(x) { if (x <= 1) return 1;
return x * arguments.callee(x-1);
}
Using Object Properties as Arguments
function easycopy(args) { arraycopy(args.from,
args.from_start || 0, // Note default value provided
args.to,
args.to_start || 0,
args.length);
}
// Here is how you might invoke easycopy():
var a = [1,2,3,4];
var b = new Array(4);
easycopy({from: a, to: b, length: 4});
instanceof和typeof
// Return the sum of the elements of array (or array-like object) a. // The elements of a must all be numbers, but null and undefined
// elements are ignored.
function sum(a) {
if ((a instanceof Array) || // if array
(a && typeof a == "object" && "length" in a)) { // or array like
var total = 0;
for(var i = 0; i < a.length; i++) {
var element = a[i];
if (!element) continue; // ignore null and undefined elements
if (typeof element == "number") total += element;
else throw new Error("sum(): all array elements must be numbers");
}
return total;
}
else throw new Error("sum(): argument must be an array");
}
function check(args) {
var actual = args.length; // The actual number of arguments
var expected = args.callee.length; // The expected number of arguments , 這個就是Function.length
if (actual != expected) { // Throw an exception if they don't match
throw new Error("Wrong number of arguments: expected: " +
expected + "; actually passed " + actual);
}
}
function f(x, y, z) {
// Check that the actual # of args matches the expected # of args
// Throw an exception if they don't match
check(arguments);
// Now do the rest of the function normally
return x + y + z;
}
apply and call
f.call(o, 1, 2);
//This is similar to the following lines of code:
o.m = f;
o.m(1,2);
delete o.m;
//The apply() method is like the call() method, except that the arguments to be passed to the function are specified as an array:
f.apply(o, [1,2]);
The Function() Constructor
//A last, very important point about the Function() constructor is that the functions it creates do not use lexical scoping; instead, they are always compiled as if they were top-level functions, as the following code demonstrates:
var y = "global";
function constructFunction() {
var y = "local";
return new Function("return y"); // Does not capture the local scope!
}
// This line displays "global" because the function returned by the
// Function() constructor does not use the local scope. Had a function
// literal been used instead, this line would have displayed "local".
alert(constructFunction()()); // Displays "global"
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言