Transferred from:Http://snandy.iteye.com/blog/420000
1. () parentheses OperatorThe () operator is commonly used to call/execute a function.
JS Code
- // Fun1
- Function fun1 (){
- Alert ('I was called ');
- }
- Fun1 ()
- // Fun2 with Parameters
- Function fun2 (PARAM ){
- Alert (PARAM );
- }
- Fun2 ('I was called ')
After ecmascript3 is added to the function with call and apply, the following two methods are available:
2. Call
JS Code
- // Fun1
- Function fun1 (){
- Alert ('I was called ');
- }
- Fun1.call (null );
- // Fun2 with Parameters
- Function fun2 (PARAM ){
- Alert (PARAM );
- }
- Fun2.call (null, 'I was called ')
3. Apply
JS Code
- // Fun1
- Function fun1 (){
- Alert ('I was called ');
- }
- Fun1.apply (null );
- // Fun2 with Parameters
- Function fun2 (PARAM ){
- Alert (PARAM );
- }
- Fun2.apply (null, ['I was called'])
4. New(This method is not recommended)
JS Code
- // Fun1
- Function fun1 (){
- Alert ('I was called ');
- }
- New fun1 ();
- // Fun2 with Parameters
- Function fun2 (PARAM ){
- Alert (PARAM );
- }
- New fun2 ('I was called ')
OK. From the above call method, there is no difference in the execution results of the four methods. However, if a function returns a value, calling the new method may disappoint you.
JS Code
- // Function fun with returned values
- Function fun (){
- Alert ('I was called ');
- Return "Jack ";
- }
- VaR c = new fun ();
- Alert (c); // [object], why not "Jack "?
This is the case,
JS Code
- // Function fun with returned values
- Function fun (){
- Alert ('I was called ');
- Return {Name: 'jack '};
- }
- VaR c = new fun ();
- Alert (C. Name); // Jack, returns again normally
Summary:
When calling a function using the new method. If a return value exists, this value is not returned when the returned value is a built-in JavaScript type (basic type) such as string, number, or Boolean;
When the returned value is an object, function, array, or other object types, this object, function, and array are returned.
When the returned value is a built-in type (basic type), what does new fun () return?
Bytes ----------------------------------------------------------------------------------------------------------------
Whether the function contains this is discussed. If this is not available, an empty object {} is returned. If this is available, a non-empty object is returned.
Define a function without this
JS Code
- // The returned value is of the basic type.
- Function fun (){
- Return "Jack ";
- }
- VaR c = new fun ();
- For (VAR recognition in c ){
- Alert (ASD );
- }
- Alert (c); // [object]
The returned value C is not "Jack". It can be seen that C is an empty object {} after the for in statement is executed and no attribute is output {}.
Let's take a look at the function with this. The function with this is actually writing a class. However, due to the flexibility of JS, many strange writing methods have been made.
JS Code
- // The returned value is of the basic type.
- Function fun (){
- This. Name = "Tom ";
- Return "Jack ";
- }
- VaR c = new fun ();
- For (VAR recognition in c ){
- Alert (ASD); // name
- }
- Alert (C. Name); // Tom
The returned value is not "Jack". For in outputs the name attribute. The last sentence outputs Tom, indicating that the returned value C is a non-empty object. The Return "Jack" here does not work at all.
Therefore, when the return value of a function is a built-in type (basic type), calling a function using the new method will result in incorrect results.
What if the return value of a function is an object, array, or function?
JS Code
- // This is not included. The returned value is an object.
- Function fun (){
- // Assemble an object
- VaR OBJ = {};
- OBJ. Name = 'andy ';
- OBJ. Age = 20;
- OBJ. MSG = function (){}
- Return OBJ;
- }
- VaR c = new fun ();
- For (VAR recognition in c ){
- Alert (ASD); // name, age, MSG
- }
- Alert (C. Name); // Andy
JS Code
- // Contains this, and the returned value is an object
- Function fun (){
- This. Sex = "man ";
- // Assemble an object
- VaR OBJ = {};
- OBJ. Name = 'andy ';
- OBJ. Age = 20;
- OBJ. MSG = function (){}
- Return OBJ;
- }
- VaR c = new fun ();
- For (VAR recognition in c ){
- Alert (ASD); // name, age, MSG
- }
- Alert (C. Name); // Andy
The output results of the two segments are the same. C contains the name, age, and MSG attributes, but not the sex attributes. It indicates that when the returned value is an object type (object, array, function), new will not use this to construct the object, but will directly return the assembled object.
This method is actually a factory method. In applications, more programmers write the first letter of the function name to make it look more like a class.
The first letter of the function name is capitalized to conform to the Java class naming rules.
JS Code
- /**
- * Define a function car
- */
- Function car (color, doors ){
- VaR car = {};
- Car. Color = color;
- Car. Doors = doors;
- Car. MSG = function (){
- Alert ("this is a" + this. color + "car, there are" + this. Doors + "doors .");
- }
- Return car;
- }
It looks strange. Car is clearly a class, and it is not like the previous function call. It seems a little different from the article title. But we can use () to call it. You can also use new to call it. Use the new method to make it more like the Java object creation method.
JS Code
- // Method 1
- VaR C1 = car ('red', 2 );
- C1.msg ();
- // Method 2
- VaR C2 = new car ('black', 4 );
- C2.msg ();
This method can also be seen in some JS libraries. For example, a very important function native in mootools. JS, the return value is a function (class ). The class in mootools core is returned by the native function. VaR class = new Native ({...}); of course, you can also directly use () for calling without using new.