Adsense

TCL regEX parsing

set Output " ---
   enabled: no
 ..."

 if {[regexp -nocase -- {enabled:[ \t]+([^\r\n]+)} $Output - defaultState]} {
puts "Default State status is: $defaultState"
}


The output is :

sh-4.2# tclsh main.tcl                                                                                                                                                                                                                        
Hello World!                                                                                                                                                                                                                                  
Default State status is: no    

Diesel vs Petrol CAR

A simple tool to compare the prices of Petrol car and Diesel car and display the breakeven point.



http://aravindavk.in/diesel-vs-petrol-car/

Prototypal Inheritance in JavaScript


var circle = {
    radius: 5,
    create: function (radius) {
        var circle = Object.create(this);
        circle.radius = radius;
        return circle;
    },
    area: function () {
        var radius = this.radius;
        return Math.PI * radius * radius;
    },
    circumference: function () {
        return 2 * Math.PI * this.radius;
    }
};

var circle2 = circle.create(10);


function Circle(radius) {
    this.radius = radius;
}

Circle.prototype.area = function () {
    var radius = this.radius;
    return Math.PI * radius * radius;
};

Circle.prototype.circumference: function () {
    return 2 * Math.PI * this.radius;
};

var circle = new Circle(5);
var circle2 = new Circle(10);

Javascript interview questions

Basic JS programmming

Scope of variable
What is Associative Array? How do we use it?
OOPS JS

Difference between Classic Inheritance and Prototypical Inheritance
What is difference between private variable, public variable and static variable? How we achieve this in JS?
How to add/remove properties to object in run time?
How to achieve inheritance ?
How to extend built-in objects?
Why extending array is bad idea?
DOM and JS

Difference between browser detection and feature detection
DOM Event Propagation
Event Delegation
Event bubbling V/s Event Capturing
Misc

Graceful Degradation V/s Progressive Enhancement

Javascript constructor example

 function Point(x, y) {
        this.x = x;
        this.y = y;
        this.dist = function () {
            return Math.sqrt((this.x*this.x)+(this.y*this.y));
        };
        this.toString = function () {
            return "("+this.x+", "+this.y+")";
        };
    }


function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    Point.prototype = {
        dist: function () {
            return Math.sqrt((this.x*this.x)+(this.y*this.y));
        },
        toString: function () {
            return "("+this.x+", "+this.y+")";
        }
    }



function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    Point.prototype.dist = function () {
        return Math.sqrt((this.x*this.x)+(this.y*this.y));
    };
    Point.prototype.toString = function () {
        return "("+this.x+", "+this.y+")";
    };


> var p = new Point(2, 2)
    > p.constructor
    [Function: Point]
    > p.constructor.name
    'Point'

JavaScript Prototype-Based Inheritance

function Shape(x, y) {
    this.x = x;
    this.y = y;
}

// 1. Explicitly call base (Shape) constructor from subclass (Circle) constructor passing this as the explicit receiver
function Circle(x, y, r) {
    Shape.call(this, x, y);
    this.r = r;
}

// 2. Use Object.create to construct the subclass prototype object to avoid calling the base constructor
Circle.prototype = Object.create(Shape.prototype);





// Declaring our Animal object
var Animal = function () {

    this.name = 'unknown';

    this.getName = function () {
        return this.name;
    }

    return this;
};

// Declaring our Dog object
var Dog = function () {

    // A private variable here      
    var private = 42;

    // overriding the name
    this.name = "Bello";

    // Implementing ".bark()"
    this.bark = function () {
        return 'MEOW';
    }

    return this;
};


// Dog extends animal
Dog.prototype = new Animal();

// -- Done declaring --

// Creating an instance of Dog.
var dog = new Dog();

// Proving our case
console.log(
    "Is dog an instance of Dog? ", dog instanceof Dog, "\n",
    "Is dog an instance of Animal? ", dog instanceof Animal, "\n",
    dog.bark() +"\n", // Should be: "MEOW"
    dog.getName() +"\n", // Should be: "Bello"
    dog.private +"\n" // Should be: 'undefined'
);





The problem with this approach however, is that it will re-create the object every time you create on. So this is not the best way to go about it. The best way is by declaring your objects on the prototype stack, like so:


// Defining test one, prototypal
var testOne = function () {};
testOne.prototype = (function () {
    var me = {}, privateVariable = 42;
    me.someMethod = function () {
        return privateVariable;
    };

    me.publicVariable = "foo bar";
    me.anotherMethod = function () {
        return this.publicVariable;
    };

    return me;

})();


// Defining test two, function
var testTwo = ​function() {
    var me = {}, privateVariable = 42;
    me.someMethod = function () {
        return privateVariable;
    };

    me.publicVariable = "foo bar";
    me.anotherMethod = function () {
        return this.publicVariable;
    };

    return me;
};


// Proving that both techniques are functionally identical
var resultTestOne = new testOne(),
    resultTestTwo = new testTwo();

console.log(
    resultTestOne.someMethod(), // Should print 42
    resultTestOne.publicVariable // Should print "foo bar"
);

console.log(
    resultTestTwo.someMethod(), // Should print 42
    resultTestTwo.publicVariable // Should print "foo bar"
);



// Performance benchmark start
var stop, start, loopCount = 1000000;

// Running testOne
start = (new Date()).getTime(); 
for (var i = loopCount; i>0; i--) {
    new testOne();
}
stop = (new Date()).getTime();

console.log('Test one took: '+ Math.round(((stop/1000) - (start/1000))*1000) +' milliseconds');



// Running testTwo
start = (new Date()).getTime(); 
for (var i = loopCount; i>0; i--) {
    new testTwo();
}
stop = (new Date()).getTime();


console.log('Test two took: '+ Math.round(((stop/1000) - (start/1000))*1000) +' milliseconds');





setTimeout and closure

Eg1

// ----- works in FF, not in IE  
var msg = "Hello World";
setTimeout(doAlert, 1, msg);
function doAlert(msg)
{
    alert(msg);
}
Eg2

// ------ works in both IE, FF
var msg = "Hello World";
setTimeout(function() {doAlert(msg);}, 1);
function doAlert(msg)
{
    alert(msg);
}

jquery: diffrent between stopPropagation and stopImmediatePropagation


                          stop   |    prevent     | prevent "same element"
                          bubbling | default action | event handlers

return false                 Yes           Yes             No
preventDefault               No            Yes             No
stopPropagation              Yes           No              No
stopImmediatePropagation     Yes           No              Yes


Example :

<span class="stop">Stop Test</span> | <span class="false">Return False</span> | <span class="none">Do None</span>| <span class="propagation">Stop propagation</span>
<div class="r"></div>

$("span.stop").click(function(e) {
    $(".r").empty().append("<h1>Stop 1</h1>");
    e.stopImmediatePropagation();
}).click(function() {
    $(".r").append("<h1>Stop 2</h1>");
    return false;
}).click(function() {
    $(".r").append("<h1>Stop 3</h1>");
});

$("span.propagation").click(function(e) {
    $(".r").empty().append("<h1>Stop propagation 1</h1>");
    e.stopPropagation();
}).click(function() {
    $(".r").append("<h1>Stop propagation 2</h1>");
    return false;
}).click(function() {
    $(".r").append("<h1>Stop propagation 3</h1>");
});
$("span.false").click(function(e) {
    $(".r").empty().append("<h1>Return 1</h1>");
    return false;
}).click(function() {
    $(".r").append("<h1>Return 2</h1>");
    return false;
}).click(function() {
    $(".r").append("<h1>Return 3</h1>");
});
$("span.none").click(function(e) {
    $(".r").empty().append("<h1>Return 1</h1>");
}).click(function() {
    $(".r").append("<h1>Return 2</h1>");
}).click(function() {
    $(".r").append("<h1>Return 3</h1>");
});

$(document.body).click(function() {
    $(".r").append("<h1>Body Click</h1>");
});


output:

Stop Test | Return False | Do NoneStop propagation

Stop 1




Stop Test | Return False | Do NoneStop propagation

Return 1

Return 2

Return 3


Stop Test | Return False | Do NoneStop propagation

Return 1

Return 2

Return 3

Body Click



Stop Test | Return False | Do NoneStop propagation

Stop propagation 1

Stop propagation 2

Stop propagation 3


Jsfiddle demo http://jsfiddle.net/markcoleman/PcUfP/

A simple Javascript program

function emp(name,age){
this.name=name;
this.age=age;
this.getEmpInfo=getEmpInfo;
}

function getEmpInfo() {
   return 'Emp name : '+this.name + '\n Emp age: '+this.age;
}

var Employee = new emp("muthunce",30);
console.log(Employee.getEmpInfo());


Output:
 Emp name : muthunce
 Emp age: 30


Using object literals

var emp = {
name : "muthunce",
age : 30,
getEmpInfo : function() {
return "Emp name : "+this.name+ " ; Emp age : "+this.age;
}
}

emp.name="muthu";
console.log(emp.getEmpInfo());

Output:

 Emp name : muthu ; Emp age : 30


Singleton using a function

var emp = new function() {
   this.name="muthunce";
   this.age = 30;
   this.getInfo= function() {
      return "Emp Name : "+this.name+" Emp age : "+this.age;
   }
}

emp.name="muthu";
emp.getInfo();

Output:
"Emp Name : muthu Emp age : 30"




Java remove empty elements from a list

List<String> list = new ArrayList<String>(Arrays.asList("", "Hi", null, "How"));
System.out.println(list);
list.removeAll(Arrays.asList("", null));
System.out.println(list);

Output:

[, Hi, null, How]
[Hi, How]

newest questions on wordpress