This is an old revision of the document!


javascript

Functions in javascript can be named or anonymous. They have access to local variables that are in the scope of their parent functions.

function fun2(){
  var output = ""
    if(typeof myGlobal != "undefined"){
      output += "myGlobal: " + myGlobal;
    }
  if(typeof oopsGlobal != "undefined"){
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output)
}

You can pass around functions willnilly in javascript. You can easily pass them in as parameters to functions.

function useFunction(func1, func2){
  func1("Pass this to func1");
  func2("Pass this to func2");
}
 
var firstFunc = function(in){
  console.log(in);
}
 
var secondFunc = function(in){
  console.log(in);
}
 
useFunction(firstFunc, secondFunc);

Objecta can be defined as variables with member variables and functions inside curly braces.

var myPerson = {
    firstName : "Jack",
    lastName : "Smith",
    age : 19,
    employed : true
    fullName : function() {
      return this.firstName + " " + this.lastName;
    }
}

They can also be defined as functions by making use the of the this and new keyword.

function Person(firstName, lastName){
  // construct the object using the arguments
  this.firstName = firstName;
  this.lastName = lastName;
 
  // a method that returns the fullname
  this.fullName = function() {
    return this.firstName + " " + this.lastName;
  }
}
 
var myPerson = Person("Paul", "Sammmut");
console.log(myPerson.fullName());

Javascript was designed to run in a browser and interact with HTML.

You can embed javascript in html sources and make use of many function that can modify HTML elements.

Every object in Javascript is passed by reference. So if you push an object into a list of objects, and that object changes later on those changes will be reflected in the list.

To avoid this from happening, use this notation:

{…o}

where o is the object we want to copy.

  • javascript.1600920186.txt.gz
  • Last modified: 2020/09/24 04:03
  • by paul