<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=WebFund_2024F_Lecture_22</id>
	<title>WebFund 2024F Lecture 22 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=WebFund_2024F_Lecture_22"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_22&amp;action=history"/>
	<updated>2026-04-22T11:30:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_22&amp;diff=24904&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture for December 3, 2024 is now available: * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.cc.vtt auto-generated captions]  ==Notes==  &lt;pre&gt; Lecture 22 ----------  Last class is Thursday  - will go over Assignment 4 solutions then  Assignment 4 is due Wednesday night  - accepted until...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2024F_Lecture_22&amp;diff=24904&amp;oldid=prev"/>
		<updated>2024-12-04T00:32:33Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture for December 3, 2024 is now available: * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.cc.vtt auto-generated captions]  ==Notes==  &amp;lt;pre&amp;gt; Lecture 22 ----------  Last class is Thursday  - will go over Assignment 4 solutions then  Assignment 4 is due Wednesday night  - accepted until...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture for December 3, 2024 is now available:&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.m4v video]&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/lectures/comp2406-2024f-lec22-20241203.cc.vtt auto-generated captions]&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 22&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Last class is Thursday&lt;br /&gt;
 - will go over Assignment 4 solutions then&lt;br /&gt;
&lt;br /&gt;
Assignment 4 is due Wednesday night&lt;br /&gt;
 - accepted until 11:30 AM on Thursday&lt;br /&gt;
 - no time for extensions, sorry&lt;br /&gt;
&lt;br /&gt;
A3 &amp;amp; A4 will be graded before the final exam on Dec 12, 9 AM&lt;br /&gt;
&lt;br /&gt;
I&amp;#039;ve sent out emails for interviews&lt;br /&gt;
 - if you got one, please schedule one or contact me if you can&amp;#039;t find a slot&lt;br /&gt;
&lt;br /&gt;
Openstack&lt;br /&gt;
 - will be open through exam&lt;br /&gt;
 - I&amp;#039;ll send out notice, but please back up your work soon after the exam&lt;br /&gt;
&lt;br /&gt;
I will be conducting interviews after the final exam&lt;br /&gt;
 - will take place the week of Dec 16th&lt;br /&gt;
 - only requires 15 minutes, no prep needed&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Q&amp;amp;A session?&lt;br /&gt;
 - I could do one on Dec. 10th, during regular class time&lt;br /&gt;
&lt;br /&gt;
Last lecture quiz will be for today&lt;br /&gt;
 - officially due Friday, but will give a few extra days to submit&lt;br /&gt;
&lt;br /&gt;
Exam will be on the code from Assignment 4&lt;br /&gt;
 - it has everything we&amp;#039;ve covered!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Strict mode&lt;br /&gt;
 - original javascript was VERY loose, made it hard to see when you made an error&lt;br /&gt;
 - strict mode catches a lot of errors&lt;br /&gt;
    - in particular, assigning to a variable that hasn&amp;#039;t been declared&lt;br /&gt;
 - Deno defaults to strict mode&lt;br /&gt;
    - node does not!&lt;br /&gt;
 - JavaScript modules default to strict mode&lt;br /&gt;
&lt;br /&gt;
To ensure strict mode&lt;br /&gt;
 - put &amp;quot;use strict&amp;quot;; before any other code (with quotes) at top of file&lt;br /&gt;
 - can also enable per function, just put &amp;quot;use strict&amp;quot;; at the top&lt;br /&gt;
&lt;br /&gt;
While strict mode will detect assigning to undeclared variables, it won&amp;#039;t stop assignment to undefined properties of objects.&lt;br /&gt;
&lt;br /&gt;
Objects, prototypes, &amp;amp; classes&lt;br /&gt;
------------------------------&lt;br /&gt;
JavaScript is an object-oriented language&lt;br /&gt;
 - it does support inheritance &amp;amp; classes, even though we haven&amp;#039;t used them in class&lt;br /&gt;
&lt;br /&gt;
While JavaScript supports classes and has special syntax for it, this support is mostly &amp;quot;syntactic sugar&amp;quot;&lt;br /&gt;
 - the underlying functionality was already there&lt;br /&gt;
&lt;br /&gt;
JavaScript&amp;#039;s model is prototype-based inheritance, not class-based inheritance&lt;br /&gt;
 - in the end, we just have objects&lt;br /&gt;
 - to inherit, we have a prototype chain&lt;br /&gt;
    - if an object doesn&amp;#039;t have a property or method, we check its&lt;br /&gt;
      prototypes for the functionality, going back in a chain&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So if you want to do inheritance, you can inherit from *any* object&lt;br /&gt;
 - you just set it as the prototype for the new object&lt;br /&gt;
&lt;br /&gt;
But doesn&amp;#039;t that mean that the prototype could change at runtime?&lt;br /&gt;
 - YES&lt;br /&gt;
 - this can be a good thing or a bad thing, depending&lt;br /&gt;
 - but it is dynamic&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you might notice, I&amp;#039;m really bad with doing object-based inheritance in JavaScript&lt;br /&gt;
 - that&amp;#039;s because I don&amp;#039;t like inheritance, I just use objects as key-value stores&lt;br /&gt;
 - and I instead use functions&lt;br /&gt;
&lt;br /&gt;
So what does &amp;quot;new&amp;quot; and &amp;quot;this&amp;quot; do?&lt;br /&gt;
 - this has the value of the object a function is associated with&lt;br /&gt;
 - if it is just a bare function, this has no value (undefined)&lt;br /&gt;
 - if you call the function with &amp;quot;new&amp;quot;, then this has the value of {}, and&lt;br /&gt;
   is what the function returns normally&lt;br /&gt;
     - this empty object will have the prototype of the function,&lt;br /&gt;
       so in this case the function is acting as a constructor&lt;br /&gt;
&lt;br /&gt;
If I want an object with methods, I just make an object and assign methods to its properties&lt;br /&gt;
 - if I want that method to access the object, I use &amp;quot;this&amp;quot;&lt;br /&gt;
 - rather than use new, just make a function that returns the object you want&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Note that modules are almost class-like in JavaScript&lt;br /&gt;
 - when you import, you can assign all exported values (functions, variables, constants) to properties of an object&lt;br /&gt;
    - see what we did with authdb.js&lt;br /&gt;
 - so prototype-based inheritance isn&amp;#039;t used that often in modern javascript&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
Note the code below is different from that shown in lecture. In particular, the prototype-based example now works, as I fumbled this in lecture. As you can see below, you can approximate the functionality of JavaScript classes with prototypes, but it isn&amp;#039;t exactly the same.&lt;br /&gt;
&lt;br /&gt;
If you&amp;#039;re wanting to create an object hierarchy in modern JavaScript, use classes. But &amp;lt;tt&amp;gt;Object.create()&amp;lt;/tt&amp;gt; gives fine-grained control that can be useful for &amp;quot;mixins&amp;quot; or other more advanced inheritance patterns (has-a rather than is-a inheritance).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
// Class example&lt;br /&gt;
&lt;br /&gt;
class Rectangle {&lt;br /&gt;
    name = &amp;quot;Rectangle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    // joke is only accessible from the class object&lt;br /&gt;
    // but is also available in inheriting class objects&lt;br /&gt;
    static joke = &amp;quot;I&amp;#039;m not classy&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
    constructor(height, width) {&lt;br /&gt;
        this.height = height;&lt;br /&gt;
        this.width = width;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    area() {&lt;br /&gt;
        return this.width * this.height;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Square extends Rectangle {&lt;br /&gt;
    name = &amp;quot;Square&amp;quot;&lt;br /&gt;
    constructor(size) {&lt;br /&gt;
        super(size, size);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var s = new Square(4);&lt;br /&gt;
s.area();  // 16&lt;br /&gt;
s.name;  // &amp;quot;Square&amp;quot;&lt;br /&gt;
s.joke; // undefined&lt;br /&gt;
Square.joke // &amp;quot;I&amp;#039;m not classy&amp;quot;&lt;br /&gt;
Rectangle.joke //  &amp;quot;I&amp;#039;m not classy&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// with prototypes &amp;amp; Object.create()&lt;br /&gt;
&lt;br /&gt;
function Oval(width, height) {&lt;br /&gt;
&lt;br /&gt;
    // Classes don&amp;#039;t need the check below,&lt;br /&gt;
    // they automatically generate an error&lt;br /&gt;
    // if you call the constructor without new    &lt;br /&gt;
    if (!this) {&lt;br /&gt;
        console.error(&amp;quot;Please call with new&amp;quot;);&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    this.width = width;&lt;br /&gt;
    this.height = height;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Oval.joke = &amp;quot;I&amp;#039;m really not classy&amp;quot;;&lt;br /&gt;
Oval.prototype.name = &amp;quot;Oval&amp;quot;;&lt;br /&gt;
Oval.prototype.area = function () {&lt;br /&gt;
        return Math.PI * this.height * this.width;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
function Circle(r) {&lt;br /&gt;
    return Oval.call(this, r, r);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Circle.prototype = Object.create(Oval.prototype, {&lt;br /&gt;
    constructor: {&lt;br /&gt;
        value: Circle,&lt;br /&gt;
        enumerable: false,&lt;br /&gt;
        writeable: true,&lt;br /&gt;
        configurable: true,&lt;br /&gt;
    },&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
Circle.prototype.name = &amp;quot;Circle&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
var c = new Circle(4);&lt;br /&gt;
c.area();  // 50.26548245743669&lt;br /&gt;
c.name;  // &amp;quot;Circle&amp;quot;&lt;br /&gt;
c.joke; // undefined&lt;br /&gt;
Circle.joke // undefined&lt;br /&gt;
Oval.joke //  &amp;quot;I&amp;#039;m really not classy&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>