javascript summary11-12

The arguments[] array is useful in a number of ways. The following example shows how you can use it to check that a function is invoked with the correct number of arguments, since JavaScript doesn't do this for you:

function f(x, y, z)

{

// First, check that the right number of arguments were 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...

}



The arguments[] array also opens up an important possibility for JavaScript functions: they can be written so that they work with any number of arguments. Here's an example that shows how you can write a simple max( ) function that accepts any number of arguments and returns the value of the largest argument it is passed (see also the built-in function Math.max( ), which in ECMAScript v3 also accepts any number of arguments):

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);


Defining Your Own Function Properties
When a function needs to use a variable whose value persists across invocations, it is often convenient to use a property of the Function object, instead of cluttering up the namespace by defining a global variable. For example, suppose we want to write a function that returns a unique identifier whenever it is invoked. The function must never return the same value twice. In order to manage this, the function needs to keep track of the values it has already returned, and this information must persist across function invocations. We could store this information in a global variable, but that is unnecessary because the information is used only by the function itself. It is better to store the information in a property of the Function object. Here is an example that returns a unique integer whenever it is called:

// 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++;

}


Functions as Data
// We define some simple functions here

function add(x,y) { return x + y; }

function subtract(x,y) { return x - y; }

function multiply(x,y) { return x * y; }

function divide(x,y) { return x / y; }


// Here's a function that takes one of the above functions

// as an argument and invokes it on two operands

function operate(operator, operand1, operand2)

{

return operator(operand1, operand2);

}


// We could invoke this function like this to compute the value (2+3) + (4*5):

var i = operate(add, operate(add, 2, 3), operate(multiply, 4, 5));


// For the sake of example, we implement the functions again, this time

// using function literals. We store the functions in an associative array.

var operators = new Object( );

operators["add"] = function(x,y) { return x+y; };

operators["subtract"] = function(x,y) { return x-y; };

operators["multiply"] = function(x,y) { return x*y; };

operators["divide"] = function(x,y) { return x/y; };

operators["pow"] = Math.pow; // Works for predefined functions too


// This function takes the name of an operator, looks up that operator

// in the array, and then invokes it on the supplied operands. Note

// the syntax used to invoke the operator function.

function operate2(op_name, operand1, operand2)

{

if (operators[op_name] == null) return "unknown operator";

else return operators[op_name](operand1, operand2);

}


// We could invoke this function as follows to compute

// the value ("hello" + " " + "world"):

var j = operate2("add", "hello", operate2("add", " ", "world"))

// Using the predefined Math.pow( ) function:

var k = operate2("pow", 10, 2)



Array.sort() function
[url]http://www.w3school.com.cn/js/jsref_sort.asp[/url]
<script type="text/javascript">

function sortNumber(a,b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值