Collapsed, this kind of JS writing have you ever seen it? I hope to get a detailed guidance solution

Source: Internet
Author: User
Collapsed, this kind of JS writing have you ever seen? Hope to get detailed guidance
function Interfaceinit () {
Dialog = (function () {
var now = null;
return {
Add:function (ID) {
alert (ID);
},
Getnow:function () {
alert (now);
}
}
})();
}

Is this a function or an object? How could this be written? How do I call?
What are the benefits of writing this way? It's written by people who can't read!

------Solution--------------------
Interfaceinit should be an object-oriented interface.

All classes or functions that inherit the Interfaceinit interface are defined by the dialog function.
------Solution--------------------
The dialog function also has add and getnow two methods, how to call it?
==========
JScript Code
var init = new Interfaceinit (); init. Dialog.add (); init. Dialog.getnow ();
------Solution--------------------
Will this change the point of view?
The following code can be thrown into the page, should alert out 2.
JScript Code

------Solution--------------------
JS class anonymous object.
------Solution--------------------
To:xustanly
Have you ever written your own code for testing?!

To:foolbirdflyfirst
JScript code
 
------solution--------------------
to LS:
I meant to let LZ understand
1. ( function () {alert (1)});//define an anonymous function and execute it immediately.
2. var a = function () {alert (1);} a ();//define a variable as a function, and then call execute

The two methods of invocation are actually the same.

So Dialog = (function () {
Return{a: ' 1 ', B: ' 2 '}//returns an object
}) ();
In fact, the equivalent of Dialog = {A: ' 1 ', B: ' 2 '}


can also be easily understood a bit
var a = function () {return {a: ' 1 ', B: ' 2 '}}
Dialog = A ();
Alert (DIALOG.A)//will alert 1


------Solution--------------------
JScript code
 
------solution--------------------
This format is very normal, if you understand JS in depth, you will find that if you want to implement some of the more advanced applications, JS code can only write this, it is recommended that the landlord look at the
(that is, O ' Reilly Rhino book), focusing on the function, object-related chapters
Then, you can find a more popular JS framework to learn his source generation Code, such as prototype
  • 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.