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