Java Script

What is the closure in JavaScript?
A closure is a combination of a function bundle together(enclosed) With references to its surrounding state(the lexical environment). In other words, a closure gives access to an outer function scope from an inner function?

What is hoisting in Java script?
  • When we declare a variable and it is moved to the top of the code. It is Called as hoisting
  • Hoisting refers to the process by the interpreter, appears to move the declaration to the top of the code before execution.
  • Variables can be affected before they are declared in the JavaScript
  • JavaScript, only hoists declaration and not initialisation. Will be undefined until the line where is initialisation is reached.
What is the promise in Java script?
  • A promise is a Promise of court execution. 
  • The code, either executes or fails to execute In both cases, the subscriber will get notification.
  • Let promise = new Promise(function (resolve,reject){});
  • Resolve and reject are to callbacks provided by JavaScript itself.
    • Resolve(value)
    • Reject(value)
  • Promise, object has these properties
    • State
      • Initially, pending changes to either fulfilled or rejected
    • Result
      • Initially, undefined, then changes to value, if resolved, or value if rejected.
  • Promise is mostly used in network calls to API or fetch data from server etc
    • Once data is available, some action needs to be performed.
What is OOPS in Java Script?

The two important principles with OOPS in java-script are Object Creation patterns(Encapsulation) and Code Reuse Patterns(Inheritance).

How to use in class?

Javascript does not contain class statement. Javascript is a prototype based language. Javascript  uses functions as classes. Defining a class is easy as defining a function.In the example below we define a new class called as Car.
 function Car()  
 {  
 }  
 var car1=new Car();  
 var car2=new Car();  

For creating instances of any class i.e. objects use new keyword.In the above snippet we have created 2 instances of object class.

Constructor in java-script
Constructor is a method that is used to initiate the properties of any class instance.Thus a constructor gets called when any class is instantiated.
As in java-script there is no class keyword and function serves as class definition, there is no need of defining constructor explicitly. The function defined for class acts as constructor. For example in the following code snippet we have an alert statement when class Car is instantiated.

Where to load a js file in head or in body?
  • Place library script such as jquery library in head section.
  • Place normal script in head unless it becomes a performance/page load issue
  • Place script associated with includes within and at the end of that include.
  • Place scripts that impacts the render of the page at the end of the body.
  • Do not place script in the markup such as 
    •  <input onClick="myfunction()"/>  
      
    •  Better to put it in event handler in your script body instead.
  •  If you cannot decide, put it in head you have a reason as it will not block issues.
  • Yahoo exceptional performance team recommends placing scripts at the bottom of your page because of the way browsers download components.
What is 80/20 rule?
  • Helps in reducing http requests.
  • 80% of the time is spent on 20% of the code.
  • It is recommended to load HTML document first which loads fastest.
  • Caching of document reduces number of Http requests.
  • Aggregating js files.
  • Using CSS sprites
  • Browser Cache usage
    • Empty cache vs full cache page view
    • The difference of user experience must be noted down.
    • Combining scripts style-sheets
  • Browsing cookie usage 
    • HTTP cookies are used for a variety of reasons such as authentication and personalization.
What are the benefits of making an valid HTML page?
  • It is better to use valid HTML tags such as "data-ng-app" in place of "ng-app" or "data-ng-init" in place of "ng-init".
  • Document remains compliance with W3C(World Wide Web Consortium) for HTML and XML-derived Web document types.
  • Non uniform browser correction(Pages do not break in different browsers).
  • Rendering time is faster as browser does not have to look up for tags as such page loads faster.
  • Google prefers valid code(helps the page in better indexing in google).
  • A valid site is more likely to be accessible to all types of browsers.
  • Self satisfaction as validation checks do not report errors.
What the common validation errors?
  • Ampersands
    • Ampersands are written '&amp', and not just '&'
  • Quotations
    • Element property values must be put within quotes
  • Closing Tags
    • All elements must be closed off even image tags if you are using XHTML,DTD
  • Doctypes
    • Page must have a valid doctype(DTD) at the top of the page.
  • Nesting elements incorrectly
    • Elements must be opened and closed in order

Comments