Learning better JS

I’ve been doing some js coding but at a very amateurish level and I don’t have the indepth understanding that I do with C/C++.  Right now, I’m learning to code better JS and these are the scribble notes:

1) JS is a simple language that can be put together in complex ways.

2) Everything has a prototype

3) meaning of “this” changes depending on context

Code bits (Looking at the three.js lib):

var THREE={};    //THREE is an empty object
THREE.Color = function(hex){   
//this function is an annonymous function.  It can ever only be accessed
}
//o is an object, prop is a property of o
//objects contain key value pairs, notated as key: value, key2: value
var o={prop: 6};    
//Prototypical inheritance
//the prototype is like a class.  Its like member functions for
//c++
THREE.color.prototype = {
  setRGBA: function(r,g,b,a){
     this.r=r;
     ...
  }
  setHex: function() {
   ...
  }
}
//
THREE.Rectangle = function(){
  var _left, _top...;  //private variables, not accessible
                       //outside
  function resize(){
      //this function has access to things in here
      //and things outside resize
      _left=...;
  }
  this.getX=function(){
    return _left;
  };
}

//object literal creation:
intersect = { prop1: x, prop2: y...}

//call() is in the object prototype.  sets the meaning of
//this
Three.Geometry.call(this);

From JQuery:

(function(window,undefined){
})(window);
same as:
function init(window,undefined){
}
init(window);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s