Skip to content

ioanis/javascript-guide

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 

Repository files navigation

Code Functions As Much As Possible.

  • Create functions that does only one thing.
  • Avoid declaring functions with var statement
foo()
bar()

function foo(){}
function bar(){}

Compose Large Abstractions From Small Modules

  • Create modules that does one thing.
  • Create repositories for modules and publish/open source them on NPM and Github if possible.
  • Never couple your code with any dependency doing more than it should
  • Avoid frameworks

A good read about this topic: http://substack.net/many_things

Write Declarative Code

  • Write less code
  • Write code that tells what it does briefly
  • Choose simple & short variable names

NPM Is Everywhere

  • Modularize your JS in CommonJS.
  • Use browserify
  • Avoid other module systems like RequireJS.

Require & Export on Top

var foo = require('foo')
var bar = require('./bar')

module.exports = {
  qux: qux,
  corge: corge
}

function qux(){}
function corge(){}

Avoid new, prototype and this

Functions returning objects will let you avoid maintaining "this" arguments and also let you benefit from functional programming.

var smith = Child('Joe', 'Smith')

function Child(parent, name){
  return {
    parent: parent,
    name: name
  }
}

And keep your code available for functional programming:

var JoeChild = partial(Child, 'Joe')

var smith = JoeChild('Smith')
var kat = JoeChild('Kat')

smith.parent
// => Joe

kat.parent
// => Joe

Declare Variables Outside of Statements

Declaring variables inside of a statement will make your code work and look less consistent. For example, below code will fail when you run it:

foobar

With the error of "ReferenceError: foobar is not defined". But following code will not fail;

foobar

if (false) {
  var foobar
}

For having better consistency, declare the variable on top of your statement;

var foobar

if (false) {
  foobar = whatever
}

While Loops Are Faster And Simpler

While loops are faster than for loops, and they're much simpler. Here is an example;

var i = 10

while (i--) {
  console.log(i)
}

This will output from 9 to 0. If you'd like the other way:

var i = -1

while (++i < 10) {
  console.log(i)
}

About

Azer's JavaScript Guide

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published