Methods, method references, and parameters in JavaScript

Source: Internet
Author: User
Tags anonymous

First of all, let's take a look at a piece of code that, if you don't understand it, is good for you:

var player = function (e) {

Return (function f (m) {

return m? (function (n) {

return $ (' #Player ', N). Get (0) | | F ($ (' iframe ', N). Get (0));

}) (m.contentwindow.document): null;

}) ($ (e). Get (0));

};

The knowledge points involved in this code include: methods, anonymous methods, immediate execution of anonymous methods, jquery objects, and the reciprocal conversion of Dom objects. Now, let's go back to basics and try to get a sense of the methods, method references, and parameters in JavaScript.

One: Sample code

First, look at the test code:

  

  

  

  

  

  

The execution result of the 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

Second: the example code explanation

1: object, i.e. new

Since P0 is a function, it can be new,function itself as a constructor. However, because the p0 itself has no parameters, methods or return, so A is basically useless.

Image

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

B ("BB");

It says that because B is a function, it can be new

Image

2: Anonymous method and Immediate execution

P01 return itself is an anonymous method of immediate execution,

So unfortunately, D itself becomes nothing, the following code cannot be executed

D ("DD");

Unlike B,b, which is the function itself, you can perform:

Image

P1 defines an anonymous method and executes it immediately, and the description does not return (or returns null), so:

1:P1 can not be new, that is, new P1 ();

2: Also cannot execute, namely P1 ();

Image

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

1:p11 itself represents the method of return, so it can be executed, i.e. P11 ("x");

2: Because P11 itself is a function, it can be new

Image

Now, for ease of viewing, the previous annotated version:

  

  

  

  

  

  

  

  

Above, basically clarified all the JS method how to use the practice. If you seriously appreciate the code above, and understand the output, then basically for the JS method of application can meet the day-to-day development.

Three: An actual code

Now, we can easily see what the following code means:

var learning = (function ($) {

var player = function (e) {

Return (function f (m) {

return m? (function (n) {

$ (' #Player ', n) refers to: Looking for #player objects in DOM object n

return $ (' #Player ', N). Get (0) | | F ($ (' iframe ', N). Get (0));

}) (m.contentwindow.document): 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 that executes as the page DOM is loaded, which acts as the main method of the page. In this code, the Learning.load method is invoked and two parameters are passed in. And then we execute a method that is:

var P1 = Learning.player ($ (' #player_tiny '));

As we can see, this method itself is defined as:

var player = function (e) {

Return (function f (m) {

return m? (function (n) {

$ (' #Player ', n) refers to: Looking for #player objects in DOM object n

return $ (' #Player ', N). Get (0) | | F ($ (' iframe ', N). Get (0));

}) (m.contentwindow.document): null;

}) ($ (e). Get (0));

};

Executes the method, executing an immediately executing anonymous method that returns the following:

M? (function (n) {

$ (' #Player ', n) refers to: Looking for #player objects in DOM object n

return $ (' #Player ', N). Get (0) | | F ($ (' iframe ', N). Get (0));

}) (m.contentwindow.document): null;

The method itself looks so complex because there is a recursive call (although the recursive call can be removed), the F method inside, and the F method itself. Perhaps, we change it to a less advanced writing, it is easy to understand a lot:

var finddoc = function (n) {

return $ (' #Player ', N). Get (0) | | Findx ($ (' iframe ', N). Get (0));

};

var findx = function (m) {

return m? Finddoc (m.contentwindow.document): 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.