Thursday, March 3, 2011

A very good introductory article js function

 A very good introductory article
of js function: F. Permadi
Translator: Sheneyan (sub-Ukraine)
original English text: INTRODUCTION TO JavaScript Functions
Chinese translation (including examples): javascript's function
sub Ukraine Note: the function a pretty good introductory article, personal feel very classic.
Word Translation list:
function: function (Function not translated)
declare: Definition
assign : assignment, allocation
functionbody: function body (that is, the function of the content)
object: the object
property: property
unnamed: Anonymous (not translated here unnamed)
object oriented programming : surface relative phase programming
class: class (such as the back of the class data type data type class I translate)
pointer: pointer
reassign: redistribution
nest: nest
feature: functions, features
local / global: local / global
blueprint: Blueprint (?)
user defined: user-defined
instance: instance
prototype: prototype (in addition to the title is not translated)
internal: Internal
constructor: constructor
duplication:
function: Definition
following these methods can define a function. All of these are effective, but how they are implemented in the background there are some differences.

wording used by the general wording of all to define a function:
functionName ([parameters]) {functionBody};
Example D1:
CODE: < br> function add (a, b)
{
return a + b;
}
alert (add (1,2)); / / results 3
when we define the function so when the content is compiled function (but not immediately, unless we go to call it). And, maybe you do not know, when this function is created when the object has the same name was created. For our example , we now have an object called
Example D2
var add = function (a, b)
{
return a + b;
}
alert (add (1,2)); / / Result 3
this code and the previous example to do the same thing. Maybe the syntax looks strange, but it should also make you feel the function is an object, and we just for the right to assign a name. it can be as and var myVar = [1,2,3] the same statement. In this way the contents of the same declared function will be compiled.
When we assign such a function, we do not always require to be anonymous function. Here, I have made and ExampleD2 the same thing, but I added a function name var add = function theAdd (a, b)
{
return a + b;
}
alert (add (1,2)); / / results 3
alert (theAdd ( 1,2)); / / result is 3
use this way to define functions in object-oriented programming is useful, because we can be like under such a function into an object so that the attributes.
var myObject = new Object ();
myObject.add = function (a, b) {return a + b};
/ / myObject is now called > / / and I can use it like this
myObject.add (1, 2);
we are able to use the new operator to define a function. This is a most unusual way and defined functions This approach is not recommended unless there are special reasons (possible reasons see below). syntax is as follows:
varName = new Function ([param1Name, param2Name, ... paramNName], functionBody);
Example D3 :
var add = new Function (> I am here with two parameters called a and b, while the body of the function to return a and b, and. Please note that new Function (...) use capital F, not lower-case f. This means that in javascript, we will create One type is a Function object. should note that the parameter name and the function body are being passed as a string. we can do anything to increase the parameter, javascript function that experience is a right parenthesis before the last string (if no parameters You can only write the function body). You do not need to write everything in a single line (use or use string concatenation operator + to separate the length code). mark next line tells JavaScript to find the rest of the string. examples are :
Example D4
var add = new Function (+ a + 'and' + b); / / and
defined function in this way did not cause the function to be compiled, and it may be defined by other means than the slower function. As for why, look at this code:
Example D5
function createMyFunction (myOperator)
{
return new Function (createMyFunction (create function ; plus results = > alert (passing different parameters to create a new Function. because the compiler can not know the final code would look like, so new Function (...) content will not be compiled. that it has any good? ah, give a example, if you need users to create their own functions, when this feature might be useful, for example in the game. We may need to allow users to add , we should avoid using this form, unless there is a special purpose.
function: object
javascript function is in a special form of object. It is the first [b〕 class data type (class data type). This means that we can give it increases the property. Here are some interesting point to note:
object creation
as mentioned earlier, when we define a function, javascript is actually background to create an object for you. The name of the object is the function name itself. the object is of type function. In the following example, we may not realize it, but we actually have created an object: it is called Ball.
Example 1
function Ball () / / may seem odd, but this statement
{/ / create an object named Ball
i = 1;
} < br> alert (typeof Ball); / / results content.
property adding
we can add to the Object to add properties, including the object function. because the real definition of a function is to create an object. we can This defines the function Ball, and add the property callsign.
function Ball () / / may seem odd, but this statement
{/ / create an object called Ball, and you can
} / / reference to it or to it like the following to increase property
Ball.callsign = br> because the function is a pointer
object, we can assign a pointer to a function. the following example, the variable ptr points to the object myFunction.
function myFunction (message)
{
alert (message) ;
}
var ptr = myFunction; / / ptr points to myFunction
ptr (as if the function name has been replaced as the pointer name. So in the above, the line ptr (in object-oriented programming is useful. For example: when we have more than one object point to the same function when the (following):
Example 4A
function sayName (name)
{
alert (name );
}
var object1 = new Object (); / / create three objects
var object2 = new Object ();
var object3 = new Object ();
object1. sayMyName = sayName; / / this function is assigned to all objects
object2.sayMyName = sayName;
object3.sayMyName = sayName;
object1.sayMyName (;
object2.sayMyName (rather than the function itself), when we change the function object itself, all point to that function pointer will change. We can see at the bottom:
Example 5:
function myFunction ()
{< br> alert (myFunction.message);
}
myFunction.message = points to myFunction
ptr1 (); / / output I / O pointer to it. In the next example, I would change myfunction () content.
Example 6:
function myFunction ()
{
alert (
myFunction (); / / output I / O function myFunction ()
{
alert (New there will be no effect, because it created another function called myFunctionPtr not modify it.
Example 6B:
function myFunction ()
{
alert (>}
var savedFunc = myFunction;
savedFunc = function ()
{
alert (Old in it, I have another function called calculate.
Example 7
function getHalfOf (num1, num2, num3)
{
function calculate (number)
{
return number / 2;
}
var result => result + = calculate (num3);
}
var resultString = getHalfOf (10,20,30);
alert (resultString); / / outputs nested inside a function call. That is, you can not do call: getHalfOf.calculate (10), because only when the external function calculate (getHalfOf ()) will be present at run time. This is consistent with our previous discussion ( function will be compiled, but only when you go to when it will be called the Executive).
which function call?
you may be thinking naming conflict. For example, calculate the following function which is called will be called ?
Example 8
function calculate (number)
{
return number / 3;
}
function getHalfOf (num1, num2, num3)
{
function calculate (number)
{
return number / 2;
}
var result == calculate (num2) + output calculate the function using the global.
function: Data types and constructor function
Let us look at another special feature - which makes it different from other object types. a function can be used as a a blueprint data types. This feature is commonly used in object-oriented programming to simulate user-defined data types (user defined data type). using user-defined data types to create objects often referred to as user-defined objects (user defined object ).

data type defines a function in, we also created a new data type. This data type can be used to create a new object. The following example, I created a new named Ball data type.
Example DT1
function Ball ()
{
}
var ball0 = new Ball (); / / ball0 now point to a new object
alert (ball0); / / output performs Ball (), and pass the reference ball0 (for the calling object). Here, you'll see this message: Example DT2
function Ball (message)
{
alert (message);
}
var ball0 = new Ball (
ball0.name = Section 6 of the code is the bottom line of code as a short 6-8 line:
function Ball (message)
{
alert (message);
}
var ball0 = new Object ();
ball0.construct = Ball;
ball0.construct (name = the meaning of the line and then review it back then Example 4. Note: You may consider direct run ball0.Ball (;...created. we can create later add properties to this object (like I did above, add the attribute name. The next question is, if we create another instance of this object, we have once again like the following to the new object to add this property.)
Example DT3 (creates 3 ball objects)
function Ball ()
{
}
var ball0 = new Ball (); / / ball0 now points to the type of Ball, a new instance of the
ball0.name = ball-1 ball-1 cause problems. what a good way to automatically increase the attributes? ah, there is a: Use this keyword. this function in the word has a special significance. It points to the object that calls the function. Let us look at the following alternative an example, this time we add on the constructor of these attributes:
Example DT4
function Ball (message, specifiedName)
{
alert (message);
this.name = specifiedName ;
}
var ball0 = new Ball (Remember: a new keyword ultimately make the constructor is executed. In this case, it will run the Ball (Therefore, this line: this.name = specifiedName into ball0.name = a name attribute to the ball0, the previous example looks very much like doing, but it is a more scalable way better. Now, we can do anything to create a lot of ball with the property without our manual add them. Moreover, people also want to create the Ball object can clearly understand its constructor and can easily find all the properties Ball. Let us add more attributes to the Ball Lane.
Example DT5
function Ball (color, specifiedName, owner, weight)
{
this.name = specifiedName;
this.color = color;
this.owner = owner;
this.weight = weigth;
}
var ball0 = new Ball (Bowling Ball ; red Balloon .

the object assigned to the property are limited to only add that we did not shaped like a string or a number of simple data types like a property. We can also assign attributes to objects. Here, supervisor is Employee's a property.
Example DT6
function Employee (name, salary, mySupervisor)
{
this.name = name;
this.salary = salary;
this.supervisor = mySupervisor ;
}
var boss = new Employee (is an object.

will function as the property of any type of object can be used as a property, recall the previous Example 4 (not Example DT4), the function is also an object. so you can make a function as an object a property. Now, I will add two functions getSalary and addSalary.
Example DT7
function Employee (name, salary)
{
this.name = name;
this.salary = salary;
this.addSalary = addSalaryFunction;
this.getSalary = function ()
{
return this.salary;
};
}
function addSalaryFunction (addition )
{
this.salary = this.salary + addition;
}
var boss = new Employee (/ boss long 10K hh Why pay wages can grow so much the boss: '(
alert (boss.getSalary ()); / / output 210Khh why the default wages are so high hh:' (
addSalary and getSalary Demo assign the function of several properties ...

No comments:

Post a Comment