Monday, October 22, 2012

Variable and Function

Variable and Function definition

So now we are learning about variable, their types, type cast, function creation and calls.

Variables: It is simple to define the variables using the 'var' keyword, We can define the type at runtime i.e. it can be string, integar, array or even a function to a variable.

Interesting part is public and private variables. The public variables are kept alive even when the function has ended and available outside of the function, The private variables ends in the function and dies with the function.

Here are the ways to define public and private variables in the function

function dynFunction() {
first = 'Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private
}

You can execute the function in the script and the public variable value will remain available for usage.

See the simplicity, Ah i even created a function in the above example, Here is my hello world program which now contains the above function and test for public variable




<html>
<Head> Sabby learning scripting <br> </head>
<body>
<div id=dynHello></div>
<div id=dynVar> </div>
<div id=dynVar2> </div>
<script type='text/javascript'>
document.getElementById('dynHello').innerHTML='<p>Hello World!</p><br>'

function dynFunc() {
first='Public'; //This is public variable definition which will stay on even when function has ended
var second='Private'; // var keyword makes this private to this function
}
dynFunc();
document.getElementById('dynVar').innerHTML=first;
document.getElementById('dynVar2').innerHTML=second;
</script>
</body>
</html>

Basic Syntax and Grammar

Basic Syntax & Language grammar

Let me start with explaining what does language grammar means to me, It means to understand the keywords, symbols, pre defined methods, system calls and their usage. It matches closely with what we call as syntax in general, for example:

() = is used to define a function
{} = is used to define what the function will contain
; = Defined the end of line for the code
var = defines the symbol

As we see it can be classified as syntax usually though the whole package of these are referred as the language grammar for that programming language, Learned this when i was reading turing machine lesson in my graduation years.

So now to the point of my learning JavaScript: It has seemed pretty straight forward to learn the basic syntax that is how to do the basic hello world, It is interesting to find static and dynamic control way of doing this i.e.

document.writeln = static type of definition
alert = A bad pop out way of static definition
document item properties = Wow this is pure dynamic way of moving things around with script.

The better part is the script definition can be embedded in pure html page or called out from inside the html file to keep a central script for all your pages. Created all the three examples for hello world, pretty quick as it goes. Sample code for dynamic way (Skipping the static ones as don't like that way with dynamic scripting :p ) , Save this as html for quick check

<html>
<Head> Sabby learning scripting <br> </head>
<body>
<div id=dynHello></div>
<script type='text/javascript'>
document.getElementById('dynHello').innerHTML='<p>Hello World!</p>'
</script>
</body>
</html>

That's it, the way to define a tag, call for the tag and define the html at runtime. Amazingly simple and quick

So overall got my basics of language to create the script tag, understand the way the language works in basic and got a hold of basic syntax. Rolling on to next topic

~Sabby


JavaScript : Start learning

JavaScript: A Start

Trying my hands to learn JavaScript, A new tool for my automation and administrative work. I am thinking it will help me revive my tech skills and give me a hobby to automate certain scripts which could be useful in projects related to Cloud automation and Infra implementation projects. So here is the background and let me get started explaining what i am doing with the language.

I have created the basic outline of my learning and will learn and share experience on these in future posts


Item
Status
Comments
Basic Syntax


Basic Language grammar


Hello world


Variable definition


Function definition


Object definition


Loops (if, for and while)


Classes (if any)


Define html border and frames


Form management


Web services integration - Ajax


File creation and management


Operating system commands management


jQuery concepts


JSON concepts


Industry usage pattern


Debugging


Performance best practices