- 浏览: 359899 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (511)
- AgileMethodology (4)
- RDBMS (32)
- NoSQL (16)
- Java (27)
- Python (28)
- Maven (15)
- Linux (27)
- CommonUtils (13)
- IDE (8)
- JavaScript (17)
- jQuery (2)
- OSGi (2)
- JavaWeb (5)
- Spring (37)
- Struts2 (3)
- ORM (13)
- Ant (1)
- apache-tiles (1)
- FreeMarker (2)
- JSON (8)
- XML (1)
- JUnit (3)
- Lucene (1)
- Web Service (9)
- Design Pattern (13)
- Algorithm (21)
- JVM (14)
- Hadoop (16)
- Spark (5)
- Scala (31)
- Git (4)
- Server (3)
- Node.js (18)
- Concurrent (42)
- Lock (9)
- Collections (3)
- Network (11)
- MicroService (7)
- Docker (13)
- FP (20)
- spring.io (2)
- ELK (1)
- Kafka (5)
最新评论
Functions are the core of any language, because they allow the encapsulation of statements that can be run anywhere and at any time. Functions in ECMAScript are declared using the function keywork, followed by a set of arguments and then the body of the function. The basic syntax is as follows:
function functionName(arg0, arg1, ... , argN) { statements }
Here are some examples.
Example01
// Function Example 1 function sayHi(name, message) { alert("Hello " + name + ", " + message); } sayHi("JavaScript", "how are you today?");
Example02
// Function Example 2 function sum(num1, num2) { return num1 + num2; } var result = sum(5, 10); alert(result);
Example03
// Function Example 3 function diff(num1, num2) { if (num1 < num2) { return num2 - num1; } else { return num1 - num2; } } var result = diff(7, 10); alert(result);
Example04
// Function Example 4 function sayHi(name, message) { return; alert("Hello " + name + ", " + message); //never called } sayHi("JavaScript", "how are you today?");
Example05
// Function Example 5 function sayHi() { alert("Hello " + arguments[0] + ", " + arguments[1]); } sayHi("JavaScript", "how are you today?");
Example06
// Function Example 6 function howManyArgs() { alert(arguments.length); } howManyArgs("string", 45); //2 howManyArgs(); //0 howManyArgs(12); //1
Example07
// Function Example 7 function doAdd() { if (arguments.length == 1) { alert(arguments[0] + 10); } else if (arguments.length == 2) { alert(arguments[0] + arguments[1]); } } doAdd(10); //20 doAdd(30, 20); //50
Example08
// Function Example 8 function doAdd(num1, num2) { if (arguments.length == 1) { alert(num1 + 10); } else if (arguments.length == 2) { alert(arguments[0] + num2); } } doAdd(10); //20 doAdd(30, 20); //50
Example09
// Function Example 9 function doAdd(num1, num2) { arguments[1] = 10; alert(arguments[0] + num2); } doAdd(10, 20); //20 doAdd(30, 20); //50
This version of doAdd() always overwrites the second argument with a value of 10. Because values in the arguments object are automatically reflected by the corresponding named arguments, the change to arguments[1] also changes the value of num2, so both have a value of 10. This doesn't mean that both access the same memory space, though; their memory spaces are separate but happen to be kept in sync. This effect goes only one way: changing the named argument does not result in a change to the corresponding value in arguments. Another thing to keep in mind: if only one argument is passed in, then setting arguments[1] to a value will not be reflected by the named argument. This is because the length of the arguments object is set based on the number of arguments passed in, not the number of named arguments listed for the function.
All arguments in ECMAScript are passed by value. It is not possible to pass arguments by reference.
Example10
// Function Example 10 function addSomeNumber(num) { return num + 100; } function addSomeNumber(num) { return num + 200; } var result = addSomeNumber(100); //300 alert(result);
ECMAScript functions cannot be overloaded in the traditional sense. If two functions are defined to have the same name in ECMAScript, it is the last function that becomes the owner of that name.
发表评论
-
JS DES加密
2017-06-29 15:41 413原创转载请注明出处:http://agilestyle.i ... -
JS实现键盘Enter键提交表单
2017-06-28 15:01 412原创转载请注明出处:http://agilestyle.i ... -
JS Typical Questions
2017-04-21 10:56 534原创转载请注明出处:http://agilestyle.i ... -
JSON.parse() & JSON.stringify()
2017-01-10 14:16 535原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object Properties —— Preventing Object Modification
2016-11-30 16:52 457原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object Properties —— Property attributes
2016-11-30 16:37 431原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object Properties —— Enumeration, Types of Properties
2016-11-29 21:16 433原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object Properties —— Define, Detect, Remove
2016-11-29 20:46 355原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object Methods
2016-11-29 14:06 300原创转载请注明出处:http://agilestyle.i ... -
JavaScript Function Overloading
2016-11-29 11:00 455原创转载请注明出处:http://agilestyle.i ... -
JavaScript Function Declarations vs. Function Expressions
2016-11-29 10:20 494原创转载请注明出处:http://agilestyle.i ... -
JavaScript Primitive Wrapper Types
2016-11-28 21:39 1034原创转载请注明出处:http://agilestyle.i ... -
JavaScript Primitive and Reference Types
2016-11-28 15:03 303原创转载请注明出处:http://agilestyle.i ... -
JavaScript Object-Oriented Programming -- Inheritance
2012-06-14 05:23 875The concept most often discus ... -
JavaScript Object-Oriented Programming -- Object Creation
2012-06-14 00:35 929The Factory Pattern The f ... -
JavaScript DOM Demo
2012-06-10 21:52 804Example01 <!DOCTYPE html> ...
相关推荐
–What makes JavaScript functions so unique –The various ways to create objects –How to define your own constructors –How to work with and understand prototypes –Inheritance patterns for types and...
13. Control the Complexity of Your JavaScript Functions with JSHint(用JSHint控制JavaScript函数的复杂性):JSHint是一个JavaScript代码质量工具,可以检测代码中的语法错误和潜在问题,有助于保持代码质量。...
This file contains JavaScript functions to convert ASCII math notation and LaTeX to Presentation MathML. Simple graphics commands are also translated to SVG images. The conversion is done while the (X...
Understand the basic usage and mechanics of JavaScript functions Explore objects and object-oriented programming Tackle new concepts such as iterators, generators, and proxies Grasp the complexities ...
Use JavaScript functions and timers Perform analog and digital I/O Work with expansion capes and modules Design Web interfaces that control electronics Assemble and program a robot and an e-mail ...
This book covers the WebGL 1.0 API along with all related JavaScript functions. You will learn how HTML, JavaScript, and WebGL are related, how to set up and run WebGL applications, and how to ...
12 Exporting C functions to JavaScript 13 Calling JavaScript functions from C 14 Preamble Conversion Functions 15 Emscripten Module & Memory Buffer 16 Debugging 17 Runtime Initialisation 18 Emrun ...
12 Exporting C functions to JavaScript 13 Calling JavaScript functions from C 14 Preamble Conversion Functions 15 Emscripten Module & Memory Buffer 16 Debugging 17 Runtime Initialisation 18 Emrun ...
12 Exporting C functions to JavaScript 13 Calling JavaScript functions from C 14 Preamble Conversion Functions 15 Emscripten Module & Memory Buffer 16 Debugging 17 Runtime Initialisation 18 Emrun ...
在JavaScript中,函数是第一类对象,这意味着它们可以被赋值给变量、作为参数传递给其他函数,甚至可以作为其他函数的返回值。在这种上下文中,"JS Function 名称排序"指的是按照函数名称对这些可操作的对象进行排序...
5. JavaScript函数(Toolkit JavaScript Functions):文档详述了一系列JavaScript函数,这些函数按照不同的功能进行了分类,例如: - 会话函数(Session Functions):包括初始化会话、终止会话以及设置学习者将在...
Experienced JavaScript programmers also know that JavaScript functions are data types. So, we should be able to store a JavaScript function inside a Session Variable or an Application Variable, ...
### JavaScript Objects, Functions, and Arrays 解析 #### 引言 本书旨在为初学者和有经验的开发者提供一个深入理解JavaScript中的对象、函数与数组的机会。作者Tony de Araujo是一位技术作家兼讲师,他通过丰富...
Use the simple ASP.NET API in SignalR for creating server-to-client remote procedure calls (RPC) that call JavaScript functions in client browsers from server-side .NET code. Exploit the API for ...
What Is JavaScript Functions
本压缩包“Mathematical Functions in Javascript.zip”很可能包含一个名为“Calculator”的示例或工具,用于演示如何在JavaScript中实现数学运算。虽然没有具体的标签来进一步指导讨论方向,但我们可以深入探讨...
3. **Custom JavaScript Functions**: 自定义的JavaScript函数,这些函数负责与FREETEXTBOX控件进行交互,比如初始化控件、添加自定义功能等。 4. **CSS Stylesheets**: 控件的样式表文件,用于定义FREETEXTBOX的...
Precise and practical explanations of JavaScript’s functions and variable scoping semantics Useful JavaScript programming patterns and idioms, such as options objects and method chaining In-depth ...