My JavaScript Article

My latest OdeToCode article is online: “What ASP.NET Developers Should Know About JavaScript”. The article covers what I personally had to learn before feeling comfortable with modern JavaScript toolkits, frameworks, and libraries. It all boiled down to the following:

  1. Every JavaScript object is a dictionary.
  2. Every JavaScript function is an object.
  3. Every JavaScript object references a prototype object.

Here is the introduction:

JavaScript – It's beat up, put down, shrugged off and kicked around. Cursed by the web browser's inconsistency yet blessed by a pervasive ubiquity -it's a technology many try to disregard even when its potential is something few can ignore. If you want to write an interactive application for the web today, then you'll need some JavaScript code on your side.

This article approaches JavaScript from the perspective of an ASP.NET developer who is comfortable with the paradigms and patterns of either C# or Visual Basic. The article doesn't look at how to use JavaScript from ASP.NET exactly, but it does look at why JavaScript is so different from the two languages we commonly use with the .NET CLR. The article assumes you already know that JavaScript is a loosely-typed language (because you don't have to declare the type of data you store in a variable), and that the syntax is similar to the C family of languages (with charming curly braces and stunningly beautiful semi-colons).

Print | posted @ Wednesday, May 09, 2007 1:12 AM

Comments on this entry:

Gravatar # re: My JavaScript Article
by Francis Norton at 5/9/2007 11:28 AM

Great article, I used to do server-side development in JavaScript many years ago (www.ie.com/article.aspx) and what you describe is very much what we had to learn, plus the new developments such as JSON and AJAX.
  
Gravatar # re: My JavaScript Article
by rodrigo at 5/10/2007 6:09 PM

Great article.. but Visual Studio supports some javascript Intellisense. If you write the following code:
var d= new Date();
in a js file you will get all methods defined in the Date class . That is also true fro every native javascript class. Off course the new javascript intellisense seams to be much more complete.
  
Gravatar # re: My JavaScript Article
by Will at 5/13/2007 10:21 AM

Excellent article. I am proficient in JavaScript and understood the concepts somewhat already, but that was an extremely clear explanation of prototypical inheritance. Thanks :)
  
Gravatar # re: My JavaScript Article
by Andreas at 5/13/2007 2:40 PM

Thanks Scott, very informative and well explained. I learned a lot from it.
  
Gravatar # re: My JavaScript Article
by Will at 5/13/2007 9:28 PM

Hey Scott,
On your private member implementation I am a little concerned about scope. I am not sure if the example was illustrated correctly or now. But for example when declaring a private field in C# you will have global scope within that instance of that class (all methods can see it) however when attempting to actually attempting to test your implementation of private fields each add hoc prototyped method that was added could not see x nor y that were passed through the contructure. Instead they would have to go through the already publicly exposed get and set methods which really does not serve any value at all in that case.
  
Gravatar # re: My JavaScript Article
by scott at 5/14/2007 3:31 AM

Will: that is correct. Since JavaScript doesn't have true "private" members we simulate them with closures. It's not the same behavior as C# (or C++), but not much in JavaScript has the same behavior ;)
  
Gravatar # re: My JavaScript Article
by Will at 5/14/2007 6:57 PM

Hey Scott in follow up with Douglas's article I came up with a example which I think better illustrates private fields. At leasts "simulates" closer to the scope behavior in .NET

var obj = function CustomObject()
{
var t = 'test';

CustomObject.prototype.GetVal = function ()
{
return t;
}
}

var newObj = new CustomObject();
alert("Does not have direct access as you can see its undefined: " + newObj._t);
alert("Has access to private field: " + newObj.GetVal());
  
Gravatar # re: My JavaScript Article
by Will at 5/14/2007 6:59 PM

type in underscore remove the underscare in the t reference. Still works ;)
  
Gravatar # re: My JavaScript Article
by Filini at 5/15/2007 9:13 AM

Two other great articles by Douglas Crockford on the same subject:

The World's Most Misunderstood Programming Language
http://javascript.crockford.com/javascript.html

Classical Inheritance in JavaScript
http://javascript.crockford.com/inheritance.html
  
Gravatar # re: My JavaScript Article
by Justin at 5/16/2007 3:38 PM

I've always found your articles very informative and helpful. This one was no exceptions. Thanks for the very educational reading.
  
Gravatar # re: My JavaScript Article
by Budigelli at 5/17/2007 7:11 PM

Very good article. Nicely explained some of the subtle differences in client side programming and server side for C#, VB.Net developers...
  
Gravatar # re: My JavaScript Article
by Dan F at 6/1/2007 8:57 AM

Most excellent article Scott!
  
Gravatar # re: My JavaScript Article
by Erik at 6/1/2007 4:37 PM

A great introduction to javascript prototype programming. The next step for most readers is probably trying to get browser events mapped onto the object method, which can be a big pain until you read this: www.brockman.se/.../method-references.html.utf8
  
Gravatar # re: My JavaScript Article
by Arnold Matusz at 6/1/2007 11:44 PM

Congratulations, very nice article! Thank you!
Arnold Matusz
  
Gravatar # re: My JavaScript Article
by Ron Krauter at 6/2/2007 4:01 PM

Scott,

Could you explain more on "closures"? You have an inner function that references an outer member. When the outer function goes out of scope, the inner function still has access to those members. This could lead to memory leaks. Is my understanding correct? Thanks in advance.
  
Gravatar # re: My JavaScript Article
by scott at 6/2/2007 5:05 PM

Ron: I will cover that topic in a blog post. Give me about 2 weeks to work it in!
  
Gravatar # re: My JavaScript Article
by Slava at 6/7/2007 12:44 PM

Hi Scott,

Thanks a lot for very well-explained and useful article!
This was expanded my skills in JavaScript.
  
Gravatar # re: My JavaScript Article
by G.Postma at 1/4/2010 5:40 AM

Please check out www.codeproject.com/.../Javascript_Basics.aspx

Looks fishy ...
  
Gravatar # re: My JavaScript Article
by Scott Allen at 1/4/2010 8:18 AM

@G.Postma - Sigh, that wouldn't be the first time.
  
Gravatar # re: My JavaScript Article
by Roc at 1/19/2010 12:28 PM

Hey Scott, the article about JavaScript is so awesome. It clarify lots of confusion in my mind from the conceptual perspective. Keep going, man!
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 5 and 3 and type the answer here: