Category Archives: saumya
Javascript : some things to keep an eye on Canvas
This came to me as a surprise, when we were doing the last tutorial. If you see the code in detail, we have the code below.
The above code is not on the head section of the HTML document! Rather it came after everything in the HTML document. I hope its for the Canvas does not get initialised before the code is executed. But moving the script to the last section of HTML document, it worked just fine.
I hope someone have got better explanation about the effect, but thought its better to share as this very thing took me a while to get it working. After all I am also learning now and sharing my experience at the same time.
The next thing to keep an eye is the code below on javascript
var canvas_one=document.getElementById('board_one');
The above code is pure javascript without utilising jQuery library. But the same thing can be achieved by the code below
var canvas_one=$('#board_one')[0];
The thing which went wrong for me in the beginning is that I was doing
var canvas_one=$('#board_one');
First I was hoping this should work, but it did not unless I access the zeroth element. Hopefully this also can be explained by some experienced developer, but then as we are going ahead in our learning its better to know how to get the element using jQuery library and its by accessing the zeroth element as below.
var canvas_one=$('#board_one')[0];
Hope that helps someone.
javascript : Inheritence by prototype
Once we got the basic information and know how, of the “prototype” property of a constructor object, we need some code to test it. So here is another all in one page test for the “prototype” property.
We have 3 constructors and then the prototype property of the constructors are defined as another object. Basically, if 3 constructors namely A,B,C are present, prototype of A is defined as an object created by “new B()”, prototype of B is defined as an object created by “new C()”. There are 4 different properties in all the objects. But the relationship is now as below
A.prototype is new B();
B.prototype is new C()
//so the relation is as below
A -> B -> C
That means, “prototype” allows us to make a relation between the objects. We will see that in a moment. Now all the properties of all the objects can be accessed through the object created by “new A()”.
If you have not yet opened the all in one page test for this example, open it now. And click the buttons to see the properties accessed from one Object.
var ClassA=(function(){
//------define-------
var a,b,c,d,getA,getB,getC,getD,numberToBeAdded;
//------configure------
a='Property a';
b=500;
c='Property c';
d='Property d';
//numberToBeAdded=100;//prototype object can not access this property, instead the local property will be used
this.numberToBeAdded=100;//With the use of "this" now its exposed to be accesed from its prototype object
//------expose-------
this.getA=function(){
return a;
}
this.getB=function(){
return b;
}
this.getC=function(){
return c;
}
this.getD=function(){
return d;
}
//
/*
this.getSum=function(userNumber){
return userNumber+numberToBeAdded;
}
*/
});
//
var ClassB=(function(){
//------define-------
var e,f,g,h,getE,getF,getG,getH;
//------configure------
e='Property e';
f=9;
g='Property g';
h='Property h';
//numberToBeAdded=200;
//------expose-------
this.getE=function(){
return e;
}
this.getF=function(){
return f;
}
this.getG=function(){
return g;
}
this.getH=function(){
return h;
}
//
/*
this.getSum=function(userNumber){
return userNumber+numberToBeAdded;
}
*/
});
//
var ClassC=(function(){
//------define-------
var i,j,k,l,getI,getJ,getK,getL,getSum,numberToBeAdded;
//------configure------
i='Property i';
j=300;
k='Property k';
l='Property l';
numberToBeAdded=300;//"this" exposes this number to be overriden by the object which will prototype ClassC(or this class)
//------expose-------
this.getI=function(){
return i;
}
this.getJ=function(){
return j;
}
this.getK=function(){
return k;
}
this.getL=function(){
return l;
}
//
this.getSum=function(userNumber){
//return userNumber+numberToBeAdded;
return userNumber+this.numberToBeAdded;//using "this" will override the local property and use the property of the object which would be using this object as a prototype
}
});
The code to use the prototype is defined as
//makes the object
var objC=new ClassC();
//makes the prototype magic
ClassB.prototype=objC;
ClassB.constructor=ClassB;
//makes the object
var objB=new ClassB();
//objB.constructor.prototype=objC;
//makes the prototype magic
ClassA.prototype=objB;
ClassA.constructor=ClassA;
var objA=new ClassA();
So how it works?!! This magic is simply possible by “prototype”. For example, if we are trying to access one property in an object created by “new A()” and the property is not present in the Object, then javascript engine will look for the property in the “prototype” object of “new A()” and that in our case is an object created by “new B()” and if the property is not found in it, it will search in the prototype property object defined for this object, that is in our case is an object created by “new C()”.
The code below shows, how we are accessing properties from the prototype object through the single object.
//calling own method
objA.getA();
//calling B's method
objA.getE();
//calling C's method
objA.getJ();
If you go through the code here, you must see that there is the use of the key word “this”. This is simply kind of saying that this property is accessible from outside. In Java or Actionscript kind of languages, its known as “public” variables. Remember that, whatever variables need to be exposed to outside, must be defined with the keyword “this”. Going through the code again will show you that, some properties are just defined with “var”, while some other are defined with “this”. Remember, “this” always points to the current object.
Now with these knowledge, we can very well override or reassign values to our objects, which are defined in the prototype object. That means, if some properties are defined in our “prototype” object and they are exposed or public variables, then we can again define the same variables in our Object and our object variables will be used for calculations. This is very interesting and sometimes get some time to understand too. Again, coming back to code, we have a function in our “prototype” object which adds some numeric value to another given number. Both the actual object and “prototype” object have got the value, which needs to be added. But if the prototype value is exposed with “this” then only the value inside function, which calls this variable with “this” will take actual value from the object not the value from the prototype! Thats because, “this” always refers to the current object.
Here is the source code for this example.
Hope that helps someone. Would love to listen comments, suggestions and insights, as I am still learning it.
Food for Thought : Pay attention to nonviolence
javascript : prototype
Moving on with my study of javascript, the next thing is “prototype”. Before that, lets say it three times, “Everything in javascript is an Object“.
There are only 3 primitive datatypes in javascript! Though some refer as, there are 5 types, but all in all those are the only ones. These primitive data types are
string
numeric
boolean
//The other two are
null
undefined
Apart from these basic datatypes, everything else is an Object. Though there are predefined Objects in javascript with these primitive datatype names such as String,Number,Boolean,Null,Undefined respectively. Please refer this for a detailed understanding.
Well, the point here is everything is an Object in Javascript. Unlike other programming languages where a Class is needed to create an Object, Javascript comes with Objects from the beginning. The dynamic nature of the language allows us to do so many things in so many ways that there really needs an eye who can see the details, of how things are done.
We know from past tutorial that “prototype” is a property of an Object created with “new” keyword. What does this do? Well, this allows us to link Objects internally!! Lets take a pause and understand another very important lesson of javascript and object oriented programming in general.
In a Class based language(Java or Actionscript) it is thought as, “how to relate the Classes, so that something is possible?”, where in a dynamic language such as javascript, it is thought as “how to relate the Objects, so that something is possible?” Read it for a number of times so that you will get a hang of the metaphor and thought process difference, in mind.
So the point here is the same, we have to relate the Objects itself but not the Classes(Anyway, we do not have classes here in javascript but can fake them though).
Coming back to our “prototype”, its the property of an Object created through the use of “new”. This “prototype” property is also an Object in itself. To test this, let us refer our previous post, and the all in one page test. Go down to the “Understanding Javascript class.” box and click on the “what is prototype” button. The pop-up will say, its an Object. That’s good, lets move on and click on “prototype test” button, now it alerts “undefined” that means, “prototype” is an object but not yet defined! So lets define a prototype and call the same method again. First to assign an object to the “prototype” property click on “initiate with custom prototype” button. That will assign a custom object as the “prototype” property. Now click on the “prototype test” button again, and this time it will alert with some value, other than “undefined”.
Whoa!! Its that simple. Yes it is that simple and for the same reason many confuse with the concept. After all the world has become complicated to understand simple things
The source code of the example is here to download.
Happy coding.
javascript : Making a Library
This is kind of looking at javascript from a fresh perspective. It does not assume you to know anything about it. Knowing another scripting language is always an advantage to know things better but sometimes it may come to a negative effect. If you know anything before, its better to assume you do not know that so that we can get up to speed in javascript as quickly as possible.
I hope you have a basic understanding of how to include external javascript and css files inside an HTML document. If not, then please go ahead and follow the posts mentioned below for a quick understanding.
Introduction to HTML
Getting used to the terms
Getting into web design action
Lets setup our experiment with the files listed below
index.html
general.css
saumyaJS.js
general.js
Now, the code inside index file is as below
Hello World.
Hello you, this is JS.
Here we are clearly adding all the CSS and JS files into it. The “general.js” file is where we are going to write the experiment codes. The “saumyaJS.js” is kind of a library we are going to write so as to know the javascript basics and help the experiment. The first line of javascript library includes the JQuery library, if you are not sure about it now, do not worry, just remember that I have used it to select HTML elements from my index.html file. And the JQuery library itself is a huge project,so we will get to that in a future post may be.
I am suggesting to go ahead and read Keith’s blog for getting the hang of javascript environment from a learner’s perspective. He has done a splendid job of putting his 31 days continuous blogpost about javascript.
31 days of Canvas tutorials
March is JavaScript Month. And… Introducing WireLibJS
First and the most important thing is variables in javascript. They are by default “Global”, that means if we define a variable as
a="name";
b=2;
c="message";
Then all these variables are global variables by default. That will allow anyone to access these variables from anywhere. Well, to make local variables we have to define them with “var” keyword. So the definition will go as
var a="name";
var b=2;
var c="message";
Well as you can already guess that there is no restriction in datatype for a variable. And the scope of these local variables are inside a function. In short javascript variables have function level scope. Now lets define a function and add some local variables into it,
myFunction=function(){
var d=0;
var firstName='saumya';
counter = 1;
alert('reset');
};
That means anywhere we need to call this function, we have to call it as below
myFunction();
Thats simple as we define a function with name “myFunction” and invoked it with “myFunction()”. Thats how a function is called.
Now one thing to understand and know is, its better to add as little as possible in global scope. Its a best practice and saves a ton of headache later in the development process. Now in our previous example, we are adding the variable “myFunction” in the global scope. But the variables “d” and “firstName” are in local scope. But then the variable “counter” is in global scope!! Thats the scary part of it. Yeah, anything without a “var” keyword is exposed to Global scope.
The previous example showed how to make a function with a name. It is possible to make a function without a name also, those are called anonymous functions. And since we know, to call a function we have to use “()” at the end of a function, the code below describes how to define an anonymous function and call it.
//makes anonymous function
function()
{
//Code for function
};
//make the anonymous function and call it at the same time
function()
{
//Code for function
}();//look at the "()" at the end of the function definition
The problem here is, once the function is called and executed, everything inside the function along with the function itself is not accessible any more. In order to get a reference to the function itself, we need to store it with some name. Thats where the named function comes into picture, that we have seen already as to how to define a named function.
Next we want to make an anonymous function, call it and then save the returned value of the function. A function can return value by using a “return” keyword. Example of a function which returns a value is as below
var myFunction=function(){
return 5;
}();
Now we have a variable named “myFunction” with a value of 5. This is simple but as you can imagine this return type can be anything. The code below shows how to return an “Object”.
var myFunction=function(){
return {one:'fistName',two:'lastName'};
}();
Now “myFunction” simply contains an object with properties “one” and “two”. If we want to access these properties, the call would be as below
myFunction.one;
myFunction.two;
With these knowledge we can write something like below,
var saumyaJS=(function(){
// --------------- Define -----------------------
var a,b,c,d,e;//define properties and methods
// --------------- Configure --------------------
a='saumya';
b='ray';
c=function(msg){
d++;
alert(d+':'+msg);
};
d=0;
e=function(){
d=0;
alert('reset');
};
// --------------- Expose -------------------------
return {
firstName:a,
lastName:b,
show:c,
reset:e
};//expose the properties and methods to outer world
}());
Here simply some local variables are defined, then configured them with values and finally whatever needs to be exposed to outside world, is retuned.
Whoaa!!! We have simply written a simple Javascript library.
If you look into the code a little bit, then its visible that the variable and functions inside the main function is accessible only by the names they are given at the time of returning. If one needs to access the value of “a” from outside then the call would be
saumyaJS.firstName;
But if one tries to access the value as
saumyaJS.a;
then one will get “undefined”!! This is how encapsulation works or simply, how one can hide values of an object in javascript.
This is a very basic concept of javascript, but I think this is the most important concept of javascript too.
Now we can see that whatever we want to access, we have to access through the “saumyaJS”, everything inside that function is not exposed outside or there is no more than one variable exposed to the outside world.
The source files are here to download and see for yourself. Hope that helps someone.
StartUp : Geek Mentors
It’s very exciting to see these people at geek mentors doing a nice job porting angrybirds to a local version.
Flash is not always about flashplayer, its much more than that if we really follow the philosophy of Flash community. These guys at this startup began their career in Flash, but as of any cool dude in Flash, they always tried to push the technology and dove into new platforms for the learning and broadening the horizon. They are hungry for technology and fun, both at once.
The momentum is always there for fun and learn new things all the time. The work never seem to be complete but then they never seem to rest that does not mean they are working under pressure, that means simply they keep on working for what they love. Apart from developing games for Apple appstore, there is a quality mentoring option for people who want to learn things the fun way by doing it. The best part is there is no project manager to manage you, come to work and do it by how you want to do.
There is a heavy investment on new technology learnings. When the world was fighting for whether to Html5 or Flash, this new startup solely concentrate on GameSalad. A solid platform for publishing your games to IPhone, iPad and even HTML5. The point is if there is a tool which can produce quality applications, why fight over how many lines of code one need to write in a particular platform!
GameSalad link for Angry Anna : http://www.angryanna.com/
The content below is kind of an interview with the company
Introduction of the team :
Mohd Shah Nawaz : Co-Founder,
Mohd Faisal: Co-Founder,
Himanshu Himthani: Art Director,
Abhishek Chawla: Production Lead.
Ajay Joshi: Educational Advisor
Experience till now : Don’t know what to write…..
Vision : At Geek Mentors Studios, our vision is to be the best company in the world–in the eyes of our customers, shareholders, communities, Employees and people.
Wishing the team all the best.
WordPress : backing up to Dropbox
I learned it the hard way that once you have a site up and running its better to keep a backup of the same locally in your machine. There are chances that the whole of the content might get erased!! You call it a server failure, no backup plan or anything, but the end result would be loosing all your content along with the conversations. In my quest to get a simpler and straight forward backup plan, I found this nice plugin for WordPress, called “WordPress Backup to Dropbox“. This plugin allows a direct, scheduled backup of the site to Dropbox. All one has to do is, schedule the time and frequency of backup and thats all, everything else is simply automated.
Hope that helps someone out there.
Using List component in OpenPlug
OpenPlug|Studio ships with its own SDK and though it seem like FlexSDK dependent, there are native components to work with for mobile devices. The major or the most used native component is List and one can access it as
mob:List id="allSDCardFiles"
x="0" y="0"
height="100%" width="100%"
rowHeight="45"
While it is expected to work fine and it does work fine in the simulator, but when tested on device (I have used an Android phone to test), the application simply crashed. Well the problem here is a bug in the baseSDK of OpenPlug. So till the bug is fixed the simple solution is to use a default itemRenderer. The fixed code will look as
mob:List id="allSDCardFiles"
x="0" y="0"
height="100%" width="100%"
rowHeight="45"
itemRenderer="ListItemRenderer"
That simply works every where from simulator to the device.
Hope that helps someone out there.
My first workable Android application, AFinder.
On my quest with different tools/languages, I started on OpenPlug|Studio for developing in Android. Though this is a tool for developing for multiple devices, I specifically tried this for Android for the time being.
This IDE is based on Eclipse and there are two flavors of installations. One installs it directly as a standalone IDE and you have option to install it over FlashBuilder. The development is also based upon the same component style development as in Flex SDK. There is a different name-space for mobile specific UIs. they start with “mob” rather than “mx”. From compiling to publishing as an .apk file for android device installations are quite straight forward. The best part about the IDE is its completely free. Well then it has potential as to one codebase and publish to multiple platforms. The community at OpenPlug|Studio forum seems active too and one can feel there are a lot going behind as to fix up bugs and add new features to the tool set. Though there are bugs and features to be added, but as I said, once you involve in forum, you got to know they are already working on it and new releases are happening almost in 2-3 months.
Here is my basic Android application to move around your file system on the device. Would love to listen the feedback.