Javascript Goodies

Functions

function declarations

function thing(p1,p2) {
  code
  return value;
}
  • Starts with word ‘function’
  • Not separately assigned to a variable
  • WILL be hoisted and declared at top

vs

function expressions

var thing = function(p1, p2) {
  code
  return value;
};
  • Assigns function with no name (annoymous) to a named variable
  • Will NOT be hoisted to top, will be defined inline

IIFE’s – immediately invoked function expressions

(function(){ /* code */ }()); // Like this
(function(){ /* code */ })(); // or this
– key identifier is surrounding parens – ()
– invoked at actual runtime

Function Overloading

Achieved with functions by examing arguments, e.g.
function abc() {
   if (arguments.length === 0) {     this code; }   
else {
     other_code;   } }

The three ways to use ‘this’ with methods in javascript functions

1 With call
some_function.call(this, "text")

A particular value for ‘this’ and specific parameters

2. With apply
some_function.apply(this, "text")

A particular value for ‘this’ and, specifically, an array for any parameters

3. With bind
some_function.bind(this, "text")
‘this’ is the value for the new function, the rest are named parameters for ‘new’

Object Properties

Common Internal Properties
  • Extensible
  • Prototype
Data Properties, i.e. values
All Data Properties also have two attribute methods:
  • value
  • writeable
or
Accessor Properties, i.e. functions
All Accessor Properties also have two attribute methods:
  • get
  • set
  e.g.
var tree = {
  _name: "Oak",
  get name() {
    console.log("here1");
    return this._name;
    },
  set name(value) {
    console.log('here2'); 
    this._name = value;
  }
};

Retrieving Property Attributes

Object.getOwnPropertyDescriptor()

Constructors and Prototypes

Constructors use ‘new’ to create a new instance of a method.

Functions should be capitalized when they are used as constructors – you will be creating new instances of them with ‘new’.

All functions have an internal prototype property
You can check for it with object.hasOwnProperty(name)

Rubyists may find its usage has some similarities to Ruby’s class level methods.

When an object (function w/new constructor) has methods that are shared by all instances use a prototype function for the Object

YourObject.prototype.yourFunctionName = function() {...}

When an object (function w/new constructor) has attribute values that are shared by all instances use a prototype attribute for the Object

YourObject.prototype.yourVariableName = value

 

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