Solved

understand js code

Posted on 2014-04-15
8
480 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 56

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript Regex 7 20
Asp response.write to clients 6 33
Jquery driving me nuts... 14 27
Collapse in/out from a "a href" each row containing different id 5 40
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

761 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