简体   繁体   中英

not able to understand this javascript code

i am new in javascript and i have to understand an appcelerator project. in appcelerator we need to code in java script.

var winForm = (function() {

    var API = {};

    API.list = [
        {
            title:'title1', hasChild:true, color:'#9B0B0B',font:'font'
        },
        {
            title:'title2', hasChild:true, color:'#9B0B0B',font:'font'
        },
        {
            title:'title3', hasChild:true, color:'#9B0B0B',font:'font'
        }
    ];//end winList

    return API;
})(); //end 
module.exports = winForm;
  1. what type of function is this?
  2. what is this '{}' initialization?
  3. what we are doing in API.list?
  4. how to call this function and list.
  5. what is this exports?

Sorry for asking so much questions in one post.

  1. It's an anonymous function declared to create scope.
  2. It creates an empty object. It has no properties, but it is defined.
  3. list is being set to an array (that's the [... ] notation) consisting of 3 objects. Each object has 4 properties, title , hasChild , color , and font , with the respective values.
  4. You cannot call this function explicitly, it's declared, run, and the result is stored in the variable winForm (which is then stored in module.exports ).
  5. Some property on the module object, whatever that is.

You should take the time to learn more about how javascript works. I recommend http://javascript.info/ as a great ramp up.

I would like to recommend some useful links, these will enrich your knowledge

1) the function is an immediate anonymous self executed expression function in the form of variable = (function() {}())

2) API is initialized to be an object (or hashtable) and its scope is inside that function

3) API.list is an array of objects, each one containing four pairs key:value

4) Function is self executed so when you return API object you assign it to the winForm variable

5) winForm is the returning object and winForm.list is the array.
Since you assign module.exports = winForm; then module.exports.list is your array

1.This function is called as anonymous function or rather you can say self executing function

2.It is creating an empty object 

3.API.list is array of the object .. To define array [ ] these brackets are used and for object { }.
4. You are using the return function .. and the result is getting stored in module.export
5. Export is the method name .. There has to be a method object define somewhere in js . you can you this method to get your result
as in the winForm  function  and used for some purpose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM