Post · 5 min read· pp. 265–269

JavaScript in small bites, Part 2

Forms

Forms are a natural and convenient way to interact with the user. A simple greeting program reads the value out of a field, and changes the value of an element elsewhere:

[form-interaction.html]

text
<html>
 <body>
  Hello, <span id="my-name">User</span>!
  Enter name: <input type="text" id="user-name" value="My Name" />
  <input type="button" id="greet-button" value="Greet Me" />
  <script>
   function PerformGreeting () {
    document.getElementById("my-name").innerHTML =
     document.getElementById("user-name").value ;
   }
   var GreetButton = document.getElementById("greet-button") ;
   GreetButton.onclick = PerformGreeting ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/form-interaction.html

This program shows a basic model for interactive pages: add an id to every element in the HTML that will act as either an input or an output, and then use event handlers to manipulate the page. JavaScript can interact with the HTML through the document.getElementById method.

Constructors

It's tedious to construct objects with the same properties by hand. For example, you might want to create a lot of space ships. Each might have an x coordinate, a y coordinate, an x speed and a y speed:

[object-creation-hand.html]

text
<html>
 <body>
  <script>
   var ship = {x : 10, y : 20, vx: 3, vy: 6} ;
   ship.speed = Math.sqrt(ship.vx * ship.vx + ship.vy * ship.vy);
   document.writeln("ship['speed']= " + ship.speed) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/object-creation-hand.html

With this approach, the speed of the ship has to be manually calculated every time. A better approach would be to create a SpaceShip that build space ships, and which that automatically calculates speed:

[object-creation-constructor.html]

text
<html>
 <body>
  <script>
   function SpaceShip(x,y,vx,vy) {
    this.x = x ;
    this.y = y ;
    this.vx = vx ;
    this.vy = vy ;
    this.speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
   }
   
   var ship = new SpaceShip(10,20,3,6)  ;

   document.writeln("ship['speed']= " + ship.speed) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/object-creation-constructor.html

The keyword this in the constructor refers to the current (newly allocated) object.

Methods

It is poor design to hard-code the speed of a space ship as a field; the speed of a ship is a function if its velocity. A better approach is to define the speed of a ship using an ordinary function:

[methods-as-function.html]

text
<html>
 <body>
  <script>
   function SpaceShip(x,y,vx,vy) {
    this.x = x ;
    this.y = y ;
    this.vx = vx ;
    this.vy = vy ;
   }
   
   var ship = new SpaceShip(10,20,3,6)  ;

   function Speed(ship) {
    return Math.sqrt(ship.vx * ship.vx + ship.vy * ship.vy) ;
   }

   document.writeln("Speed(ship) = " + Speed(ship)) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/methods-as-function.html

JavaScript provides a way to associate functions with objects as "methods." A method is allowed to access the fields of its associated object using the this keyword. To turn a function into a method, one can just assign a function to a field:

[methods-from-functions.html]

text
<html>
 <body>
  <script>
   function SpaceShip(x,y,vx,vy) {
    this.x = x ;
    this.y = y ;
    this.vx = vx ;
    this.vy = vy ;
   }
   
   var ship = new SpaceShip(10,20,3,6)  ;

   function SpeedMethod() {
    return Math.sqrt(this.vx * this.vx + this.vy * this.vy) ;
   }

   ship.speed = SpeedMethod ;

   document.writeln("ship.speed() = " + ship.speed()) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/methods-from-functions.html

Of course, we could also assign the method during object initialization:

[methods-during-initialization.html]

text
<html>
 <body>
  <script>
   function SpaceShip(x,y,vx,vy) {
    this.x = x ;
    this.y = y ;
    this.vx = vx ;
    this.vy = vy ;

    this.speed = function () {
     return Math.sqrt(this.vx * this.vx + this.vy * this.vy) ;
    }
   }
   
   var ship = new SpaceShip(10,20,3,6)  ;

   document.writeln("ship.speed() = " + ship.speed()) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/methods-during-initialization.html

Prototypes

A popular (and efficient) way to associate methods with objects is attach methods to a constructor's prototype. Methods attached to a prototype are bound to every instance of an object automatically:

[methods-from-prototype.html]

text
<html>
 <body>
  <script>
   function SpaceShip(x,y,vx,vy) {
    this.x = x ;
    this.y = y ;
    this.vx = vx ;
    this.vy = vy ;
   }

   SpaceShip.prototype.speed = function () {
     return Math.sqrt(this.vx * this.vx + this.vy * this.vy) ;
   } ;
  
   var ship = new SpaceShip(10,20,3,6)  ;

   document.writeln("ship.speed() = " + ship.speed()) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/methods-from-prototype.html

What is this?

The object bound to the keyword this depends on the current method that was invoked. If called using the object.method() form, then this is bound to the object. If called using the ordinary function call form fun(), then this gets bound to the top-level Window object. The fun.call() form allows the caller to specify the value of this is the first parameter:

[what-is-this.html]

text
<html>
 <body>
  <script>
   function WhatIsThis() {
    return this ;
   }

   function Something () {
    this.whatIsThis = WhatIsThis ;
    this.toString = function () { return "[Something]" } ;  
   }

   var o = new Something() ;

   document.writeln("o.whatIsThis() = " +
    o.whatIsThis()) ;
   document.writeln("<br />") ;
   document.writeln("WhatIsThis() = " +
    WhatIsThis()) ;
   document.writeln("<br />") ;
   document.writeln("WhatIsThis.call(314) = " +
    WhatIsThis.call(314)) ;
  </script>
 </body>
</html>

produces:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/what-is-this.html

Practice: A Calculator

Tie together everything you've learned so far, and make the following calculator work:

▶ Embedded content: https://matt.might.net/articles/learning-javascript-in-small-bites-part-2/calculator.html

Source code: calculator.html