点击句子查看译文 显示原文      显示译文      双语对照    源址


Well, it's been a while since i posted and it boils down to the holidays.there's nothing like the holidays to throw your whole life out of whack.if you are like me, then you keep your life as organized as possible and it's the only reason you are able to function.and that got me thinking about this blog post and gave me an idea.

I've been preparing some articles on MVC, and even a presentation.something i likely will be taking to code camps, and presenting to even my students at HACC.and that got me thinking about how one of the greatest strengths of MVC is the flexibility and ability to organize code into self contained building this out, we can organize our code into separate pieces of classes and navigation controllers.we can isolate code and maintain loose coupling to make for more testable code.

Now i can't tell you how many times i've seen wonderful code fall apart.too many times, i've seen people say"we're using MVC"and say that like it guarantees that your code is not only using current technologies but is correct and architected properly.MVC is a framework, just like any other, and that means that just like any framework, it is only as good as the design of the code and the work of the programmers.if there was a technology that guaranteed good code, we'd all be out of a job.

Now that being said, there are lots of developers out there who leverage MVC correctly.they use the proper separation of concerns.they set up their controllers, using a logical grouping of the controllers and controller actions.but that is a whole other post on to itself, and the ways MVC can be abused.

But all that aside, the one place i see the most abuse of basic structure and patterns is JavaScript.for example, let's say you write code like the following in partial view :

<div>Number 1 : <inputtype="text"id="Number1"/><br/>Number 2 : <inputtype="text"id="Number2"/><br/><inputtype="button"id="btnAddNumbers"onclick="AddNumbers()"value="Add Numbers"/></div>

Now like any good developer, you put your JavaScript code in a separate JS file called" Add.js".and it contains the following code :

function AddNumbers() {var num1 = parseInt($("#Number1").val());var num2 = parseInt($("#Number2").val());var total = num1 + num2;

Now to most of us, this looks like perfectly fine JavaScript code.nothing really special here. when you go to add this to a page, you add a reference to the JS file in the head tag of your page.and all is well, right?

Not quite... see right now, the JavaScript code above is being declared in what is called the Global namespace.Meaning that the code is being declared at the page level, and everything in this function is available to the entire page.

The question is, what happens when another developer writes a partial view with the following HTML :

<div>Number to increment:<inputtype="text"id="NumberValue"/><br/><inputtype="button"id="btnAdd"value="Increment Number"onclick="AddNumbers()"/></div>

And they write the following function :

function AddNumbers()
 var num = parseInt($("#NumberValue").val());
 num = num + 1;

And they declare their JS at the bottom of the page, thinking that it would load at the end, and insert their partial view into the same view as yours.the question is what is going to happen now?there are two functions declared with the same name.if this were server side code, we would get a compilation error.

But seeing that this is JavaScript, if i entered a"1"in the" Number 1"textbox, and a" 2"in the" Number 2"textbox, and clicked the" Add Numbers"button, i would get a" NaN"in the" NumberValue"textbox.

Now why is that? because when the browser loads the page, it does so from the top down, which means that it replaces the" AddNumbers"function with the one that does the incrementing.this is because JavaScript handles functions in much the same way as by declaring it in the global namespace, you are allowing for the potential that your code could be replaced by someone else's.

So what can you do?to be honest, the best option is to scope your JavaScript so that it doesn't sit in the global namespace, and presents this type of do that by treating a function just like an object, and modify your code in the following manner.

function CalcMath() {var addNumbers = function () 
 {var num1 = parseInt($("#Number1").val());var num2 = parseInt($("#Number2").val());var total = num1 + num2;
 }return {
 AddNumbers : addNumbers

This creates an object called" CalcMath", and a"method"off of that object called" AddNumbers".and this allows me to update the button to have the following :

<div>Number 1 : <inputtype="text"id="Number1"/><br/>Number 2 : <inputtype="text"id="Number2"/><br/><inputtype="button"id="btnAddNumbers"onclick="CalcMath().AddNumbers()"value="Add Numbers"/></div>

Now, if someone adds a function called" AddNumbers", it has no affect on your partial view.this allows for everything to say modular, and separated and keeps other code from impacting your code in the future.

Many of the JavaScript frameworks out there (Angular, Knockout, etc.) are based on this principle, and use it as a foundation for the other benefits they provide.

So that's it for right now, more to come about the presentation, and more specifically more to come on resources for startups, and articles on lessons learned from my own current startup work.