Methods, method references, and parameters in JavaScript

Source: Internet
Author: User

First, let's look at a piece of code. If you don't understand it, this article will be helpful to you:

Var player = function (e ){

Return (function f (m ){

Return m? (Function (n ){

Return $ ('# player', n). get (0) | f ($ ('iframe', n). get (0 ));

Registrant metadata (m.contentcontactdoc ument): null;

}) ($ (E). get (0 ));

};

Knowledge points involved in the Code Section include methods, anonymous methods, immediate execution of anonymous methods, conversion of JQuery objects and Dom objects. Now, let's look back at the basics and try to get a clear idea of methods, method references, and parameters in JavaScript.

I. Sample Code

First, first look at the test code:

  

  

  

  

  

  

The execution result of this Code is:

======================================

Self: outp0

Self: p0

======================================

Self: outp00B

Newo: BB

Newo: outp00C

======================================

Self: outp01D

Newo: p01

======================================

Self: p1

======================================

Self: p11

Newo: outp11_2p11

Newo: outp11p11

======================================

Self: p12

Newo: outp121

Invoker: p12outp122

Ii. Demo code

1: object, that is, new

Since p0 is a function, it can be new, and the function itself is equivalent to the constructor. However, since p0 itself has no internal parameters, methods, or return, A is basically useless.

Image

Because p00 returns a function, B can execute itself, that is, it is called;

B ("BB ");

As mentioned above, because B is a function, it can be

Image

2: anonymous method and immediate execution

P01 return is an anonymous method that is executed immediately,

Unfortunately, D itself becomes nothing, and the following code cannot be executed

D ("DD ");

Unlike B, B is the function itself, so you can execute:

Image

P1 defines an anonymous method and executes it immediately. It indicates that no (or null is returned) is returned, so:

1: p1 cannot be new, that is, new p1 ();

2: it cannot be executed, that is, p1 ();

Image

P11 defines an anonymous method, executes it immediately, and returns a method, so

1: p11 itself represents the return method, so it can be executed, that is, p11 ("x ");

2: Because p11 is a function, you can use new

Image

Now, for ease of viewing, the previous version with a comment added:

  

  

  

  

  

  

  

  

The above describes how to use all Js methods. If you carefully understand the above Code and understand the output, then basically the application of JS methods can meet the daily development requirements.

Iii. Actual code

Now, we can easily understand the meaning of the following code:

Var learning = (function ($ ){

Var player = function (e ){

Return (function f (m ){

Return m? (Function (n ){

// $ ('# Player', n) indicates the # Player Object in DOM object n.

Return $ ('# player', n). get (0) | f ($ ('iframe', n). get (0 ));

Registrant metadata (m.contentcontactdoc ument): null;

}) ($ (E). get (0 ));

};

Var playing = (function (){

Return {

Current: function (){

Return chapter;

},

Tiny: function (e ){

TinyObj = e;

},

Body: function (e ){

BodyObj = e;

}

}

})();

Return {

Player: player,

Load: function (options, fn ){

Playing. tiny (options. tiny );

Playing. body (options. body );

}

};

}) (This. jQuery );

$ (Function (){

Learning. load ({

Tiny: $ ('# player_tiny '),

Body: $ ('# player_body ')

},

Function (e ){

});

Var p1 = learning. player ($ ('# player_tiny '));

Var p2 = learning. player ($ ('# player_body '));

If (p1) p1.pause ();

If (p2) p2.pause ();

});

In the above Code, the last method call refers to the Code executed after the page dom is loaded, which serves as the main method of the page. In this Code, the learning. load method is called and two parameters are passed in. Then, we executed a method, which is:

Var p1 = learning. player ($ ('# player_tiny '));

We can see that this method is defined:

Var player = function (e ){

Return (function f (m ){

Return m? (Function (n ){

// $ ('# Player', n) indicates the # Player Object in DOM object n.

Return $ ('# player', n). get (0) | f ($ ('iframe', n). get (0 ));

Registrant metadata (m.contentcontactdoc ument): null;

}) ($ (E). get (0 ));

};

Execute this method and execute an anonymous method that is immediately executed. This method returns the following:

M? (Function (n ){

// $ ('# Player', n) indicates the # Player Object in DOM object n.

Return $ ('# player', n). get (0) | f ($ ('iframe', n). get (0 ));

Registrant metadata (m.contentcontactdoc ument): null;

This method itself looks so complicated because there is a recursive call (although this recursive call can be removed), that is, inside the f method, the f method itself is called. Maybe we can replace it with a less advanced writing method, so it is easy to understand:

Var findDoc = function (n ){

Return $ ('# player', n). get (0) | findX ($ ('iframe', n). get (0 ));

};

Var findX = function (m ){

Return m? Finddoc(m.contentdomaindoc ument): null;

};

Var player = function (e ){

FindX ($ (e). get (0 ));

};

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.