About Me

My photo
Talk to me... you will know

Friday, February 17, 2012

Inheritance in Javascript



Javascript has grown a lot from what i remember of working on it.. I remember the plain javascript i used to work on which now has advanced a lot to include inheritance and other OOP concepts. To understand more i tried an example to understand its reaches...

It's extremely hard to understand for those with classic OOP background but JavaScript has no Classes, just objects, where functions are first class objects but still objects!
Assumed this, we can try in any case to think about a JavaScript Class as a "function which aim is to create instances of function itself".
try with Mootools or some other library
new Class({}) instanceof Class;
 FALSE, since Class returns a function
 and not the created instanceof Class
 Nothing more and nothing less. The fact we would like to use that function to create and initialize instances is just how every Functions work, or if we prefer, a developer convention and a quick way to chain a prototype into an object.
 Since a Class is a Function, and whatever "new Class()" will always be an "instanceof Function", Class.prototype should be exactly the Function.prototype one, so that nobody can ever say again: "Look, I have created a Class".. All that has been made is a function..  But its still elastic




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Page Start</title>
<script type="text/javascript">
/*Class declared: Person*/
Person = function(id, name, age){
    this.id = id;
    this.name = name;
    this.age = age;
    alert('Accepting Person Object');
}
/*properties of person class*/
Person.prototype = {
    wakeUp: function() {
        alert('I am awake');
    },
    getAge: function() {
        return this.age;
    },
    
    toStringHere : function() {
        return "id : "+this.id+", name : "+this.name+", age : "+this.age;
    }
}


Person.prototype.fly = function(){
alert("finally i can fly");
}
/*An instance to help inherit the properties*/
Inheritance_Manager = {};
/*DOM extension mechanism || Simple extension through object*/
Inheritance_Manager.extend = function(subClass, baseClass) {
    function inheritance() { }
    inheritance.prototype = baseClass.prototype;
    subClass.prototype = new inheritance();
    subClass.prototype.constructor = subClass;
    subClass.baseConstructor = baseClass;
    subClass.superClass = baseClass.prototype;
}
/*Class Declared : Manager */
Manager = function(id, name, age, salary) {
alert('Registering Manager');
    Manager.baseConstructor.call(this, id, name, age);
    this.salary = salary;
    alert("Registration Complete");
}
/*Manager Inherits Person and its handling of constructor*/
Inheritance_Manager.extend(Manager, Person);


Manager.prototype.lead = function(){
   alert('I am a good leader');
}
/*use objects to check how the various functions are accessed*/
var p = new Person(1, 'RAS', 22);
document.write(p.toStringHere()+"<br>");
var pm = new Manager(1, 'Ajo Koshy', 22, '27000');
document.write(pm.toStringHere()+"<br>");
</script>
</head>
</html>

Tuesday, February 07, 2012

Escape Sequencer


To access json strings which have been returned with a double quote often poses a problem on the server side as the characters are not in sync with the values and cannot be processed... So the following problem will help you process the same problem by escaping the character in the json data daved in a file.


//buzz.txt



{"groupOp":"AND","rules":[{"field":"tlc_product_number","op":"eq","data":"2563"},{"field":"tlc_product_desc","op":"eq","data":"1" SPECIAL LABEL (CATERING)"},{"field":"fobsdate","op":"eq","data":"02/01/2012"}]




// EscapeSequencer.java




package com.string.regex;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;






public class EscapeSequencer {
public static void main(String args[]){
List<Integer> ind1= new ArrayList<Integer>();
List<Integer> ind2= new ArrayList<Integer>();
String val= "";
try {
val= readFileAsString("C:/buzz");
System.out.println(val);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int pro=val.indexOf("data\":\"")+6;
String word1 = "data\":\"";
String word2 = "\"}";
String value="";
String filterVal=val.substring(pro,val.indexOf('"', pro));
for(int i=0;i<val.length();i++)
{
if(i+word1.length()<=val.length()){

String compString = val.substring(i, i+word1.length());
if(compString.equalsIgnoreCase(word1))
{


i=i+word1.length();
ind1.add(i);


}



}
//System.out.println(ind1.toString());


}
for(int i=0;i<val.length();i++)
{
if(i+word2.length()<=val.length()){
String compString= val.substring(i,i+word2.length());
if(compString.equalsIgnoreCase(word2))
{
i=i+word2.length();
ind2.add(i);
}
}
//System.out.println(ind2.toString());
}


System.out.println(val.indexOf("\"}"));
for(int j=0;j<ind1.size();j++){
try{
filterVal=val.substring(ind1.get(j)+1,ind2.get(j)-2);
//System.out.println(val.indexOf("\"}"));
}
catch(StringIndexOutOfBoundsException sie)
{
sie.printStackTrace();
}
value=filterVal;
//System.out.println(value+"...");
filterVal=filterVal.replace("\\", "\\\\")
   .replace("\"", "\\\"")
   .replace("\r", "\\r")
   .replace("\n", "\\n");
val=val.replace(value, filterVal);
System.out.println(val);
}


}
private static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

There was an error in this gadget