JavaScript is a prototype language. To turn it into a class based language adds little value? I am not talking about best-practice here. I remember reading an article from way back, which claimed that the class-based worldview is perceivably more flawed than the one of prototypes. My summary can be found here: http://carnotaurus.tumblr.com/post/3248631891/class-based-javascript-or-not. I am resisting to use the class-based jQuery add-on and other attempts at faciliating class-based JavaScript. Peer pressure is strong but is there a stronger theoretical or practical reason why I should stop resisting?
JavaScript OOP – Why Make JavaScript Class-Based?
javascriptobject-oriented
Related Solutions
OO, according to Alan Kay is all about message passing, and that's it. You will see that qualities such as polymorphism and encapsulation are actually derived from message passing.
Now that stance is in fact very extreme, because it basically means that only Smalltalk and languages alike would qualify.
I think you can define OO as building your system on entities, that fully encapsulate their state, and that are rendered exchangeable due to their inherent polymorphous qualities. One could thus argue a purely OO language ensures these two core qualities are always met. What renders OO languages "impure" would be mechanisms that allow the creation of constructs that do not meet these criteria, such as the possibilities to:
- declare public fields
- declare variables that can only hold instances of a specific class and its subclasses (i.e. variables should be typed against interfaces, which is what the biological objects communicate through in Kay's anology), which is rendered even narrower if the class in question is final, as that leaves even less room for polymorphism
- declare primitives
Then again, IMHO language purity is more a weakness than a strength. OO is not a silver bullet. No single paradigm is.
I'm not entirely sure I've identified the right problem, but if I have, this may help:
We created a class called ScriptManager
which lets us add arbitrary javascript files and/or functions to the bottom of all our pages when they are rendered. Give this a try:
public class ScriptManager
{
public List<string> scripts = new List<string>();
public List<string> scriptFiles = new List<string>();
}
public static class HtmlExtensions
{
[ThreadStatic]
private static ControllerBase pageDataController;
[ThreadStatic]
private static ScriptManager pageData;
public static ScriptManager ScriptManager(this HtmlHelper html)
{
ControllerBase controller = html.ViewContext.Controller;
// This makes sure we get the top-most controller
while (controller.ControllerContext.IsChildAction)
{
controller = controller.ControllerContext.ParentActionViewContext.Controller;
}
if (pageDataController == controller)
{
// We've been here before
return pageData;
}
else
{
// Initial setup
pageDataController = controller;
pageData = new ScriptManager();
return pageData;
}
}
}
Then, at the bottom of your _Layout.cshtml
page:
@foreach (var script in Html.ScriptManager().scriptFiles.Distinct())
{
@Html.Script(script);
}
@foreach (var script in Html.ScriptManager().scripts)
{
@Html.Raw("<script type='text/javascript'>")
@Html.Raw(script)
@Html.Raw("</script>")
}
Finally, you use it in a partial view like:
@{
Html.ScriptManager().scriptFiles.Add(Url.Content("~/Scripts/jquery.blockUI.js"));
}
You lose the ability to tell the client about the scripts in the header of the page, but they will be included on all pages you use a partial view that uses the blockUI (for example).
Best Answer
Continue to resist!
JavaScript is fine as is, it just suffers from a few misunderstandings.