Collapsed. Have you seen this js writing? Hope you can get detailed instructions and solutions

Source: Internet
Author: User
Collapsed. Have you seen this js writing? Expected details: functioninterfaceInit () {Dialog = (function () {varnow = null; return {add: function (id) {alert (id );}, g crashes. Have you seen this js writing? Hope to get detailed instructions
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 can I write such a statement? How can I call it?
What are the advantages of such writing? Not everyone can understand it!

------ Solution --------------------
InterfaceInit should be the interface in object-oriented

All classes or functions that inherit the interfaceInit interface must define the Dialog function.
------ Solution --------------------
The Dialog function contains the add and getNow methods. how can this function be called?
============
JScript code
Var init = new interfaceInit (); init. Dialog. add (); init. Dialog. getNow ();
------ Solution --------------------
Will this change be clear?
You can throw the following code into the page, and the alert will output 2.
JScript code
Script var Dialog ={}; function interfaceInit () {var now = null; Dialog = {add: function (id) {alert (id) ;}, getNow: function () {alert (now) ;}} var interface = new interfaceInit (); (function () {var dialog = diface; Dialog. add (2) ;}) () script
------ Solution --------------------
Js anonymous objects.
------ Solution --------------------
To: xuStanly
Have you tested your own code ?!

To: foolbirdflyfirst
JScript code
Script var Dialog ={}; function interfaceInit () {var now = null; Dialog = {add: function (id) {alert (id) ;}, getNow: function () {alert (now) ;}} var interface = new interfaceInit (); (function () {var dialog = diface; Dialog. add (2) ;}) () script
------ Solution --------------------
To ls:
My intention is 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 and execute

The two call methods are actually the same.

So Dialog = (function (){
Return {a: '1', B: '2'} // return an object
})();
Actually it is equivalent to Dialog = {a: '1', B: '2 '}


It can also be easy to understand.
Var a = function () {return {a: '1', B: '2 '}}
Dialog = ();
Alert (Dialog. a) // will alert 1


------ Solution --------------------
JScript code

      
      

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.