References Used


Arrays

  1. JavaScript arrays are objects, not built-in data types
  2. You can create either an uninitialized or an initialized array

    typeexampleeffect
    uninitialized array x = new Array(10) create an array with 10 elements
    initialized array x = new Array(10, 20, 30) create an array with 3 elements: 10, 20, and 30
    initialized array x = [10, 20, 30]; create an array with 3 elements: 10, 20, and 30

  3. String arrays

    1. Splitting a string into an array: array = string.split(" "): The parameter is a character on which to split the string, in this case, a blank character. You cannot use a fancy regular expression and you cannot have multiple occurrences of the character separating the strings. For example, if you have:
      a = "Brad Vander Zanden"
      then your array will be ["Brad", "Vander", " ", "Zanden"].

    2. Joining array elements to form a string: string = array.join(","): The parameter is a separator character

  4. Sorting arrays

    1. sorted_array = array.sort(): sorts array alphabetically and stores the result in a new array called sorted_array.
    2. numeric sorting or other types of sorting can be accomplished by passing to the sort method a comparison function that takes two parameters, a and b to be compared. The comparison function should return a negative number if a should appear before b, 0, if they are the same, and a positive number if a should appear after b. For example:
      function numcompare(a, b) { return a - b; } sorted_nums = nums.sort(numcompare);


Functions

  1. Declared using the keyword function
  2. Can take any number of arguments
  3. return keyword returns a value
  4. Called in the normal C/C++ manner
  5. Use var to declare a variable as local. If you simply use a variable without declaring it then it will be placed in the global name space and will remain defined after the function exits.
  6. Function pointers can be assigned to variables. For example:
    function hi_brad() { alert("hi brad!"); } a = hi_brad; a();


Objects

  1. Created using the new keyword
  2. Deleted using the delete keyword
  3. "Anonymous" objects can be created using the {} notation: a = {};
  4. Objects are somewhat like associative arrays in that properties can be dynamically added to or deleted from an object and properties can be accessed using either . (dot) or [] (bracket) notation. For example:

    new_obj = {}; new_obj.name = "brad"; new_obj['zip_code'] = 37920; alert("name = " + new_obj['name']); alert("zip code = " + new_obj.zip_code); delete new_obj.name;


Classes

  1. Javascript uses a prototype-instance model in which the prototype is an object that may be dynamically modified by adding/deleting properties and/or methods.

  2. Old-style Classes: Old-style classes are not typically used in modern javascript but you might encounter them in code and hence should know how they work

    1. Constructor functions: You create a prototype object by creating a constructor function with the name of the "class" that you wish to create. You can use the this variable to assign values to the object's properties. For example:
      function Account(cust_name, initial_deposit, date) { this.name = cust_name; this.balance = initial_deposit; this.origination_date = date; this.creditBalance = function(amount) { this.balance += amount; } } new_acct = new Account("Brad", 500, '4/10/08'); alert("name: " + new_acct.name + '\n' + "deposit: " + new_acct.balance + '\n' + "date: " + new_acct.origination_date); new_acct.creditBalance(50);

    2. Adding methods to objects: You can add a method to an object by defining a function and then assigning a pointer to the function to one of the object's properties as shown above.

    3. Prototype object: All objects have a prototype property that points to the object's prototype. If the object lacks a property, then the javascript interpreter consults the object's prototype to find the property and continues up the prototype chain until it either finds the property or the chain is exhausted. In the above example, creditBalance is duplicated in every instance of Account. A better way to have handled creditBalance would be to store it with Account's prototype object, so that creditBalance would only be stored once:
      function Account(cust_name, initial_deposit, date) {
        this.name = cust_name;
        this.balance = initial_deposit;
        this.origination_date = date;
      }
      
      Account.prototype.creditBalance = functjion(amount) {
          this.balance += amount;
      }
            

    4. Creating an inheritance chain: You can make one object inherit from another object by performing three actions:

      1. The child class calls the parent class' constructor function from the child's constructor function using the call() method.
      2. The Object.create() method copies the parent's prototype, and the new copy is assigned to the child's prototype to give the child class the same functionality as the parent class.
      3. The child class' prototype.constructor is explicitly set to the child's constructor function.

      For example:

      function CDAccount(cust_name, initial_deposit, date, interestRate) {
          Account.call(this, cust_name, initial_deposit, date);
          this.interestRate = interestRate;
      }
      
      CDAccount.prototype = object.create(Account.prototype);
      CD.prototype.constructor = CDAccount;
      
      // Add a new method to credit interest to the account
      CDAccount.prototype.creditInterest = function() {
          this.balance *= (1 + this.interestRate);
          return this.balance;
      }
      let newAcct = CDAccount("brad", 100, "2021-03-05", 0.05);
      console.log(newAcct.creditInterest());   // prints 105
      

  3. Modern Javascript Classes: EcmaScript 6 (the formal name for Javascript is EcmaScript) introduced cleaner, more C++-like syntax for creating classes and subclasses that performs the above actions for you.

    1. Creating a class: A class is declared by using the class keyword followed by a class name.
      1. methods do not need the function keyword. They are simply method names, followed by a parameter list and then {}'s.
      2. The method name constructor() is reserved for the class constructor.
      3. For example:
        class Account
            constructor (cust_name, initial_deposit, date) {
                this.name = cust_name;
                this.balance = initial_deposit;
                this.origination_date = date;
             }
        
             creditBalance(amount) {
                this.balance += amount;
             }
        }
        	  

    2. Inheritance

      1. The extends keyword allows one class to inherit from another.
      2. The subclass constructor calls super() to call the parent class constructor. super() must be called before using the this keyword in the subclass constructor.
      3. For example:
        class CDAccount extends Account {
            constructor	(cust_name, initial_deposit, date, interestRate) {
        	  super(cust_name, initial_deposit, date);
                  this.interestRate = interestRate;
            }
        
            creditInterest() {
                this.balance *= (1 + this.interestRate);
                return this.balance;
            }
        }