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
http://www.w3school.com.cn/js/jsref_sort.asp
<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>
分享到:
相关推荐
Part II: JavaScript Tutorial - Summary. Chapter 4: Browser and Document Objects. Chapter 5: Scripts and HTML Documents. Chapter 6: Programming Fundamentals, Part I. Chapter 7: Programming ...
**前端开源库-karma-verbose-summary-reporter** `karma-verbose-summary-reporter` 是一个专为前端开发者设计的开源库,它与 Karma 集成,提供了更详尽的测试结果摘要报告。Karma 是一个流行的自动化测试运行器,...
将GITHUB_TOKEN替换为您的回购密钥并触发操作后,您可以使用profile-summary-card-output文件夹中的所有内容。添加到现有存储库添加此操作以回购,并将yml文件中的GITHUB_TOKEN替换为您的回购密码。GitHub动作用法...
JavaScript是一种广泛应用于Web开发的脚本语言,尤其在前端领域占据主导地位。"JavaScript高级"这一主题涵盖了语言的深入理解和高级用法,包括但不限于原型、闭包、异步编程、模块化、性能优化以及ES6及以后的新特性...
- **JS Summary**:总结JavaScript的核心概念和技术要点。 #### JS示例 - **JS Examples**:提供超过200个示例代码,涵盖从简单到复杂的各种应用场景。 - **JS Objects Examples**:面向对象编程的示例,展示如何...
【标题】"lectures_summary-源码.rar" 提供的是一种学习资源的压缩包,很可能包含一系列编程课程的源代码。从"source code"这个词我们可以推测,这份资料可能与编程教学或者软件开发有关,可能是为了帮助学生或...
通常,"summary-20230720.zip"这样的文件名可能表示这是一个包含2023年7月20日总结或报告的压缩文件。如果这是一份关于IT行业的总结,可能涵盖了技术趋势、最新产品发布、行业新闻、代码示例或项目更新等内容。 ...
gitbook-plugin-summary Gitbook插件自动生成SUMMARY.md介绍这个插件是出于无法从基本树结构自动生成SUMMARY.md文件的挫败感而创建的。 结果是你安装了这个插件,它就在你当前的book.json文件book.json 。 无需...
总结---An-Chrome-Extension 当鼠标悬停在网站链接上时显示摘要的扩展要使用此扩展程序,您需要有 chrome。 与此扩展一起安装后,当您将鼠标移到链接上时,将显示一个附加窗口。 该窗口将显示链接的基本信息,即最大...
npm i @codersrank/summary --save 安装后,您需要导入和注册Web组件: import CodersrankSummary from '@codersrank/summary' ; // register web component as <codersrank> element window . customElements . ...
配置除了Jest提供的默认报告程序之外,还添加jest-summary-reporter : { "reporters": [ "default", "jest-summary-reporter" ]}如果您还想查看通过/未决的测试,请执行以下操作: { "reporters": [ "default", [...
JavaScript연산자연산자연산자메소드중중중토토토토토토토토토향향향그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을그래밍을원함원함원함 下载ECMAScript第5版(2009年...
标题中的“ant - build.xml - summary”指的是Apache Ant工具中的构建文件`build.xml`的概要。Apache Ant是一个广泛使用的Java项目自动化构建工具,类似于Unix的make,但基于XML。在Ant中,`build.xml`是项目的配置...
【标题】"summary-news-app:AI汇总的新闻应用程序"是一个基于现代Web技术和人工智能技术构建的项目,旨在为用户提供高效、个性化的新闻阅读体验。通过利用先进的自然语言处理(NLP)算法,特别是BERT模型,该应用...
Github配置文件摘要卡繁体中文此存储库受profile-summary-for-github的启发,此操作将生成github配置文件摘要卡并推送到您的存储库。 添加此动作后,您也可以自己触发动作。 将其添加到工作流程后,应触发工作流程,...
> npm install gulp-file-summary --save-dev用var fileSummary = require ( 'gulp-file-summary' ) ;gulp . task ( 'summary' , function ( ) { return gulp . src ( './lib/*.js' ) . pipe ( fileSummary ( { ...
`python-profile-summary-for-github` 是一个专为GitHub用户设计的工具,它提供了可视化的配置文件摘要,帮助用户更直观地理解并管理他们的GitHub个人资料。这个工具基于Python编程语言,利用了Flask框架来构建web...
npm install summary-statistics --save 用法 var ss = require ( 'summary-statistics' ) var values = [ 3 , 14 , 15 , 92 , 65 , 35 , 89 , 79 , 32 , 38 , 46 , 26 , 43 , 38 , 32 , 79 , 5 ] var summary = ...
对于 "node-github-profile-summary-master" 这个文件夹名,我们可以推测这是项目源代码的主分支或者是最稳定的版本。一般情况下,GitHub 仓库的默认分支是 `master`,其中包含了项目的主要代码和配置文件。这里可能...
Serratus-Summary-api-athena 带有示例的端点 /family/ /family/Coronaviridae /family/Coronaviridae?identityMin=80&identityMax=90 /sequence/ /sequence/AY874537_3000883 /sequence/AY874537_3000883?...