Monday, June 3, 2013

learn a word a day 6 : perambulate

So a new chapter in learn a word a day

today's new word is perambulate:

: walk or travel through or around esp for pleasure and in a leisurely walk
: walk from place to place, walk about

ex : I like to perambulate regularly around the streets.
I perambulate to clear my stupid head.

My determination levels for this project are dropping now, as you can see i was not able to write the previous words. I also notice that each of my posts varies a lot since, they are changing their pov , sometimes i or you. i hope i don't sound facetious to you. But if you are a fastidious reader you can clearly see the mistaken use of pronouns in my blog. But you also will notice the motif of writing this blog, so i hope you can forgive me. I want self as well as your impetus to continue and prolong this project. I hope you understood what i meant here.

TARGETS AND GOALS

I kept three targets till sunday, i was able to complete only one of them. That is 'learn a word a day'

I completed another target by today, so i am some what glad , i completed two of my targets. And i failed in one, that is algorithm a day.

Now i am realizing the action method stated in 'Making Ideas Happen' , i need to act first to see how the output is coming and i realized that two of the projects were easily made rather than pondering on selecting which one to complete, i tried to do all and finished two of them.

So from now on, no pondering only executing. that is the golden word or keyword whatever it can be.

So new targets, since i already completed javascript, now since i also realized how much time i am wasting, i will have to try to minimize time waste and increase my productivity this time to finish my targets.


This week targets are :

JQUERY
C, C++ PROGRAMMING : 25 PAGES A DAY
LEARN A WORD A DAY
TOPCODER PROGRAMMING ONE PROBLEM A DAY

GET A JOB -------TO VICTORY

Yes!, i finally completed my first training course. Yes, i have done one thing completely without interruption for the first time in a long time. Looks like i am getting my mojo back. But i don't feel the elation and triumph, i see much bigger obstacles ahead in my path to execution.

For the first time in my life, i am setting myself a goal in my life, i want to get a job, a good software development job. That is my goal and i will strive for it with full on determination.

This is my first win in a battle, but the war is still being waged. Only way to end the war is victory.

To victory..........

javascirpt lesson 10 -- continued

In javascript, by default all properties of an object are public.

Just as functions have local variables, which can be accessed only from inside a function. we can also have private variables in a class which we won't share publicly.

if create a variable without using 'this' keyword in a constructor function, then it is a private variable.

ex :

function Person(name){
this.name = name;
var balance = 234;
}

var sur = new Person("bob");
console.log(sur.balance); // not accessible, gives undefined output

*** Even though we cannot access private variables directly, we can access them by using a public method. this method is very similar to access a private variable.
ex:


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;

   this.getBalance = function() {
      // your code should return the bankBalance
      return bankBalance;
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();
console.log(myBalance);

A method can return another method inside a method.
ex:

this.askTeler = function(){
       return returnBalance();
   };

********** types of variables we are concerned with are 'number' 'string' and 'object'

javascript lesson 9 -- object oriented programming

when i was making a constructor in the previous lessons, i am infact making a new class.
A class can be thought of as a type, just like how 'number' and 'string' are types

when i made two objects bob and susan in previous lessons, they infact belong to class Person.

prototype keeps track of what a class can have property or method. the prototypes in javascript are automatically defined with a constructor.

*** if we want to add a method to class prototype, such that all the objects of this class can use the method. then we have to do this to extend the prototype
syntax:
classname.prototype.newmethod = function(){
            statements;
};

function Dog(breed){
this.breeds = breed;
}
Dog.prototype.bark = function(){
  console.log("woof");
};                                             // we added a bark method so other objects can use this method.

**Inheritance:
it allows one class to see and use the properties and methods of another class.

**DRY  -- Don't Repeat Yourself principle of programming.

** to inherit class1 properties to class2, we need to set the prototype of class2 to be class1.
ex:

// the original Animal class and sayName method
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is "+this.name);
};

// define a Penguin class
function Penguin(name){
    this.name = name;
    this.numLegs = 2;
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();

**the motif i noticed here is that to add any thing to a class like a method or inheritance of another class we use prototype.

* If javascript can't find something in the current class, it goes to the top of the prototype chain, to see if it from a class it inherited from.

This goes up the way upto the top Object.prototype . By default all classes directly  inherit from Object class, unless we change the class's prototype.

javascript lesson 8 very important

contructor can be thought of as a template.

For retrieving property values , we have two ways, dot notation and bracket notation

bracket notation has an advantage where we can use variable which store property names as well.

ex :
var somobj = { propname : "propval"};
var p = "propname";

somobj[p];             // this also means somobj["propname"]  , hence this is the advantage.


** sometimes we need to know the type of variable we are using, it can be 'number', 'string','function' or 'object'.

so to find this , we have a function to know this, 'typeof()'
ex :

var a = { b : "dfd"};
console.log(typeof(a));        // o/p is object

****** All objects have a useful function, which tells if an object has a certain property or not, that is 'hasOwnProperty'
ex:

return somobj.hasOwnProperty('name') ;    // returns true or false, if it has that property then true.


** for in loop can be used to print all the property names.
we can also use this to print the property values if we use bracket notation.

ex :
var somobj = { type : "indie" }

for ( var prop in somobj){
     console.log(prop);                    // print the name of property, i.e, o/p is type
     console.log(somobj[prop]);      // print the value of property


Sunday, June 2, 2013

javascript lesson 7

As we think of property in a object as a variable associated with an object.

methods in a object can be thought as functions associated with an object.

ex:
   obj.method = function(){

};

methods can be used to change property values.

if we want to use the method for other objects as well, for this we use 'this' keyword.

first we define a method

var setAge  = function(newage){
this.age = new(newage);
}

now we define our object

var obj = new Object();

obj.age = 22;
obj.setttingnewage = setAge;        //this acts as a placeholder, it will store whichever object called this
obj.settingnewage(23);             // method, so now whenever  we type obj.settingnewage(23), this.age
                                                   // in the method will refer to bob.age . here this holds obj, since obj called it.

***we can define objects inside objects in javascript.

I tried to create objects by 2nd method using 'new' keyword. here 'Object' is the constructor. it is already defined by javascript. Now i am learning that we can make custom constructors as well.

the Object function constructor creates a object with no properties and we have to manually add them.

so we create custom constructors instead.
ex:

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

so now we have 'Person' constructor, we can create person objects much easily here

var bob = new Person("bob", 22);   // properties are already defined

we can also initialize properties inside a constructor not just define them.
ex:
function Person(name, age){
this.name = name;
this.species = "Homo Sapien";
}                // we initialized the property species here.

**** Not just properties, we can also define methods directly inside constructors.

**** I know object is just like string or integer just like any variables. like we have done a array of integer and strings, we can also make a array of objects

ex : var family = new Array();
       family[0] = new Person("sur",22);
       family[1] = new Person("bur",23);


arrays filled with objects will work just like arrays filled with strings or integers.

*** we can send objects as argument to a function, just like we send int or string as arguments

Math.PI -this func gives the value of pi