WebFund 2015W Lecture 10: Difference between revisions

From Soma-notes
Line 21: Line 21:
This code should instead say:
This code should instead say:


<source lang="javascript" enclose="div">
<pre>
   if (!userNotes.hasOwnProperty(username)) {
   if (!userNotes.hasOwnProperty(username)) {
       userNotes[username] = [];
       userNotes[username] = [];
   }
   }
</source>
</pre>


If you are treating a JavaScript object as an associative array you must explicitly check whether a given key value (property) is present using the hasOwnProperty() method inherited by every object.
If you are treating a JavaScript object as an associative array you must explicitly check whether a given key value (property) is present using the hasOwnProperty() method inherited by every object.


Note you can create an object x with no prototype by doing the following:
Note you can create an object x with no prototype by doing the following:

Revision as of 17:43, 4 February 2015

Resources


Video

The video from the lecture given on February 4, 2015 is available now as an MP4.

Notes

CORRECTION

In lecture we saw that we could break notes-demo by giving hasOwnProperty as a username. This error is because the following code in the login post handler is incorrect:

    if (!userNotes[username]) {
	userNotes[username] = [];
    }

This code should instead say:

   if (!userNotes.hasOwnProperty(username)) {
       userNotes[username] = [];
   }

If you are treating a JavaScript object as an associative array you must explicitly check whether a given key value (property) is present using the hasOwnProperty() method inherited by every object.

Note you can create an object x with no prototype by doing the following: