-
1. What Is The Role Of Optimizer?
The Optimizer is a built process where your project is deployed to end user and its combines all scripts/css files together and minify them.
-
2. How To Use Require Js With Jquery?
The Require JS uses jQuery and lots of another dependency i.e.
require([‘jquery’], function ($) {
//jQuery was loaded and can be used now.
});
-
3. What Is Amd Module?
The AMD is stands for Asynchronous Module Definition and is used for defining modules and their dependencies with asynchronous manner.
-
4. What Is Data-main Attribute?
The data-main attribute is an attribute that Require JS will check to start script loading. The data-main set to the base URL for all the scripts.
Example:
–RequireJS Sample apps
-
5. What Is Module In Require Js?
Module is independent unit of program which make easy maintenance and reusability of code. In RequireJs we talk about JavaScript module.
-
6. What Is The Project Structure Of Require Js?
Project directory and the structure looks like:
–- app.js
- index.html
- lib
- modules
- template.js
- require.js
- jquery.js
- underscore.js
-
7. What Is Config Function?
The Require JS can be initialized by passing the main configuration in the HTML template through the data-main attribute.
If you want to update the Require JS configuration values with your own configurations value. You can do using the require js. Config function.
The configurations options:
–- config – This is for configuration to a module by using the config option
- baseUrl -This is the root path to start the loading of modules.
- paths – this is the path mapping for modules that don’t exists in under the base URL.
- Shims – This is use for configuration for dependencies.
- deps – array of dependencies to load
- url Args -This is the query string arguments are used to fetch all resources that are loaded by using Require JS.
- callback -It executes a function after loading the dependencies and is required when Require is specified as config object before loading Require JS.
- xhtml – It is used to create the script elements by using the document.createElement() method.
- script Type – It defines the value for script type attribute used in the document. Default type is “text/javascript”.
Example looks like:
–require.config({
baseUrl: ‘scripts/app’,
paths: {
lib: ‘../lib’
},
shim: {
‘backbone’: {
deps: [‘underscore’],
exports: ‘Backbone’
}
}
});
-
8. What Are Asynchronous Module Definition (amd) Modules?
Defining Modules:
-The Module is defined using define () method and it used for loading the module i.e.define({
country:”India”,
state: “UP”,
city: “Noida”,
userDetail: function () {
return “User Detail”;
}
});
Defining Functions:
– A module can also use a function without dependencies i.e.define(function () {
return {
country:”India”,
state: “UP”,
city: “Noida”
}
});
Defining Functions with Dependencies:
– The Dependencies module looks like.define([“../comp”, “../user”],
function(comp, user) {
return {
country:”India”,
state: “UP”,
city: “Noida”,
addUser: function() {
comp.decrement(this);
user.add(this);
}
}
});
Defining a Module as a Function:
– Its looks like.define([“../comp”, “../user”],
function(comp, user) {
return function(userName){
return userName != null ? userName :’NA’
}
});
Defining a Module with a Name:
– Its looks like.define(“Users”, [“../comp”, “../user”],
function(comp, user) {
return {
country:”India”,
state: “UP”,
city: “Noida”,
addUser: function() {
console.log(this);
}
}
});
-
9. When Should I Use Require () And When To Use Define ()?
The define () method looks like:
–- The define () method is used for facilitating module definition
- The define () method accepts two optional parameters module ID and array of required modules [dependencies]
- The define () method MUST return the implementation for your module i.e.
define(
module_id /*optional*/,
[dependencies] /*optional*/,
definition function /*function for instantiating the module or object*/
);
And
define ([‘moduleA’, ‘moduleB’], function (moduleA, moduleB) {
//define the module value by returning a value
return function () {};
});
The require () method looks like:
–- The require () method is used for handling dependency loading
- The require () function doesn’t have to return the implementation of a new module.
require([‘jquery’], function ($) {
//jQuery was loaded and can be used now.
});
Example 1:
–define( ‘MyApp’, [“marionette”], function (Marionette) {
// set up the app instance
var app = new Marionette.Application();
app.on(“initialize:after”, function(){
console.log(“initialize started.”);
});
// export the app from this module
return app;
});
Example 2:
–// Fetch and execute Marionette App
require( [“MyApp”], function (app) {
// Execute App
app.start();
});
Example 3:
–define({
“root”: {
“india”: “india”,
“australia”: “australia”,
“england”: “england”
}
});
-
10. What Are Disadvantage Of Require Js?
- No support for AMD in Node.js
- Not all modules implement AMD
-
11. What Are The Main Features Of Require Js?
- Require JS manages the dependencies between JavaScript files and improves the quality of the code and application speed.
- It is combines and minifies the modules into one script for speed-up.
- Require JS Collecting the different JavaScript files from different modules at the compilation time.
- Easy debugging
- Lesser HTTP requests
- Reduce code Complexity in large application.
- And so on.
-
12. How To Define Entry Point For Require Js?
We need to define an html file and it could be “index.html” where Require JS is loading i.e.
Require JS Sample apps
In above example, the data-main attribute of require.js will start initialization of files.
-
13. Why Do People Use Require Js? What Are The Benefits?
- It is an open source JavaScript library.
- It is AMD compatible asynchronous module loader.
- It is Asynchronous by nature.
- Lazy loaded on-demand.
- It is easily portable.
- It is Avoids global variables.
- It has ability to load nested dependencies.
- Its Dependencies are easy to identify.
- It can be easily configured.
- It can load multiple versions of the same library.
- It will make the website more optimize and speed.
-
14. How To Install Require Js?
You can install the latest release of Require JS from the command prompt using the below commands-
//Install the Require JS package
npm install -g require js
-
15. What Is Require Js?
- Require JS is a JavaScript library and AMD compatible asynchronous module loader.
- Require JS is used to loads the JavaScript files and resolve their dependencies and its supported to the latest versions of popular browsers.
- Require JS helps to improve the performance and code of quality.
- Require JS was developed by David Mark. Its open source and it was released in 2009.
- Require JS is used by node.js to fetch and load module.
- Require JS contains a small set of plugins which allow us to loading the various types of resources like text, Dom Ready, i18n, CSS and loading.
Require JS includes three main API functions:-
- Define () –
This function is used to define a module. Each module is defined a unique module ID. This module Id is used by Require JS at the runtime. - Require () –
This function is used to load dependencies and It is a global function. - Config ()
– This function is used to configure the Require JS runtime functionality.
Java Script Interview Questions
Java Script Tutorial
Angular JS Interview Questions
Ext JS Tutorial
Ext JS Interview Questions
Node.js Interview Questions
Node.js Tutorial
Java Interview Questions
Java Script Interview Questions
Java Tutorial
Koa.js Interview Questions