My JavaScript Article

Wednesday, May 9, 2007

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).


Comments
Francis Norton Wednesday, May 9, 2007
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.
rodrigo Thursday, May 10, 2007
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.
Will Sunday, May 13, 2007
Excellent article. I am proficient in JavaScript and understood the concepts somewhat already, but that was an extremely clear explanation of prototypical inheritance. Thanks :)
Andreas Sunday, May 13, 2007
Thanks Scott, very informative and well explained. I learned a lot from it.
Will Sunday, May 13, 2007
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.
scott Monday, May 14, 2007
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 ;)
Will Monday, May 14, 2007
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());
Will Monday, May 14, 2007
type in underscore remove the underscare in the t reference. Still works ;)
Filini Tuesday, May 15, 2007
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
Justin Wednesday, May 16, 2007
I've always found your articles very informative and helpful. This one was no exceptions. Thanks for the very educational reading.
Budigelli Thursday, May 17, 2007
Very good article. Nicely explained some of the subtle differences in client side programming and server side for C#, VB.Net developers...
Dan F Friday, June 1, 2007
Most excellent article Scott!
Erik Friday, June 1, 2007
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
Arnold Matusz Friday, June 1, 2007
Congratulations, very nice article! Thank you!
Arnold Matusz
Ron Krauter Saturday, June 2, 2007
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.
scott Saturday, June 2, 2007
Ron: I will cover that topic in a blog post. Give me about 2 weeks to work it in!
Slava Thursday, June 7, 2007
Hi Scott,

Thanks a lot for very well-explained and useful article!
This was expanded my skills in JavaScript.
G.Postma Monday, January 4, 2010
Please check out www.codeproject.com/.../Javascript_Basics.aspx

Looks fishy ...
gravatar Scott Allen Monday, January 4, 2010
@G.Postma - Sigh, that wouldn't be the first time.
Roc Tuesday, January 19, 2010
Hey Scott, the article about JavaScript is so awesome. It clarify lots of confusion in my mind from the conceptual perspective. Keep going, man!
gravatar Jay Edgar Wednesday, January 5, 2011
Scott, thanks so much for this fantastic article! While I have done some OO programming in PHP in the past, I don't do it much and don't retain the principles.

I just picked up "jQuery in Action," and the appendix assumes (too much) familiarity with OO. I found your article in a Google search, and it does a fantastic job explaining principles in a step-by-step fashion. I think I'll now be better able to grasp what jQuery is doing. And I've bookmarked your article for future reference.

Really--thank you.
gravatar scott Wednesday, January 5, 2011
@Jay - thank YOU for the great feedback.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!