User talk:ZhiqiangCai1958

From Wikipedia, the free encyclopedia

[edit] Javascript for a C# programmer -- Basic section.

ZhiqiangCai1958 17:21, 11 February 2007 (UTC) I have been developing webforms using C# for 5 years. Although occasionally I used javascript, I did not really have any over all idea about it. This sunday morning, I determined to have a quick scan of an online tutorial of javascript from the W3School tutorial site. I finised the basic section in about an hour. Before I continue, I would like to write down what is new to me, I mean, what looks new to a C# programmer in javascript.

1)Script tag. A piece of javascript can be in head section, body section or on an external file. The tag just tells where a javascript begins and where it ends. It may also be used to find where the external file is located. It is simple. Yet it is the frist thing I have to remember and open the door to the language. Once I got familiar with this tag, I feel like I am an expert of it already, because the rest of the contents are so similar to C# that I only need to pickup what is Different from C#.

2) Semicolon is optional: I do not have to know this. I can simply write javascript code as C# and separate statements by semicolons. However, ignoring the semicolons may make me look more like a javascript expert -- see, I know it is optional!

3) Variable declarations: You always use var to declare a variable and do not put a type such as int, float, string, bool ect. I think the compile need to figure out the type of a variable by its use in the code. I am wondering if I can use a variable for multiple types. For example, sometimes it is a string and sometimes it is an int. Probably not. Well, why should I bother myself on that. I do not even want to have a try, though it is so eay to try it out.

4) Global vs. Local. Nothing new. Global variable-- declared out of functions. Local variables-- declared inside functions.

5) Statements: easy. Write them as you are using C#.

6) Call a function by event: The simple way is like: onClick="somefunction()". Sometimes, it can be like: onClick="return somefuction()", or even more complex: onClick="allert(...), return false". I think the return value is to tell whether or not a form should be submitted.