Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Saturday, May 27, 2017

What? JavaScript?

JavaScript is a Class-Less language. So, JS Developers also classless?

JavaScript is like a Religion, people use it or misuse it, as per their convenience.

JS is not object oriented language. JS does not have class but everything is an object. JS does not have class but object is an instance of classs. So funny!

JS is a Class-Free language. JS needs an object to create an object. Then, who created first object? From where did it come?

Tuesday, November 10, 2015

JavaScript Interview Questions

When should you use var keyword in declaration and when should you not?
As a good practice, one should always use the var keyword. Not using var, creates a global variable. So, we should avoid avoiding var.
(function() {
   num1 = 1;
   var num2 = 2;
})();
console.log(num1); // outputs 1
console.log(num2); // error: num2 is not defined

What is prototypal inheritance in JavaScript?
JavaScript (ECMA5) does not have classes and does not support classical inheritance. Instead, each object can be a prototype of another object. Then, the object inherits the properties of its prototype. In below example, properties of animal are inherited by cow and the properties of cow are inherited by calf. So, calf has four legs same as cow and animal.
var animal = {legs:4};
var cow = Object.create(animal);
var calf = Object.create(cow);

Why would you include 'use strict' at the beginning of a JavaScript source file?
Using strict mode enforces stricter error checking at run time. It raises errors that would have otherwise failed silently. It helps to avoid simple mistakes like creating accidental globals, undefined values, or duplicate property names.

What datatypes are supported in JavaScript?
Number, String, Boolean, Object, Function, Null, Undefined

What is the difference between == and === ?
‘==’ evaluates equality of the value, while ‘===’ evaluates  equality of both type and value.

What is the  difference between window.onload() and the jQuery $(document).ready() method?
The window.onload method runs after all the assets (HTML, CSS, images) have loaded. The $(document).ready() runs as soon as the Document Object Model (DOM) is loaded.

How do you check if a variable is an object?
null is an object but null object is 'falsy'. So this works -
if(foo && typeof foo === "object") {
console.log('foo is object and is not null');
}

What are closures in JavaScript and how are they used?
A closure is an expression that can have free variables together with an environment that binds those variables.

What is the difference between Function as an expression and Function as a statement?
Function as an expression. Cannot be used before it is created. var add = function do(){};
Function as a statement. function do(){}; Can be called before it is created because it is pushed to top of scope.

Does JS need polymorphism?
JS parameters is English word. Arguments is a keyword. When parameters are passed to a function. JS passes one more parameter to the function called arguments. If you do console.log(arguments.length), it gives number of all the arguments passed. arguments is an array LIKE object. To fix max number of parameters inside function check arguments.length <= 1. arguments.push() does not work as arguments is not an array.

If you pass less number of parameters, JS does not complain.

If function does not have return statement, it returns undefined.

What are the different ways to create an object?
You can create and object in three possible ways -
- Object as a literal: var x = {a:'3', b:'4'};
- Object using a constructor
- Using Object.create

Whatever way and as soon as you create a function, always its prototype object gets created. In JS, an object creates another object. So, when you create a function, constructor in JS is not a concept, its just a function.

Object as a literal:
No matter how you create object with properties.JavaScript properties of an object are acually stored as an object. It works as an array of objects. To see the object run below code.
var x = {a:'3', b:'4'};
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a property readonly
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//To generate error if someone tries to overwrite this, write 'use strict'
'use strict'
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a json object
var x = {a:'3', b:'4'};
console.log(JSON.stringify(x));

//Make one property enumerable false. It does not show up in console. The property will be available by dot operator x.a but it cannot be enumerated in for loop. Using for loop it will not be available.
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {enumerable:false});
console.log(JSON.stringify(x));
 
//Object using a constructor
function foo(who){
this.me = who;
}
var a1 = new foo("james");
var a2 = new foo("bond");
console.log(a1.me);
console.log(a2.me);
 //a1.[[Prototype]] gives foo.prototype.
 //As [[Prototype]] is hidden, Mozilla introduced __proto__
 //So, function has prototype, object has __proto__
 //In IE, __proto__ does not work so use, a1.contructor.prototype
 
 

Thursday, November 14, 2013

Articulate Storyline: SCORM Reporting from WebObject to LMS

The sample zip file posted here in articulate community contains an example of WebObject sending SCORM variable to LMS. Published output has two javascript files. WebObject uses these two files.
  1. story_content/custom.js: This file acts as bridge between Storyline variables and WebObject. WebObject uses custom.js file to communicate with LMS and also to update the Storyline variables on slide for display.
  2. webobject/scripts/main.js: WebObject uses main.js for all its internal logics. 

// custom.js
// JavaScript Document

var findAPITries = 0;
var lmsAPI;
var gameScore = 0;

function findAPI(win){
while ((win.objLMS == null) && (win.parent != null) && (win.parent != win))
{
findAPITries++;
// Note: 7 is an arbitrary number, but should be more than sufficient
if (findAPITries > 7) 
{
alert("Error finding API -- too deeply nested.");
return null;
}
win = win.parent;
}
return win.objLMS;
}
function getAPI()
{
   var theAPI = findAPI(window);
   if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
   {
      theAPI = findAPI(window.opener);
   }
   if (theAPI == null)
   {
    if (_Debug) // BMD added
      alert("Unable to find an API adapter");
   }
   return theAPI
}
function init(){
lmsAPI = getAPI();
}
function GetPlayer()
{
var player = parent.GetPlayer();
return player;
}
function setVariable(varName, varValue){
var player = GetPlayer();
player.SetVar(varName, varValue);
setScormScore();
}
function getVariable(varName){
var player = GetPlayer();
gameScore = player.GetVar(varName);
console.log("gameScore=" + gameScore);
console.log("cmi.core.score.raw=" + getScormScore());
}
function setScormScore(){
var blnResult;
blnResult = lmsAPI.SetScore(gameScore, 100, 0);
console.log("Returning " + blnResult);
return blnResult;
}
function getScormScore(){
var sLearnerScore = lmsAPI.GetScore();
return sLearnerScore;
}
init();
// custom.js ends here
------------------------------------
// main.js
// JavaScript Document

function setGameScore(score){
gameScore = score;
setVariable("gameScore", gameScore);
}
function getGameScore(){
getVariable("gameScore");
}