Wednesday 10 April 2013

JavaScript - Code Snippets - Adding Elements in Reverse Order

HTML:
<div id="parentElement"></div>


SCRIPT:
// Get a reference to the element in which we want to insert a new node
var parentElement = document.getElementById('parentElement');

for(var i = 1; i < 5; i++) {
    // Get a reference to the first child
    var theFirstChild = parentElement.firstChild;
 
    // Create a new element
    var newElement = document.createElement("div");
    var txtElement = document.createTextNode("Hello " + i);
 
    newElement.appendChild(txtElement);

    // Insert the new element before the first child
    parentElement.insertBefore(newElement, theFirstChild);
}

Friday 29 March 2013

JavaScript - Predict the Output - Question 5


    var bar = {
       "a": function () {
            alert("Hello");
       }
    };
    
    function foo() {     
         Object.prototype.a = 'Set from prototype';     
         return function inner() {
             alert(bar.a());
          }     
    }

foo()();

Thursday 14 February 2013

Tuesday 12 February 2013

JavaScript - Predict the Output - Question 3


"use strict";

var employee = {
    name: 'Amnal'
};

Object.defineProperty(employee, 'age', {
    value: 28,
});

for(var x in employee)
{
    console.log(employee[x]);
}

"use strict";
var employee = {
    name: 'Amnal'
};

Object.defineProperty(employee, 'age', {
    value: 28,
});

for(var x in employee)
{
    console.log(employee[x]);
}

Monday 4 February 2013

JavaScript - Predict the Output - Question 2


Object.defineProperty(this, "MAX_SIZE", {
  value: 100
});

alert("OLD VALUE "+ MAX_SIZE); 

MAX_SIZE = 200; 

alert("NEW VALUE "+MAX_SIZE);

What would be the output & why?

Tuesday 29 January 2013

JavaScript - Predict the Output - Question 1

"use strict";
function strict() {
  "use strict"; // top of the function
  return {
    withStrict: function(){
      return this; // undefined
    }(),
    withoutStrict: Function("return this")()
  };
}
var really = strict();
really.withStrict; 
You must specify the reason too.