`

JavaScript Functions

 
阅读更多

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. 

 

分享到:
评论

相关推荐

    The Principles of Object-Oriented JavaScript 1st Edition

    –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...

    [JavaScript][PDF][英文版]Oh My JS

    13. Control the Complexity of Your JavaScript Functions with JSHint(用JSHint控制JavaScript函数的复杂性):JSHint是一个JavaScript代码质量工具,可以检测代码中的语法错误和潜在问题,有助于保持代码质量。...

    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...

    Learning JavaScript(O'Reilly,3ed,2016)

    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 ...

    Programming.the.BeagleBone.Black.epub

    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 ...

    WebGL编程指南源码.zip

    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 ...

    Getting started with WebAssembly & Emscripten-March 5, 2019.zip-共3分卷

    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 ...

    Getting started with WebAssembly & Emscripten-March 5, 2019.z01

    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 ...

    Getting started with WebAssembly & Emscripten-March 5, 2019.z02

    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 ...

    JS Function 名称排序

    在JavaScript中,函数是第一类对象,这意味着它们可以被赋值给变量、作为参数传递给其他函数,甚至可以作为其他函数的返回值。在这种上下文中,"JS Function 名称排序"指的是按照函数名称对这些可操作的对象进行排序...

    SCODevelopersDocumentation

    5. JavaScript函数(Toolkit JavaScript Functions):文档详述了一系列JavaScript函数,这些函数按照不同的功能进行了分类,例如: - 会话函数(Session Functions):包括初始化会话、终止会话以及设置学习者将在...

    javascript asp教程第十三课--include文件

    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 Explained

    ### JavaScript Objects, Functions, and Arrays 解析 #### 引言 本书旨在为初学者和有经验的开发者提供一个深入理解JavaScript中的对象、函数与数组的机会。作者Tony de Araujo是一位技术作家兼讲师,他通过丰富...

    SignalR Programming in Microsoft ASP.NET

    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 ...

    WhatIsJavaScriptFunctions.pdf 英文原版

    What Is JavaScript Functions

    Mathematical Functions in Javascript.zip

    本压缩包“Mathematical Functions in Javascript.zip”很可能包含一个名为“Calculator”的示例或工具,用于演示如何在JavaScript中实现数学运算。虽然没有具体的标签来进一步指导讨论方向,但我们可以深入探讨...

    带有 helperscripts 文件夹的freetextbox 控件

    3. **Custom JavaScript Functions**: 自定义的JavaScript函数,这些函数负责与FREETEXTBOX控件进行交互,比如初始化控件、添加自定义功能等。 4. **CSS Stylesheets**: 控件的样式表文件,用于定义FREETEXTBOX的...

    Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript[EPUB版]

    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 ...

Global site tag (gtag.js) - Google Analytics