Sunteți pe pagina 1din 3

Javscript typing tips:

Use // for one line comments.


// This is a single line comment.

/* */ for mulitple line comments.


/* This is comment1,
This is comment2,
This is comment3 .. */
The variable declared outside any function is called global variable.
They are part of window object.
function f1(){
v1=some experession.
}
var v1;
f1();
//The above code is valid.
function f1(){
v1=something else;
}
f1();
var v1; //This is invalid.

Use window.onload to check for any features of HTML5 or to attach any event
handlers to the loaded elements.
This will prevent the need to write script tag at the end of body element.

If you use a variable that has not been declared, it returns 'undefined'
function f1(){
v1 = some expression; //gives undefined
}
If you use a variable that has been declared but not assigned any value, it returns
'null'.
function f1(){
var v1;
alert(v1); //gives null
}

When calling functions or using variables, copy the names of the function or
variable and paste them where you are using it.
(Typing the function and variable names again may cause spelling mistake).
function myFunction1(){
var myVar1;
.
.
.
alert(myvarl); //typing may cause case change and confusion in characters
like 1 or l.
}

myfucntionl(); //typing again may cause case, spelling and character changes.

To use getElementById function, copyt the text present in the id value of the
element in the html page.
<div id="myElem"><span>...</span></div>
var divElem = document.getElementById("myElem");
Use innerHTML property to get the contents of a element.
divElem.innerHTML returns '<span>...</span>'
Use outerHTML property to get the whole element itself;
divElem.outerHTML returns <div id="myElem"><span>...</span></div>
Use style property to access or change the style of an element.
divElem.style.property property can be width, height, font, background, color, ...
for sub-properties like margin-left, padding-right, text-color, access these
properties by removing - and capitalizing the first character or second word.
divElem.style.marginLeft, divElem.style.paddingRight, divElem.style.textColor

To write function, type function (keyword) then type both paranthesis (), then type
both flower brackets {}
function fnName(){}
bring the cursor back between the paranthesis, and start typing the names of the
parameters and separate them with comma ,
function fnName(param1, param2,..){}
put the cursor between the flower brackets and press enter twice.
function fnName(param1, param2,..){

}
Bring the cursor to the empty line, give a tab space and start typing new line of
code.
End the line of code with a ; (but it is not necessary)
function fnName(param1, param2,..){
var v1="abc"; var v2= 123;...
var v3= param1 (any operator) param2;
}
function fnName(param1, param2,..){
var v1="abc"
var v2=123
...
}

You can return any variable or value from a function


Return type is not required
function fnName(param1, param2,..){
var v1="abc"
var v2=123
...
return v1 or v1+v2 or "pqr" or 123;
}
to return multiple variables you have to return it in form of a object.
function fnName(param1, param2,..){
var v1="abc"
var v2=123
...
var obj ={r1:v1,r2:v2,..}
return obj;
or
var r1,r2;
return {r1:v1,r2:v2};
}
//the returned object is {r1,r2}
var ret = fnName(p1, p2,..);
alert(ret.r1, ret.r2);
the convent to create variable and function names is that
keep the first word as a verb and in small case, then capitalize the first letter
of second word, and so on..
var firstName, streetAddress, zipCode, emailAddress;
function getStudentNameAndAddress

create private variable names with _


_pvt1, _dontChange2, _internalVar,
include the javascript file in the head section using
<script src="jsfile.js">
avoid using spaces in file and folder names.

S-ar putea să vă placă și