Solved

understand js code

Posted on 2014-04-15
8
464 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
Comment Utility
I assume declare takes an array of objects.
that is how to coerce BaseWidget into the array
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
Leaving the square brackets out would fail if the function expects an array and loops over them
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
Ah, ok, then it is cleverer than I thought
0
 

Author Comment

by:ts84zs
Comment Utility
thanks so much i am looking into it...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now