`

The ways to define a javascript function

 
阅读更多

1. Declarative Function

/*
The syntax:

function functionname(param1, param2,param3,...,paramn){
      finction statements
}

or 

var functionname = function (param1, param2,param3,...,paramn){
      finction statements
}

Example:*/

function sayHi(toWhom){
alert("hi " + toWhom);
}
sayHi("World");

var sayHi = function (toWhom){
alert("hi " + toWhom);
}
sayHi("World");

 

2. Anonymous Function

/*Syntax:
var variable= new Function("param1", "param2", ...,"paramn", "function body");

Example:*/

var func = new Function("x", "y",  "alert('x + y  is ' + (x + y))");
func(1, 2);

 3. Function Literal or Function Expression

function funcObject(x, y, op){
alert(op(x, y));
}

funcObject(1, 2,  function(x, y){return x + y;});
 

 

分享到:
评论

相关推荐

    The Principles of Object-Oriented JavaScript 1st Edition

    –The various ways to create objects –How to define your own constructors –How to work with and understand prototypes –Inheritance patterns for types and objects The Principles of Object-Oriented ...

    QT CMake 3.3编译器

    QT CMake 3.3编译器 解决 使用Qt Creator 14.0.1 (Community) 创建项目时...报错的内容就是:error: The kit needs to define a CMake tool to parse this project. 译 错误:工具包需要定义一个CMake工具来解析这个项目

    JavaScript Patterns

    * Discover different ways to define a function in JavaScript * Create objects that go beyond the basic patterns of using object literals and constructor functions * Learn the options available for ...

    You can define up to 4000 styles in a .xls workbook is a wrong excepiton

    You can define up to 4000 styles in a .xls workbook is a wrong excepiton 将抛异常的方法去掉,是程序继续执行,生成excel 。本来想免费,但是苦于没积分所以要了5分 见谅 。实在没积分的话可以给我发邮件,给...

    javascript.the.definitive.guide.5th.2006(英文版)

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    数据结构作业Hash表

    Use the inherited search function of class Dictionary to determine if a word exists in the dictionary. If the word exists in the dictionary, assume that it is spelled correctly. It if does not exist,...

    解决样式多问题 You can define up to 4000 styles 包括poi全部jar

    You can define up to 4000 styles in a .xls workbook”。这意味着当尝试创建更多的单元格样式时,系统会报错,因为`.xls`格式的工作簿不允许存储更多的样式。 在处理这种情况时,有几种可能的解决方案: 1. **...

    JavaScript Regular Expressions(PACKT,2015)

    Regular expressions are patterns or templates that allow you to define a set of rules in a natural yet vague way, giving you the ability to match and validate text. Therefore, they have been ...

    Excel.2013.Working.with.Data.Ranges.and.Tables

    identify multiple ways to define an Excel table using buttons on the HOME and INSERT tabs, the Quick Analysis feature, a keyboard shortcut and by formatting a data range as a table, and recognize ways...

    Mastering JavaScript Object-Oriented Programming

    Kicking off with a subtle refresher on objects, we'll show you how easy it is to define objects with the new ES6 classes. From there, we'll fly you through some essential OOP principles, forming a ...

    数位板压力测试

    The user should be able to use and control the tablet in as natural and easy a manner as possible. The user's preferences should take precedence over application requests, where possible. Here are ...

    JavaScript权威指南(第五版).chm

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    Function_Define.h

    新塘单片机的keil支持包,安装这个之后才能在keil中找到新塘的单片机,才能找到仿真器,并进行仿真。

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The definition of an inline function needs to be in a header file, so that the compiler has the definition available for inlining at the call sites. However, implementation code properly belongs in ....

    JavaScript权威指南第五版【新】(犀牛书)

    This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including: ...

    practical internet of things with javascript

    Using Raspberry Pi 3 and JavaScript (ES5/ES6) as the base to build all the projects, you will begin with learning about the fundamentals of IoT and then build a standard framework for developing all ...

    User Define Protocol Function for NX PLC _B1.pdf.pdf

    User Define Protocol Function for NX PLC _B1.pdfpdf,User Define Protocol Function for NX PLC _B1.pdf

    define方法和构造函数return对象学习

    Object.extend = function(a, b) { for (var i in b) a[i] = b[i]; return a; }; ``` #### 六、示例分析 最后,文章给出一个具体的示例,展示了如何使用这些自定义方法来创建对象: ```javascript var Demo = ...

Global site tag (gtag.js) - Google Analytics