Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

understand js code

Posted on 2014-04-15
8
Medium Priority
?
492 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 59

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 59

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

618 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