function regular() {
  log('do stuff in regular');
}

var variable = function() {  
  log('do stuff in variable');
};

regular();
variable();
withArgument(function() {
  log("do stuff in function argument");
})();

function withArgument(arg){
  arg();
  return function() {
    log("do stuff in returned function");
  };
}
var withProperty = function() {
  log("do stuff with function property - "
      + withProperty.prop);
};

withProperty.prop = 'my property';
withProperty();
/*** As a function ***/

getContext();

function getContext() {
  log('Context is window', this === window);
}
/*** As a method ***/

var sheldon = {
  name: 'Sheldon Cooper',
  myContext: getContext
};

sheldon.myContext();
    
function getContext() {
  log('Context is sheldon', this === sheldon);
}
  if (!(this instanceof Person)) {
    return new Person(name);
  }
/*** As a constructor ***/

var leonard = new Person('Leonard');
leonard.sayHi();

var penny = new Person('Penny');
penny.sayHi();

//by the way, is this correct?
var sheldon = Person('Sheldon');

function Person(name) {
  this.name = name;
  this.sayHi = function () {
    log("Hi, I'm " + this.name);
  }
}
/*** Via call/apply ***/

var howard = {};
var raj = {};

addInfo.call(howard, 'Howard', 'Wolowitz');
addInfo.apply(raj, ['Raj', 'Koothrappali']);

log(howard.firstName, howard.lastName);
log(raj.firstName, raj.lastName);

function addInfo(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}
/*** Practical call usage ***/

Array.prototype.each = function(callback) {
  for (var i = 0; i < this.length; i++) {
    callback.call(this[i]);
  }
};

var arr = [{ name: 'Leonard' }, { name: 'Sheldon'}, 
			{ name: 'Raj'}, { name: 'Howard'}];
	  
arr.each(function() {
  log(this.name);
});
Person.prototype.name = '[default]';
function Person(name) {  
//  this.name = name;
};

var p = new Person('Name');
//log(p.name);

for (var i in p)
  log(p.hasOwnProperty(i));
  log(i);


Person.prototype.sayHello = function() {
  log("Hello, I'm " + this.name);
};

function Doctor(name) {
  this.name = name;
};

//Doctor.prototype = { sayHello : Person.prototype.sayHello };
//Doctor.prototype = Person.prototype;  //updating the inherited prototype will also cause the base prototype to change
Doctor.prototype = new Person();
var rajesh = new Doctor('Rajesh');
rajesh.sayHello();

log(rajesh instanceof Doctor);
log(rajesh instanceof Person);
log(rajesh instanceof Object);
/*** Private/public members ***/

var sheldon = new Person('Sheldon', 'Cooper');
sheldon.sayHi();
sheldon.changeFirstName('Dr.');
sheldon.sayHi();

log('First name is private', 
    sheldon.firstName === undefined);
log('Full name is private',
    sheldon.fullName === undefined);

function Person(firstName, lastName) {      
  var fullName = firstName + ' ' + lastName;
  
  this.sayHi = function () {
    log("Hi, I'm " + fullName);
  };
    
  this.changeFirstName = function(name) {
    firstName = name;
    fullName = firstName + ' ' + lastName;
  };
}
/*** Using the prototype***/

Person.prototype.sayHi = function () {
  log("Hi, I'm " + this.fullName);
};

var leonard = new Person('Leonard', 'Hofstadter');
leonard.sayHi();
log('Full name is public',
    leonard.fullName !== undefined);

function Person(firstName, lastName) {
  this.fullName = firstName + ' ' + lastName;  
    
  this.changeFirstName = function(name) {
    firstName = name;
    this.fullName = firstName + ' ' + lastName;
  };
}
/*** Precedence ***/

function Person(firstName, lastName) {
  this.fullName = firstName + ' ' + lastName;  
    
  this.sayHi = function () {
    log("Hi from inside the object, I'm "
        + this.fullName);
  };
}

Person.prototype.sayHi = function () {
  log("Hi from the prototype, I'm "
        + this.fullName);
};

var leonard = new Person('Leonard', 'Hofstadter');
leonard.sayHi();
function Person(name) {
  this.name = name;
  this.changeName = function(newName) {
    this.name = newName;
  };
};

Person.prototype.sayHi = function() {
  log("Hello, I'm " + this.name);
};

function Doctor(name) {
  this.name = name;
};

Doctor.prototype = { 
  sayHi : Person.prototype.sayHi 
};
var raj = new Doctor('Rajesh');
raj.sayHi();

log('Raj is a Doctor', raj instanceof Doctor);
log('Raj is a Person', raj instanceof Person);
log('Raj is an Object', raj instanceof Object);
log('Raj can change his name', 
    raj.changeName !== undefined);
function Person(name) {
  this.name = name;
  this.changeName = function(newName) {
    this.name = newName;
  };
};

Person.prototype.sayHi = function() {
  log("Hello, I'm " + this.name);
};

function Doctor(name) {
  this.name = name;
};

Doctor.prototype = Person.prototype;  
var raj = new Doctor('Rajesh');
raj.sayHi();

log('Raj is a Doctor', raj instanceof Doctor);
log('Raj is a Person', raj instanceof Person);
log('Raj is an Object', raj instanceof Object);
log('Raj can change his name', 
    raj.changeName !== undefined);
function Person(name) {
  this.name = name;
  this.changeName = function(newName) {
    this.name = newName;
  };
};

Person.prototype.sayHi = function() {
  log("Hello, I'm " + this.name);
};

function Doctor(name) {
  this.name = name;
};

Doctor.prototype = new Person();
var raj = new Doctor('Rajesh');
raj.sayHi();

log('Raj is a Doctor', raj instanceof Doctor);
log('Raj is a Person', raj instanceof Person);
log('Raj is an Object', raj instanceof Object);
log('Raj can change his name', 
    raj.changeName !== undefined);

Demystifying JavaScript

Vlad Iliescu

JavaScript

Functions are
first-class objects

Function invocation and context

Functions can be used for object-oriented code

Functions support inheritance

Recap

Questions?

References

  1. http://ejohn.org/apps/learn/
  2. http://bonsaiden.github.com/JavaScript-Garden/
  3. http://javascript.crockford.com/
  4. http://helephant.com/2008/08/17/how-javascript-objects-work/
  5. https://developer.mozilla.org/en-US/docs/JavaScript
  6. Secrets of the JavaScript Ninja, John Resig, Bear Bibeault
  7. High Performance JavaScript, Nicholas C. Zakas
  8. http://www.jwz.org/blog/2010/10/every-day-i-learn-something-new-and-stupid/#comment-1021
  9. http://www.computerworld.com.au/article/255293/a-z_programming_languages_javascript/?fp=4194304&fpid=1

Thanks for listening!

Vlad Iliescu

#