JavaScript functions 1th/3 page _javascript Tips

Source: Internet
Author: User
Tags anonymous

Author: F. Permadi
Translator: Sheneyan (Zi Wu)
English Original: INTRODUCTION to JavaScript functions
Chinese translation (including examples): JavaScript functions
Sub-note: a pretty good entry for function, personal feeling is quite classic.

Word Translation list:

Function: Functions (functional not translated)
DECLARE: Defining
Assign: assigning, assigning
Functionbody: Function body (is the content of the function)
Object: Objects
Property: Properties
Unnamed: Anonymous (not translated here to unnamed)
Object Oriented Programming: Surface relative Phase programming
Class: Classes (such as the later class data type I translate into class datatype)
Pointer: pointers
Reassign: reassigning
Nest: Nesting
Feature: Functions, features
Local/global: Local/Global
Blueprint: Blueprint (? )
User defined: Custom
Instance: Example
Prototype: Prototypes (no translations except headings)
Internal: Internal
Constructor: Builder
Duplication:

Functions: Defining

There are several ways in which you can define a function. All of these are valid, but there are some differences in how they are implemented in the background.

Commonly used in the drafting

In general, we use this notation to define a function:

CODE:
functionname ([parameters]) {functionbody};

Example D1:

CODE:
function Add (A, B)
{
return a+b;
}
Alert (Add (1,2)); Result 3

When we define a function like this, the content of the function is compiled (but not executed immediately unless we call it). And, maybe you don't know that when this function is created, an object with the same name is created. For our example, we now have an object called "Add" (to get a closer look at the bottom function: Object section.) )

anonymous functions

We can also define it by assigning a variable name to an anonymous function.

Example D2

CODE:
var add=function (A, B)
{
return a+b;
}
Alert (Add (1,2)); Result 3

This code does the same thing as the previous example. Maybe the syntax looks strange, but it should make you feel that the function is an object, and we just assign a name to the pair. You can think of it as a statement like Var myvar=[1,2,3]. The content of the function declared in this way will be compiled as well.

When we assign such a function, we do not necessarily require that it be an anonymous function. Here I did the same thing as ExampleD2, but I added the function name "Theadd" and I could refer to the function by calling the function name or the variable.

Example D2A

CODE:
var add=function theadd (A, B)
{
return a+b;
}
Alert (Add (1,2)); Result 3
Alert (Theadd (1,2)); And the result is 3.

Using this method to define a function is useful in object-oriented programming, because we can make a function a property of an object like this.

CODE:
var myobject=new Object ();
Myobject.add=function (a,b) {return a+b};
MyObject now has an attribute (or method) called "Add"
And I can use it as follows
Myobject.add (1, 2);

We can also define a function by using the operator new. This is one of the most unusual ways to define a function and is not recommended in this way unless there are special reasons (see below for possible reasons). The syntax is as follows:

CODE:
Varname=new Function ([Param1name, Param2name,... paramnname], functionbody);

Example D3:

CODE:
var add=new Function ("A", "B", "Return a+b;");
Alert (Add (3,4)); Result 7

I have two parameters here called A and B, and the function body returns the and of A and B. Please note that the new Function (...) Uppercase F is used instead of lowercase f. This tells JavaScript that we're going to create an object with a type that is a function. Also note that the parameter names and function bodies are passed as strings. We can add parameters as we please, JavaScript knows that the function is the last string before the closing parenthesis (you can write only the function body if there are no arguments). You don't have to write everything in one line (use \ or use a string connector + to separate the long code). \ tag tells JavaScript to look for the rest of the string on the next line. Examples are as follows:

Example D4

CODE:
var add=new Function ("A", "B",
"Alert" +//Note "+"
"(' adding ' +a+ ' and ' +b '); \//and" \ "different usage
return a+b; ");
Alert (Add (3,4)); Result 7

Defining a function in this way causes the function not to be compiled, and it may be slower than a function defined in other ways. As for why, take a look at this code:

Example D5

CODE:

function Createmyfunction (myoperator)
{
Return the new Function ("A", "B", "return a" + Myoperator + "B;");
}

var add=createmyfunction ("+"); Create function "Add"
var subtract=createmyfunction ("-"); Create function "Subtract"
var multiply=createmyfunction ("*"); Create function "Multiply"
Test the functions
Alert ("Result of addition =" +add (10,2)); The result is 12.
Alert ("Reduced result =" +subtract (10,2)); The result is 8.
Alert ("The result of the multiplication =" +multiply (10,2)); The result is 20.
alert (add);

This interesting example creates three different function and creates a new function by passing different parameters in real time. Because the compiler has no way of knowing what the final code will look like, the new Function (...) Content will not be compiled. So what's the good of that? Well, for example, this might be useful if you need users to be able to create their own functions, such as in a game. We may need to allow the user to add "behavior" to a "player". But again, in general, we should avoid using this form unless there is a special purpose.

Functions: Objects

A function is a special form of object in JavaScript. It is the first [B) class data type]. This means that we can add attributes to it. Here are some interesting points to note:

Creation of objects

As we've just mentioned, when we define a function, JavaScript actually creates an object in the background for you. The name of this object is the function name itself. The type of this object is a function. In the following example, we may not be aware of this, but we have actually created an object: it is called ball.

Example 1

CODE:
function Ball ()//may seem a little strange, but this statement
{//created an object called Ball
I=1;
}
Alert (typeof Ball); Result "function"

We can even print out the contents of this object and it will output the actual code of the function, Example2: Click Alert (Ball); Look at the Ball content.

Add a property

We were able to add attributes to object, including the objects function. Because the essence of defining a function is to create an object. We can "secretly" add attributes to a function. For example, we define the function ball here and add the attribute callsign.

CODE:
function Ball ()//may seem a little strange, but this statement
{//created an object called ball, and you can
//reference It or add attributes to it as follows
Ball.callsign= "The Ball"; Add attributes to Ball
alert (ball.callsign); Output "The Ball"

Pointer

Because function is an object, we can assign a pointer to a function. In the following example, the variable PTR points to the object myfunction.

CODE:
function MyFunction (message)
{
alert (message);
}
var ptr=myfunction; PTR points to myfunction.
PTR ("Hello"); The sentence will execute MyFunction: output "Hello"

We are able to run this function as if it had been replaced by the name of the pointer. So on top, this line of PTR ("Hello"); and MyFunction ("Hello"); the meaning is the same.

Pointers to functions are quite useful in object-oriented programming. For example, when we have multiple objects pointing to the same function (below):

Example 4 A

CODE:
function Sayname (name)
{
alert (name);
}
var object1=new Object (); Create three objects
var object2=new Object ();
var object3=new Object ();
Object1.saymyname=sayname; Assign this function to all objects
Object2.saymyname=sayname;
Object3.saymyname=sayname;

Object1.saymyname ("Object1"); Output "Object1"
Object2.saymyname ("Object2"); Output "Object2"
Object3.saymyname ("Object3"); Output "Object3"

Because only the pointer is saved (not the function itself), when we change the function object itself, all pointers to that function will change. We can see at the bottom:

Example 5:

CODE:

function MyFunction ()
{
alert (myfunction.message);
}
Myfunction.message= "old";
var ptr1=myfunction; PTR1 Point to MyFunction
var ptr2=myfunction; PTR2 also points to MyFunction

PTR1 (); Output "old"
PTR2 (); Output "old"

Myfunction.message= "New";

PTR1 (); Output "New"
PTR2 (); Output "New"

Current 1/3 page 123 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.