| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

JS that doesn't suck

Page history last edited by Steve Donie 14 years, 1 month ago

Step 1. Learn jQuery

 

separate your concerns - what is view-like, what is validation/business logic, what is command/query/return

 

Javascript is a first class language

 

He has a pattern he likes to follow - pull things out of the form, put into a javascript object, manipulate that object, make decisions, go somewhere else or update the page.

 

JQuery and JQuery objects:

Tries to follow an MVC pattern in javascript

You have a view - it gathers the data, converts that to a javascript object, pass that to the controller

What does an object look like in javascript?

var x = { }

inside the brackets is an enum - but in javascript, enums are powerful collection-like things

so var x= { rafi : cool,

                  bubbi: not cool,

                  someoneElse: function () { // determine coolness }

                }

 

This is just a collection - a bag

Then you can do things like x.someoneElse() returns their coolness value

 

You can make variables and methods private also

 

x=function(y) {

  var _bob = djdkjdk;

  var _suzie=fu;

  return {

    joe = _bob + _suzie,

  }

}

 

the variables in that return value are now 'hidden' - act like private variables

so you can do x.joe, but not x._bob

 

One cool thing you can do is replace functions at runtime

var x = {

  rafi: cool,

  bobbi: not Cool,

  success: function(){

 }

}

 

Then later you can do:

x.success = someNewFunction() {}

if you need it to behave diferently.

 

Here's an example:

 

form val = { }

 

var controller = function(formval) {

  var x = ;

  var y = ;

return {

  redirect: function() {

  formval

}

}

 

Code organization question:

you can use the prototype keyword - he doesn't use that, causes changes to all things that derive from that prototype...

 

 

 

Comments (0)

You don't have permission to comment on this page.