Solved

understand js code

Posted on 2014-04-15
8
474 Views
Last Modified: 2014-06-01
I am trying to understand this javascript code.. I have a .js file and code is below...

 In this section of the code,
return declare([BaseWidget], {
why is BaseWidget written within square bracket?  

Whole Code :
define(['dojo/_base/declare', 'jimu/BaseWidget'],
function(declare, BaseWidget) {
  //To create a widget, you need to derive from BaseWidget.
  return declare([BaseWidget], {
    // DemoWidget code goes here 

    //please note that this property is be set by the framework when widget is loaded.
    //templateString: template,

    baseClass: 'jimu-widget-demo',

    name: 'Demo',

    postCreate: function() {
      this.inherited(arguments);
      console.log('postCreate');
    },
 onSignOut: function(){
      console.log('onSignOut');
    }
  });
});

Open in new window


pl help

thanks
0
Comment
Question by:ts84zs
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40003252
I assume declare takes an array of objects.
that is how to coerce BaseWidget into the array
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40003403
The code is used to create a new BaseWidget object.
You can read more about define here (https://github.com/amdjs/amdjs-api/wiki/AMD). The define passes (optionally an id - not in this case) and an array of dependencies that are resolved and passed to the factory.

So

define(['dojo/_base/declare', 'jimu/BaseWidget'],
function(declare, BaseWidget) {

Open in new window


The above code resolves the two values in the array and passes them in the same order to the factory function as the parameters declare and BaseWidget.

The declare function comes from dojo/_base/declare. It is passed into the factory and is used to construct an object with a superclass of BaseWidget - the object passed in as the second parameter (more here http://dojotoolkit.org/reference-guide/1.6/dojo/declare.html).

Why the square brackets? If you look at the above documentation you will see the SuperClass parameter can be an object or an array of objects (for multiple inheritance) - in this case they are using an array of 1 object - which means the square brackets are in all likelihood superflous i.e. you would get the same result for this example if you left them out.

The code is an object factory used for creating an instance of a specific class.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40003542
Leaving the square brackets out would fail if the function expects an array and loops over them
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40003585
Leaving the square brackets out would fail if the function expects an array and loops over them
If you refer to the documentation in my previous post it says.
superclass 	null|Object|Object[] 	

This parameter is either

    null (no base class),
    an object (a base class) or
    an array of objects (multiple inheritance).

Open in new window

Which implies if it is a single object it does not require brackets - but if brackets are included it will still process it as an array is also an option.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40003639
Ah, ok, then it is cleverer than I thought
0
 

Author Comment

by:ts84zs
ID: 40004228
thanks so much i am looking into it...
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question