?
Solved

understand js code

Posted on 2014-04-15
8
Medium Priority
?
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

Accepted Solution

by:
Julian Hansen earned 2000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 58

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

762 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