[ { "text": "[MUSIC PLAYING]", "start": 0.0, "duration": 2.465 }, { "text": "DAVID MALAN: All right, welcome\neveryone to Mobile App Development", "start": 18.241, "duration": 2.799 }, { "text": "with React Native.", "start": 21.04, "duration": 0.93 }, { "text": "My name is David Malan,\nand this here is--", "start": 21.97, "duration": 2.04 }, { "text": "JORDAN HAYASHI: Jordan Hayashi.", "start": 24.01, "duration": 0.96 }, { "text": "DAVID MALAN: --and this\nis a course that assumes", "start": 24.97, "duration": 2.0 }, { "text": "as background only\nsomething like CS50, which", "start": 26.97, "duration": 2.204 }, { "text": "is Harvard's Introduction\nto Computer Science.", "start": 29.174, "duration": 1.916 }, { "text": "But more generally, if you haven't\ntaken that particular course,", "start": 31.09, "duration": 2.666 }, { "text": "prior programming experience in\nmost any language should suffice.", "start": 33.756, "duration": 3.424 }, { "text": "The focus of this class, of course, is\ngoing to be on mobile app development.", "start": 37.18, "duration": 2.46 }, { "text": "And the interesting\nthing about this space", "start": 39.64, "duration": 1.75 }, { "text": "is that it's been\nchanging pretty rapidly.", "start": 41.39, "duration": 1.937 }, { "text": "It wasn't all that long\nago where none of us", "start": 43.327, "duration": 1.833 }, { "text": "actually had smartphones in our pockets.", "start": 45.16, "duration": 1.959 }, { "text": "And so the landscape has been\nchanging, particularly quickly.", "start": 47.119, "duration": 2.541 }, { "text": "And it's getting better and better.", "start": 49.66, "duration": 1.53 }, { "text": "If you've done any app developments on\nmobile devices or any web development", "start": 51.19, "duration": 4.35 }, { "text": "on mobile devices,\nearly on we pretty much", "start": 55.54, "duration": 2.13 }, { "text": "just had HTML and CSS, maybe some\nJavaScript for interactivity.", "start": 57.67, "duration": 4.05 }, { "text": "Then lots of libraries\ncame onto the scenes.", "start": 61.72, "duration": 1.87 }, { "text": "They, via CSS and JavaScript,\ngave the appearance", "start": 63.59, "duration": 3.86 }, { "text": "that you had a mobile-like\napplication on your phone,", "start": 67.45, "duration": 2.85 }, { "text": "but the user interface\nwasn't all that good.", "start": 70.3, "duration": 2.01 }, { "text": "And you can tell that this\nisn't really \"native\".", "start": 72.31, "duration": 2.31 }, { "text": "That is, code that's in Objective-C or\nSwift on iOS or in Java for Android.", "start": 74.62, "duration": 5.76 }, { "text": "And something always felt a bit off.", "start": 80.38, "duration": 1.67 }, { "text": "So you could learn any\none of those languages", "start": 82.05, "duration": 1.93 }, { "text": "or pick up the various tool kits\nthat exist to write native software.", "start": 83.98, "duration": 3.63 }, { "text": "But it's a decent learning curve.", "start": 87.61, "duration": 1.42 }, { "text": "And if you're running a\ncompany or writing an app,", "start": 89.03, "duration": 1.85 }, { "text": "you have to generally pick and choose.", "start": 90.88, "duration": 1.47 }, { "text": "Do I want to target Android?", "start": 92.35, "duration": 1.2 }, { "text": "Do I want to do iOS, or do I essentially\nwant to do twice as much work?", "start": 93.55, "duration": 3.57 }, { "text": "And so that alone could\nbe a potential hurdle.", "start": 97.12, "duration": 2.612 }, { "text": "And then there were\nthese other libraries", "start": 99.732, "duration": 1.708 }, { "text": "that allowed you to approximate the\nexperience of writing your code once,", "start": 101.44, "duration": 4.86 }, { "text": "and then run it on both platforms.", "start": 106.3, "duration": 1.78 }, { "text": "But there, too, you could always\ntell that something wasn't quite", "start": 108.08, "duration": 3.23 }, { "text": "natural for the particular device.", "start": 111.31, "duration": 1.924 }, { "text": "And then more recently\nhas come onto the scene", "start": 113.234, "duration": 1.916 }, { "text": "a number of new frameworks,\nparticularly React Native,", "start": 115.15, "duration": 3.8 }, { "text": "an open source framework\npopularized by Facebook", "start": 118.95, "duration": 2.44 }, { "text": "that really actually now enables truly\nnative cross-platform developments", "start": 121.39, "duration": 5.34 }, { "text": "while using JavaScript to\nrather stitch things together", "start": 126.73, "duration": 2.65 }, { "text": "and then leaning on the\nframework to provide you", "start": 129.38, "duration": 2.0 }, { "text": "with those truly native user interface\nwidgets and other features that", "start": 131.38, "duration": 3.36 }, { "text": "would come to expect from\nthose various languages.", "start": 134.74, "duration": 2.321 }, { "text": "So what we'll do over the\ncourse of the semester", "start": 137.061, "duration": 1.999 }, { "text": "is really dive in deeply\nto mobile app development,", "start": 139.06, "duration": 2.58 }, { "text": "specifically building on top\nof this popular framework.", "start": 141.64, "duration": 2.58 }, { "text": "You might have heard of\nReacht Native or React", "start": 144.22, "duration": 2.294 }, { "text": "in the context of web browsers, which\nhas been around for some time as well.", "start": 146.514, "duration": 3.166 }, { "text": "And to a lot of those paradigms that\nfolks have been using for a few years", "start": 149.68, "duration": 3.083 }, { "text": "now on laptops and desktops is now\navailable with additional features", "start": 152.763, "duration": 3.247 }, { "text": "in a mobile context, especially.", "start": 156.01, "duration": 1.77 }, { "text": "So today we're going to dive in\nquite quickly to JavaScript itself.", "start": 157.78, "duration": 3.18 }, { "text": "If you have a bit of background\nin that, that's great.", "start": 160.96, "duration": 2.25 }, { "text": "We'll hopefully fill in\nsome gaps along the way.", "start": 163.21, "duration": 1.8 }, { "text": "And we'll also look at some of the\nmore advanced features both tonight", "start": 165.01, "duration": 2.916 }, { "text": "and next week as well,\nparticularly ES6 6 or ECMAScript 6,", "start": 167.926, "duration": 3.714 }, { "text": "which is essentially the\nlatest version of JavaScript,", "start": 171.64, "duration": 2.42 }, { "text": "and with it some new syntactic\nfeatures and programmatic capabilities.", "start": 174.06, "duration": 4.024 }, { "text": "A week after that, we'll\ntake a look at React itself", "start": 178.084, "duration": 2.166 }, { "text": "and JSX, which rather comingles\ncode with XML or a markup language,", "start": 180.25, "duration": 4.26 }, { "text": "if you're familiar.", "start": 184.51, "duration": 1.03 }, { "text": "And then look at some of\nthe particular features", "start": 185.54, "duration": 2.42 }, { "text": "and UI features that you get\nwith a framework like React JS.", "start": 187.96, "duration": 3.12 }, { "text": "Components, props, how\nyou deal with states,", "start": 191.08, "duration": 2.37 }, { "text": "how you can actually stylize things,\nget user input, and create views,", "start": 193.45, "duration": 3.81 }, { "text": "if you're familiar with paradigms\nlike MVC, model view controller.", "start": 197.26, "duration": 3.68 }, { "text": "Review governs what it is the user\nis seeing and interacting with.", "start": 200.94, "duration": 3.13 }, { "text": "We'll take a look at\ndebugging techniques,", "start": 204.07, "duration": 1.75 }, { "text": "particularly for a mobile platform,\nwhich might not necessarily be obvious", "start": 205.82, "duration": 3.08 }, { "text": "if the device you're working on is\nhere, and the device you're testing on", "start": 208.9, "duration": 3.041 }, { "text": "is here.", "start": 211.941, "duration": 0.549 }, { "text": "We'll give you some tools\nand techniques for that.", "start": 212.49, "duration": 1.98 }, { "text": "Focusing ultimately on data and\nnavigation, how you can actually", "start": 214.47, "duration": 2.77 }, { "text": "get users around in your applications.", "start": 217.24, "duration": 2.07 }, { "text": "And then looking at a popular\nthird party tool and framework", "start": 219.31, "duration": 2.61 }, { "text": "called Expo, which actually\nmakes it even easier", "start": 221.92, "duration": 2.42 }, { "text": "to develop for this\nparticular environment", "start": 224.34, "duration": 1.75 }, { "text": "and get started and\nget worked on quickly.", "start": 226.09, "duration": 1.92 }, { "text": "Looking at Redux and state\nmanagement again more generally.", "start": 228.01, "duration": 2.52 }, { "text": "Performance tuning of your applications.", "start": 230.53, "duration": 2.094 }, { "text": "And then finally how you\nactually get this thing off", "start": 232.624, "duration": 2.166 }, { "text": "of your laptop or desktop\nand off of just your phone", "start": 234.79, "duration": 3.21 }, { "text": "and on to other phones\nand mobile devices.", "start": 238.0, "duration": 2.547 }, { "text": "And we'll apply all of\nthese lessons learned", "start": 240.547, "duration": 1.833 }, { "text": "and the real challenges, and the\nhands dirty portion of the class", "start": 242.38, "duration": 2.708 }, { "text": "will be by way of the course's projects.", "start": 245.088, "duration": 1.912 }, { "text": "Three of them assign specifications from\nus that spec out exactly what it is you", "start": 247.0, "duration": 3.75 }, { "text": "should aspire to build.", "start": 250.75, "duration": 1.32 }, { "text": "And then the class will culminate\nwith the final project, where", "start": 252.07, "duration": 2.76 }, { "text": "it will be up to you to propose, to\ndesign, and ultimately implement most", "start": 254.83, "duration": 5.28 }, { "text": "any mobile application a top\nReact Native, as you might like.", "start": 260.11, "duration": 4.38 }, { "text": "Without further ado, let's dive\ninto an overview of the class itself", "start": 264.49, "duration": 4.02 }, { "text": "as well as, then.", "start": 268.51, "duration": 1.05 }, { "text": "JavaScript.", "start": 269.56, "duration": 0.5 }, { "text": "Let me turn things\nover already to Jordan.", "start": 270.06, "duration": 1.51 }, { "text": "JORDAN HAYASHI: Great, yeah.", "start": 271.57, "duration": 1.166 }, { "text": "Thanks for the great intro, David.", "start": 272.736, "duration": 2.104 }, { "text": "So a few things about\nthe course itself, you", "start": 274.84, "duration": 4.43 }, { "text": "can find information on the website.", "start": 279.27, "duration": 1.5 }, { "text": "I e-mailed you all the\nlink earlier today.", "start": 280.77, "duration": 2.8 }, { "text": "On it, there's a link\nto the Slack, which", "start": 283.57, "duration": 2.46 }, { "text": "is what we're going to use for\npretty instant communication.", "start": 286.03, "duration": 3.182 }, { "text": "You can create groups\namongst yourselves,", "start": 289.212, "duration": 1.708 }, { "text": "and we'll use that for any quick\ntidbits when we have to send out.", "start": 290.92, "duration": 4.162 }, { "text": "And then, additionally,\nwe have our staff email", "start": 295.082, "duration": 1.958 }, { "text": "that's linked as well, which is how\nyou can just email the staff directly.", "start": 297.04, "duration": 4.83 }, { "text": "And so a little bit about lectures.", "start": 301.87, "duration": 2.13 }, { "text": "We'll have a short break about halfway.", "start": 304.0, "duration": 1.625 }, { "text": "You can get up, you can use the\nbathroom, go rest your legs.", "start": 305.625, "duration": 4.075 }, { "text": "If you have a question at\nall during the lecture,", "start": 309.7, "duration": 2.43 }, { "text": "feel free to just shoot up your\nhand or interrupt me directly.", "start": 312.13, "duration": 3.342 }, { "text": "Concepts constantly build on each\nother, so it's pretty important", "start": 315.472, "duration": 2.708 }, { "text": "to learn everything\nup to a certain point", "start": 318.18, "duration": 2.0 }, { "text": "in order to build off of it later on.", "start": 320.18, "duration": 2.702 }, { "text": "And if something isn't super\nimportant to know when you ask", "start": 322.882, "duration": 2.458 }, { "text": "a question about it, I'll let you know.", "start": 325.34, "duration": 1.624 }, { "text": "So don't worry about\nasking dumb questions.", "start": 326.964, "duration": 2.336 }, { "text": "And the staff will be monitoring\nthe Slack during lecture.", "start": 329.3, "duration": 3.63 }, { "text": "So if people online have any\nquestions, feel free to post there,", "start": 332.93, "duration": 3.15 }, { "text": "and the staff will interrupt me.", "start": 336.08, "duration": 2.71 }, { "text": "Another thing, I love live examples.", "start": 338.79, "duration": 2.45 }, { "text": "I think the best examples\nare created on the spot.", "start": 341.24, "duration": 2.28 }, { "text": "And with that comes\na little bit of risk.", "start": 343.52, "duration": 2.83 }, { "text": "So live coding does have risks.", "start": 346.35, "duration": 1.43 }, { "text": "If I make a mistake,\nfeel free to correct me.", "start": 347.78, "duration": 2.25 }, { "text": "I have some candy in the lectern.", "start": 350.03, "duration": 2.392 }, { "text": "So if you correct me,\nI'll give you some candy.", "start": 352.422, "duration": 1.958 }, { "text": "For those online, sorry you don't get\ncandy, but you can have some glory.", "start": 354.38, "duration": 4.89 }, { "text": "Cool.", "start": 359.27, "duration": 1.489 }, { "text": "Let's start talking about JavaScript.", "start": 360.759, "duration": 1.541 }, { "text": "So JavaScript is an\ninterpretive language.", "start": 362.3, "duration": 2.295 }, { "text": "So I posted a little\nbit in the Slack earlier", "start": 364.595, "duration": 1.875 }, { "text": "asking about most comfortable languages.", "start": 366.47, "duration": 2.01 }, { "text": "Most of you guys were\nsaying JavaScript, but I", "start": 368.48, "duration": 2.49 }, { "text": "know those of you who took CS50\nmight have some experience with CE,", "start": 370.97, "duration": 3.87 }, { "text": "which is a compiled language.", "start": 374.84, "duration": 2.46 }, { "text": "So JavaScript is not compiled.", "start": 377.3, "duration": 2.807 }, { "text": "It's actually interpreted, so an\ninterpreter will read line by line,", "start": 380.107, "duration": 2.833 }, { "text": "execute that code line by line.", "start": 382.94, "duration": 2.01 }, { "text": "Each browser has built into\nit its own JavaScript engine,", "start": 384.95, "duration": 3.935 }, { "text": "which either interprets the code.", "start": 388.885, "duration": 1.375 }, { "text": "It might do some magic with\nsome just-in-time compilation,", "start": 390.26, "duration": 2.757 }, { "text": "but for the most part it's just\nreading your code line by line", "start": 393.017, "duration": 2.583 }, { "text": "and executing it.", "start": 395.6, "duration": 1.53 }, { "text": "Each browser actually has its\nown engine, and there are names.", "start": 397.13, "duration": 3.09 }, { "text": "So chrome is called V8.", "start": 400.22, "duration": 2.37 }, { "text": "If you've heard of Node.js at all, it\nalso uses that same node v8 engine.", "start": 402.59, "duration": 4.81 }, { "text": "Firefox has SpiderMonkey.", "start": 407.4, "duration": 1.25 }, { "text": "Safari has JavaScriptCore.", "start": 408.65, "duration": 1.6 }, { "text": "Chakra for Microsoft Edge\nand Internet Explorer.", "start": 410.25, "duration": 2.45 }, { "text": "You don't really need\nto know these names,", "start": 412.7, "duration": 1.75 }, { "text": "but it's a little bit important just\nto know that different ones exist,", "start": 414.45, "duration": 3.29 }, { "text": "because JavaScript actually\nis built off of a standard.", "start": 417.74, "duration": 3.72 }, { "text": "The standard is put out by ECMA,\nwhich stands for the European Computer", "start": 421.46, "duration": 3.6 }, { "text": "Manufacturers Association.", "start": 425.06, "duration": 1.89 }, { "text": "And that association is in charge of\njust putting out a spec, meaning hey,", "start": 426.95, "duration": 6.192 }, { "text": "I know you guys have this language.", "start": 433.142, "duration": 1.458 }, { "text": "But this is exactly what\nthis language needs to do.", "start": 434.6, "duration": 2.125 }, { "text": "And for every single\nfunction, this is exactly how", "start": 436.725, "duration": 2.585 }, { "text": "this function should behave.", "start": 439.31, "duration": 1.65 }, { "text": "So each of these browsers--", "start": 440.96, "duration": 1.73 }, { "text": "the engines in the browsers--", "start": 442.69, "duration": 1.45 }, { "text": "actually implement the standard.", "start": 444.14, "duration": 1.99 }, { "text": "But there are some functions where\nthe standard is a little bit hazy on,", "start": 446.13, "duration": 3.14 }, { "text": "or maybe it doesn't even define\nthis particular function.", "start": 449.27, "duration": 3.37 }, { "text": "And there's no hard line in exactly\nwhat that function should do.", "start": 452.64, "duration": 4.7 }, { "text": "So they may differ for anything\nnot defined by that standard.", "start": 457.34, "duration": 3.18 }, { "text": "And some syntax.", "start": 463.37, "duration": 2.41 }, { "text": "Let's actually hop into\ncode directly for this.", "start": 465.78, "duration": 3.81 }, { "text": "So let's do-- cool.", "start": 469.59, "duration": 4.35 }, { "text": "And the way that you declare\nvariables in JavaScript,", "start": 473.94, "duration": 2.64 }, { "text": "there's actually three different ways.", "start": 476.58, "duration": 1.93 }, { "text": "We'll talk about that\na little more later.", "start": 478.51, "duration": 1.749 }, { "text": "But for now, we'll just use\nthis keyword called const.", "start": 480.259, "duration": 2.402 }, { "text": "So if I want to declare a\nvariable, I'd say const.", "start": 482.661, "duration": 2.189 }, { "text": "So give me a variable called first name.", "start": 484.85, "duration": 1.76 }, { "text": "And I can give it any value I want.", "start": 491.89, "duration": 2.55 }, { "text": "So let's just call it my first name.", "start": 494.44, "duration": 1.66 }, { "text": "So you notice I have a\nstring literal there.", "start": 499.4, "duration": 1.89 }, { "text": "It's using double quotes, and then I\nend that statement with a semi-colon.", "start": 501.29, "duration": 6.24 }, { "text": "I can also do a last name.", "start": 507.53, "duration": 1.2 }, { "text": "And if you notice, this time, I\nstill have that string literal,", "start": 513.736, "duration": 2.624 }, { "text": "but this time I'm using single quotes.", "start": 516.36, "duration": 1.439 }, { "text": "Because in JavaScript\nthere's really no difference", "start": 517.799, "duration": 2.083 }, { "text": "between double and single quotes.", "start": 519.882, "duration": 1.398 }, { "text": "You also notice I omitted that\nsemi-colon, which in JavaScript is OK.", "start": 521.28, "duration": 3.72 }, { "text": "So my colons are actually optional.", "start": 525.0, "duration": 1.47 }, { "text": "So say I wanted something\nthat was not a string.", "start": 529.44, "duration": 2.88 }, { "text": "I can just give it a value, like 42.", "start": 532.32, "duration": 3.63 }, { "text": "So in C, you might see\nsomething like, give me an int,", "start": 535.95, "duration": 3.63 }, { "text": "or give me a string or a char star.", "start": 539.58, "duration": 4.622 }, { "text": "You're declaring types\nright away with C,", "start": 544.202, "duration": 1.708 }, { "text": "but for something like JavaScript,\nyou actually don't have to do that.", "start": 545.91, "duration": 4.78 }, { "text": "We can also do arrays.", "start": 550.69, "duration": 1.31 }, { "text": "And we can just declare them in\nline like this, and I can have--", "start": 554.654, "duration": 2.666 }, { "text": "So, if you notice, I actually have\nthree different types all in this array.", "start": 574.73, "duration": 3.72 }, { "text": "So I have a string, I have a\nnumber, and I have a function.", "start": 578.45, "duration": 2.825 }, { "text": "And this is perfectly fine.", "start": 581.275, "duration": 1.125 }, { "text": "JavaScript doesn't really care\nwhat you throw in an array.", "start": 582.4, "duration": 2.416 }, { "text": "You can have all sorts of\ndifferent variable types.", "start": 584.816, "duration": 3.184 }, { "text": "And so say we want to access\nthose things in an array.", "start": 588.0, "duration": 2.597 }, { "text": "Anybody care to guess\nexactly how I would do that?", "start": 590.597, "duration": 2.083 }, { "text": "So say I want to execute that function.", "start": 592.68, "duration": 2.32 }, { "text": "How might I go about doing that?", "start": 595.0, "duration": 1.62 }, { "text": "Any guesses?", "start": 596.62, "duration": 1.54 }, { "text": "Yeah.", "start": 598.16, "duration": 1.567 }, { "text": "Yeah, exactly.", "start": 599.727, "duration": 0.583 }, { "text": "The array of two.", "start": 600.31, "duration": 0.708 }, { "text": "So if you're familiar with\nother languages, a lot of them", "start": 601.018, "duration": 2.592 }, { "text": "have the same syntax for\nindexing into an array.", "start": 603.61, "duration": 2.57 }, { "text": "And so since we have three\nthings in this array,", "start": 606.18, "duration": 2.71 }, { "text": "we do what's called zero indexing,\nwhereby the first index in that array", "start": 608.89, "duration": 3.54 }, { "text": "is called the zeroth index.", "start": 612.43, "duration": 1.65 }, { "text": "And then you count from there.", "start": 614.08, "duration": 1.41 }, { "text": "So we have zeroth here,\nfirst, and second here.", "start": 615.49, "duration": 3.22 }, { "text": "And so say I want to\naccess that function.", "start": 618.71, "duration": 2.09 }, { "text": "I just do array 2, and\nI get that function.", "start": 620.8, "duration": 2.94 }, { "text": "And say I now wanted to\nexecute that function.", "start": 623.74, "duration": 2.43 }, { "text": "I can, say, execute it like that.", "start": 626.17, "duration": 1.742 }, { "text": "So this is something that you\nmight see in other languages,", "start": 627.912, "duration": 2.458 }, { "text": "but JavaScript you can do it as well.", "start": 630.37, "duration": 1.86 }, { "text": "You can just grab that function out of\nthat array and execute it like that.", "start": 632.23, "duration": 5.66 }, { "text": "Say I wanted a for loop, and I wanted\nto console log everything in that array.", "start": 637.89, "duration": 3.25 }, { "text": "I can do it just almost\nlike C. So I can do for--", "start": 641.14, "duration": 5.7 }, { "text": "this time we'll use\nlet for the variable.", "start": 646.84, "duration": 2.13 }, { "text": "We'll talk a little\nmore about that later.", "start": 648.97, "duration": 2.09 }, { "text": "So I start at zero.", "start": 651.06, "duration": 1.327 }, { "text": "Well i is less than the raised length.", "start": 652.387, "duration": 1.583 }, { "text": "[TYPING]", "start": 653.97, "duration": 2.43 }, { "text": "You might see me--", "start": 668.08, "duration": 0.9 }, { "text": "my personal preference is\nto omit the semicolons,", "start": 668.98, "duration": 2.85 }, { "text": "but you might read something\nonline that has them.", "start": 671.83, "duration": 2.46 }, { "text": "It really doesn't matter all that much.", "start": 674.29, "duration": 2.12 }, { "text": "And so this line of code here.", "start": 676.41, "duration": 1.25 }, { "text": "You might have seen a for loop in\nother languages that you've used.", "start": 677.66, "duration": 2.54 }, { "text": "So this one is just saying\nfor give me a variable that's", "start": 680.2, "duration": 2.58 }, { "text": "called i that starts at zero.", "start": 682.78, "duration": 1.92 }, { "text": "And while it's less than the\nnumber of values in the array--", "start": 684.7, "duration": 2.97 }, { "text": "array.length-- just\nkeep incrementing it.", "start": 687.67, "duration": 2.13 }, { "text": "And then every single\ntime console.log, which", "start": 689.8, "duration": 2.85 }, { "text": "is JavaScript's print function,\nwhatever that value in that array is.", "start": 692.65, "duration": 5.07 }, { "text": "So we can actually run this.", "start": 697.72, "duration": 1.23 }, { "text": "And as you see the first\ntime, line 12, where", "start": 702.92, "duration": 8.36 }, { "text": "we say index into that array to\nthe second value, and call it.", "start": 711.28, "duration": 4.74 }, { "text": "That's what's printing high.", "start": 716.02, "duration": 1.47 }, { "text": "And this for loop here will now\nprint string 42 and this thing called", "start": 717.49, "duration": 3.18 }, { "text": "function, which is this function.", "start": 720.67, "duration": 3.23 }, { "text": "Cool, any questions on syntax?", "start": 723.9, "duration": 2.32 }, { "text": "Yeah.", "start": 726.22, "duration": 0.6 }, { "text": "AUDIENCE: In your array\nthere, does it matter if you", "start": 726.82, "duration": 3.08 }, { "text": "have that third comma after your--", "start": 729.9, "duration": 1.66 }, { "text": "JORDAN HAYASHI: It does not matter.", "start": 731.56, "duration": 1.458 }, { "text": "So the question was, does it matter on\nline 9 that comment at the very end?", "start": 733.018, "duration": 4.292 }, { "text": "And so JavaScript allows you to have\ntrailing commas, meaning in arrays,", "start": 737.31, "duration": 3.46 }, { "text": "objects, or even function calls.", "start": 740.77, "duration": 1.742 }, { "text": "You can actually have extra commas, and\nit doesn't matter whether you have it", "start": 742.512, "duration": 3.208 }, { "text": "or not.", "start": 745.72, "duration": 0.63 }, { "text": "It's optional just\nlike the semicolon is.", "start": 746.35, "duration": 2.37 }, { "text": "But were I to omit\nthe comma here and try", "start": 748.72, "duration": 2.94 }, { "text": "to run that, I'm going to get\na syntax error as expected.", "start": 751.66, "duration": 4.86 }, { "text": "Cool, any other questions?", "start": 760.2, "duration": 3.134 }, { "text": "Great.", "start": 763.334, "duration": 1.476 }, { "text": "So back to slides.", "start": 764.81, "duration": 0.75 }, { "text": "So types.", "start": 765.56, "duration": 0.594 }, { "text": "So we talked about types\na little bit earlier,", "start": 766.154, "duration": 1.916 }, { "text": "and I'm just going to talk a\nlittle bit more about them.", "start": 768.07, "duration": 2.68 }, { "text": "So JavaScript has what's called dynamic\ntyping, meaning given a variable,", "start": 770.75, "duration": 4.13 }, { "text": "it has no type associated with it.", "start": 774.88, "duration": 2.05 }, { "text": "So just like I said, give me\na variable called first name", "start": 776.93, "duration": 3.59 }, { "text": "and set it equal to a string.", "start": 780.52, "duration": 1.92 }, { "text": "I could actually change\nthat later to, say, a type", "start": 782.44, "duration": 2.46 }, { "text": "number or a Boolean\nor anything like that.", "start": 784.9, "duration": 2.22 }, { "text": "And JavaScript is fine with that just\nbecause it has those dynamic types.", "start": 787.12, "duration": 4.17 }, { "text": "And there a certain\nnumber of primitive types.", "start": 791.29, "duration": 2.13 }, { "text": "Primitive types are types that have\nno methods, and they're immutable.", "start": 793.42, "duration": 4.36 }, { "text": "And so undefined, null are two of them.", "start": 797.78, "duration": 2.54 }, { "text": "Boolean.", "start": 800.32, "duration": 0.81 }, { "text": "Everybody should know\nBoolean true or false.", "start": 801.13, "duration": 2.86 }, { "text": "Number 1, 2, 3, negative 1, 2, 3.", "start": 803.99, "duration": 3.55 }, { "text": "It has no float, so 0 and\n0.1 are both of type number.", "start": 807.54, "duration": 6.79 }, { "text": "String, which are the\nthings between the quotes.", "start": 814.33, "duration": 6.45 }, { "text": "And then symbol, which is\nsomething that's new in ES6,", "start": 820.78, "duration": 2.43 }, { "text": "but we're not going to talk about\nit, nor are we going to use it,", "start": 823.21, "duration": 5.13 }, { "text": "and then everything else is an object,\nand we'll talk a little bit more", "start": 828.34, "duration": 3.382 }, { "text": "about that later.", "start": 831.722, "duration": 0.708 }, { "text": "Cool and so a lot of languages\nhave this thing where", "start": 836.24, "duration": 5.49 }, { "text": "you change one type to another type.", "start": 841.73, "duration": 1.87 }, { "text": "So since JavaScript\nis dynamically typed,", "start": 843.6, "duration": 2.18 }, { "text": "we have this thing called typecasting.", "start": 845.78, "duration": 3.02 }, { "text": "So coercion is the act of changing\none type to a different type,", "start": 848.8, "duration": 3.67 }, { "text": "and there's two different ways\nyou can coerce these variables.", "start": 852.47, "duration": 2.88 }, { "text": "So say, as an example, we\nhave this const called x", "start": 855.35, "duration": 3.18 }, { "text": "and we give it a value 42.", "start": 858.53, "duration": 2.04 }, { "text": "Say I wanted to change\nthat value to a string.", "start": 860.57, "duration": 2.22 }, { "text": "There are a couple\ndifferent ways I can do it.", "start": 862.79, "duration": 1.24 }, { "text": "There's explicit coercion.", "start": 864.03, "duration": 1.76 }, { "text": "There's implicit coercion.", "start": 865.79, "duration": 1.3 }, { "text": "So explicit is, hey, I'm\njust going to tell you", "start": 867.09, "duration": 2.57 }, { "text": "exactly what I want by wrapping\nit with the type that I want,", "start": 869.66, "duration": 4.29 }, { "text": "and give it to me.", "start": 873.95, "duration": 1.17 }, { "text": "And so that's being very\nexplicit with what I want.", "start": 875.12, "duration": 2.19 }, { "text": "And so the example of\nthat would be say I", "start": 877.31, "duration": 1.83 }, { "text": "want to get a string from that value x.", "start": 879.14, "duration": 2.22 }, { "text": "I just wrap it with capital string,\nand it pops out 42 with a string.", "start": 881.36, "duration": 5.39 }, { "text": "There's also a way of\ndoing it implicitly,", "start": 886.75, "duration": 1.75 }, { "text": "which is I'm going to rely\non the behavior of JavaScript", "start": 888.5, "duration": 3.24 }, { "text": "in order to get this to a string.", "start": 891.74, "duration": 1.69 }, { "text": "And so say I want to\nadd 42 to empty string.", "start": 893.43, "duration": 4.859 }, { "text": "That wouldn't really make sense\nif I wanted to have a number,", "start": 898.289, "duration": 2.541 }, { "text": "but since 42 is easily\ncastable to a string,", "start": 900.83, "duration": 2.49 }, { "text": "I can say, hey, give me\n42, add an empty string,", "start": 903.32, "duration": 3.39 }, { "text": "and I expect to get back\nsomething of type string.", "start": 906.71, "duration": 2.85 }, { "text": "That is called implicit coercion.", "start": 909.56, "duration": 3.61 }, { "text": "And so how might we go\nabout comparing-- oh, yeah.", "start": 913.17, "duration": 2.166 }, { "text": "Question.", "start": 915.336, "duration": 0.5 }, { "text": "AUDIENCE: Is the string passing\nbehind the same ex.2 string?", "start": 915.836, "duration": 3.753 }, { "text": "JORDAN HAYASHI: Yeah, so another\nway would be to invoke that.", "start": 919.589, "duration": 2.541 }, { "text": "So the question was,\nwhat about ex.2 string?", "start": 924.92, "duration": 3.84 }, { "text": "And so yes, another way to\nget from a number to a string", "start": 928.76, "duration": 3.69 }, { "text": "would be to invoke that method on\nthe string or on the number, sorry.", "start": 932.45, "duration": 4.99 }, { "text": "Cool, and so how might we\ngo about comparing values?", "start": 937.44, "duration": 4.16 }, { "text": "So in most languages you can compare\nvalues with the double equals,", "start": 941.6, "duration": 3.39 }, { "text": "but JavaScript has this\nthing called triple equals.", "start": 944.99, "duration": 2.761 }, { "text": "So there are two different\nways of comparing equality.", "start": 947.751, "duration": 2.249 }, { "text": "There's double equals and\nthere's triple equals,", "start": 950.0, "duration": 2.26 }, { "text": "where double equals will\nactually coerce the types,", "start": 952.26, "duration": 2.48 }, { "text": "and triple equals require\nthat the types match.", "start": 954.74, "duration": 4.05 }, { "text": "And so let's play with\nthat a little bit.", "start": 958.79, "duration": 1.905 }, { "text": "So say I have this value, and I want\nto know exactly what type that is.", "start": 966.88, "duration": 9.51 }, { "text": "So there's this operator called typeof\nin JavaScript where I can invoke this.", "start": 976.39, "duration": 4.62 }, { "text": "And that will give me the type\nof whatever that variable holds.", "start": 988.77, "duration": 8.99 }, { "text": "So if I were to run this code,\nthen it would say number,", "start": 997.76, "duration": 6.05 }, { "text": "because that is the type.", "start": 1003.81, "duration": 1.8 }, { "text": "So right now in order to\nexecute my JavaScript,", "start": 1005.61, "duration": 1.98 }, { "text": "I'm using this thing called node,\nwhich, as mentioned earlier,", "start": 1007.59, "duration": 3.45 }, { "text": "is basically a command line runtime for\nJavaScript, which is built off of V8.", "start": 1011.04, "duration": 5.2 }, { "text": "Any browser has a console where you\ncan also just type JavaScript directly", "start": 1019.74, "duration": 3.09 }, { "text": "in there.", "start": 1022.83, "duration": 1.05 }, { "text": "So if I were to open up the tools on\nChrome, which is my browser of choice,", "start": 1023.88, "duration": 6.969 }, { "text": "I actually get this, which\nis a JavaScript console built", "start": 1030.849, "duration": 9.441 }, { "text": "into all of these browsers.", "start": 1040.29, "duration": 1.613 }, { "text": "So if you guys are using Chrome at\nhome, and you want to follow along,", "start": 1041.903, "duration": 2.916 }, { "text": "you're welcome to open up\nthe developer tools here.", "start": 1044.819, "duration": 3.991 }, { "text": "Go to console, and you have your\nown JavaScript interpreter here.", "start": 1048.81, "duration": 3.93 }, { "text": "So say I wanted to do that thing\nwhere I do const x equals 42,", "start": 1052.74, "duration": 5.944 }, { "text": "and I wanted to get the\ntype of that variable.", "start": 1058.684, "duration": 1.916 }, { "text": "I can just do typeof x, and\nit will output that number.", "start": 1060.6, "duration": 4.62 }, { "text": "So there's a little bit of a caveat with\nthis, where this might surprise you.", "start": 1072.74, "duration": 4.52 }, { "text": "So who thinks they can\nguess what this will output?", "start": 1083.33, "duration": 4.24 }, { "text": "First of all, what should it output?", "start": 1087.57, "duration": 1.725 }, { "text": "So if we remember back a few slides, we\ntalked about all the different types.", "start": 1092.71, "duration": 6.4 }, { "text": "One of them is undefined,\none of them is null.", "start": 1099.11, "duration": 2.18 }, { "text": "And say I want to get typeof null.", "start": 1101.29, "duration": 6.99 }, { "text": "So what should it output?", "start": 1108.28, "duration": 3.191 }, { "text": "Yes?", "start": 1111.471, "duration": 0.499 }, { "text": "AUDIENCE: String.", "start": 1111.97, "duration": 0.845 }, { "text": "JORDAN HAYASHI: String,\nwhy would you say string?", "start": 1112.815, "duration": 2.041 }, { "text": "AUDIENCE: Because [INAUDIBLE].", "start": 1116.902, "duration": 1.968 }, { "text": "JORDAN HAYASHI: That's a good guess.", "start": 1124.53, "duration": 1.5 }, { "text": "So basically, the answer was so if you\ncan console log it must be a string.", "start": 1126.03, "duration": 5.34 }, { "text": "And while that is correct, most of these\ntypes can actually be cast to string.", "start": 1131.37, "duration": 8.4 }, { "text": "So we talked about implicit\nversus explicit coercion.", "start": 1139.77, "duration": 3.12 }, { "text": "And the way that console\nlog works is it actually", "start": 1142.89, "duration": 2.64 }, { "text": "will turn these values into a\nstring in order to console.log them,", "start": 1145.53, "duration": 4.68 }, { "text": "but it doesn't necessarily mean the\ntypeof that value itself is a string.", "start": 1150.21, "duration": 4.99 }, { "text": "Yeah?", "start": 1155.2, "duration": 1.48 }, { "text": "Yeah, so the null.", "start": 1156.68, "duration": 2.08 }, { "text": "So we would expect the typeof null\nto be null, since null is actually", "start": 1158.76, "duration": 3.57 }, { "text": "a primitive type.", "start": 1162.33, "duration": 1.69 }, { "text": "However, this actually returns object.", "start": 1164.02, "duration": 4.61 }, { "text": "So JavaScript does have\nsome strange behaviors.", "start": 1168.63, "duration": 3.64 }, { "text": "This is one of them.", "start": 1172.27, "duration": 2.06 }, { "text": "And people often ask,\nhey, we're on ES6 now.", "start": 1174.33, "duration": 3.48 }, { "text": "There have been six different\nversions of JavaScript.", "start": 1177.81, "duration": 2.91 }, { "text": "Why don't you just\nchange this to be null?", "start": 1180.72, "duration": 4.87 }, { "text": "And the answer that ECMA gives is,\nwell, the whole internet would break.", "start": 1185.59, "duration": 4.99 }, { "text": "And so each new version of\nECMAScript should definitely", "start": 1190.58, "duration": 4.39 }, { "text": "be backwards compatible\nwith the previous versions.", "start": 1194.97, "duration": 2.37 }, { "text": "Otherwise, say I put\nout a website tomorrow.", "start": 1197.34, "duration": 3.36 }, { "text": "If somebody comes down and\nchanges the JavaScript spec,", "start": 1200.7, "duration": 2.91 }, { "text": "then my website might break.", "start": 1203.61, "duration": 1.38 }, { "text": "And so a lot of websites\nactually rely on this to be true.", "start": 1204.99, "duration": 4.26 }, { "text": "And, therefore, if a breaking\nversion of ECMAScript is released,", "start": 1209.25, "duration": 4.47 }, { "text": "it might actually just have\nunforeseen consequences.", "start": 1213.72, "duration": 2.677 }, { "text": "So this is just one of those\nstrange JavaScript gotchas.", "start": 1216.397, "duration": 2.333 }, { "text": "Cool, so another good question would\nbe so when should I use double equals", "start": 1223.62, "duration": 5.16 }, { "text": "versus triple equals?", "start": 1228.78, "duration": 1.96 }, { "text": "And people generally say you\nshould never use double equals,", "start": 1230.74, "duration": 4.43 }, { "text": "because that means you have to know\nexactly how every single thing coerces.", "start": 1235.17, "duration": 4.719 }, { "text": "And not only you, but every\nsingle person who reads your code", "start": 1239.889, "duration": 2.541 }, { "text": "should know what all\nthese values coerce do.", "start": 1242.43, "duration": 2.97 }, { "text": "And some of them might\nbe somewhat surprising.", "start": 1245.4, "duration": 2.2 }, { "text": "So we have a chart here that talks\nabout the JavaScript equality table.", "start": 1247.6, "duration": 4.43 }, { "text": "For those of you who have the slides\nopened, you can click on that link,", "start": 1252.03, "duration": 3.01 }, { "text": "and it will bring you to\nthe repo that has this.", "start": 1255.04, "duration": 2.24 }, { "text": "Basically, some of these\nstrings are somewhat strange.", "start": 1257.28, "duration": 3.18 }, { "text": "Like how empty array is double\nequals to false, which doesn't really", "start": 1260.46, "duration": 7.74 }, { "text": "make a ton of sense.", "start": 1268.2, "duration": 1.432 }, { "text": "A lot of these don't\nreally make a ton of sense", "start": 1269.632, "duration": 1.958 }, { "text": "and, basically, never use\nthat double equals because it", "start": 1271.59, "duration": 3.99 }, { "text": "might have some strange behaviors.", "start": 1275.58, "duration": 1.955 }, { "text": "So moving on with coercion.", "start": 1281.156, "duration": 1.124 }, { "text": "So we talked about coercing\nthings into other types,", "start": 1285.22, "duration": 3.65 }, { "text": "but how about if we're getting to bools?", "start": 1288.87, "duration": 2.22 }, { "text": "So JavaScript has these\nthings called falsy values.", "start": 1291.09, "duration": 3.99 }, { "text": "Who can name a falsy value?", "start": 1295.08, "duration": 3.39 }, { "text": "So falsy value is any value that,\nif cast to bool, becomes false.", "start": 1298.47, "duration": 6.388 }, { "text": "AUDIENCE: Two.", "start": 1304.858, "duration": 0.912 }, { "text": "JORDAN HAYASHI: Two?", "start": 1305.77, "duration": 1.41 }, { "text": "So two is actually truthy.", "start": 1307.18, "duration": 2.13 }, { "text": "So every number except for one number\nis truthy, well two numbers actually.", "start": 1309.31, "duration": 4.2 }, { "text": "AUDIENCE: Zero.", "start": 1313.51, "duration": 0.86 }, { "text": "JORDAN HAYASHI: Zero would\nbe one of them, yeah.", "start": 1314.37, "duration": 2.93 }, { "text": "Not a number which is actually a number,\nof type number, is also a falsy value.", "start": 1317.3, "duration": 6.23 }, { "text": "Who can name another falsy value?", "start": 1323.53, "duration": 2.144 }, { "text": "AUDIENCE: Blank?", "start": 1325.674, "duration": 0.976 }, { "text": "JORDAN HAYASHI: Blank what?", "start": 1326.65, "duration": 0.69 }, { "text": "AUDIENCE: Or empty.", "start": 1327.34, "duration": 0.84 }, { "text": "JORDAN HAYASHI: Empty what?", "start": 1328.18, "duration": 0.69 }, { "text": "AUDIENCE: Empty array.", "start": 1328.87, "duration": 1.282 }, { "text": "JORDAN HAYASHI: Empty\narray is actually truthy.", "start": 1330.152, "duration": 1.958 }, { "text": "False is another one.", "start": 1336.02, "duration": 2.52 }, { "text": "Undefined, null.", "start": 1338.54, "duration": 1.02 }, { "text": "So those are the five falsy values.", "start": 1342.59, "duration": 2.73 }, { "text": "So who can name some truthy values?", "start": 1345.32, "duration": 1.77 }, { "text": "Somebody said two, which is a valid one.", "start": 1349.78, "duration": 2.39 }, { "text": "Three, four, every other number other\nthan zero, negative zero, not a number.", "start": 1352.17, "duration": 5.98 }, { "text": "Empty array is also another truthy.", "start": 1358.15, "duration": 2.43 }, { "text": "Empty object and\nliterally everything else.", "start": 1360.58, "duration": 3.27 }, { "text": "So anything other than those\nvalues right there are truthy.", "start": 1363.85, "duration": 3.75 }, { "text": "Cool, so objects, arrays,\nfunctions, objects.", "start": 1371.73, "duration": 3.417 }, { "text": "It looks like I put objects twice\nthere, but I actually put it four times.", "start": 1375.147, "duration": 3.083 }, { "text": "So JavaScript has this weird\nthing where if it's not", "start": 1378.23, "duration": 3.56 }, { "text": "one of those primitive\nvalues, it's an object.", "start": 1381.79, "duration": 1.92 }, { "text": "And so we'll talk about\nthis thing in a little bit", "start": 1386.637, "duration": 2.083 }, { "text": "called prototype\ninheritance, which talks", "start": 1388.72, "duration": 2.37 }, { "text": "about how these objects\ninherit from each other", "start": 1391.09, "duration": 2.64 }, { "text": "and how they actually\nwork under the hood.", "start": 1393.73, "duration": 1.83 }, { "text": "But first let's compare those two types.", "start": 1395.56, "duration": 1.89 }, { "text": "So we talked about primitives\nearlier, which is--", "start": 1397.45, "duration": 2.25 }, { "text": "who can name some of the primitives?", "start": 1399.7, "duration": 3.525 }, { "text": "AUDIENCE: Null.", "start": 1403.225, "duration": 0.895 }, { "text": "JORDAN HAYASHI: Null, undefined.", "start": 1404.12, "duration": 3.24 }, { "text": "AUDIENCE: Number.", "start": 1407.36, "duration": 0.84 }, { "text": "JORDAN HAYASHI: Number.", "start": 1408.2, "duration": 1.394 }, { "text": "AUDIENCE: Boolean.", "start": 1409.594, "duration": 0.916 }, { "text": "JORDAN HAYASHI: Boolean.", "start": 1410.51, "duration": 0.45 }, { "text": "AUDIENCE: String.", "start": 1410.96, "duration": 0.92 }, { "text": "JORDAN HAYASHI: String.", "start": 1411.88, "duration": 0.958 }, { "text": "And simple.", "start": 1421.53, "duration": 0.737 }, { "text": "So good, you got them all.", "start": 1422.267, "duration": 1.083 }, { "text": "Nice.", "start": 1423.35, "duration": 0.5 }, { "text": "So everything other than those\nprimitive types are actually objects.", "start": 1428.317, "duration": 2.833 }, { "text": "So primitives are immutable,\nwhich means if you", "start": 1431.15, "duration": 2.577 }, { "text": "want to change them, you're actually\nreplacing them with a new value", "start": 1433.727, "duration": 2.833 }, { "text": "rather than actually changing them\nthemselves, whereas objects are not.", "start": 1436.56, "duration": 4.05 }, { "text": "They're actually mutable.", "start": 1440.61, "duration": 1.92 }, { "text": "So who knows what storing\nby reference means?", "start": 1442.53, "duration": 4.77 }, { "text": "So storing by reference\nmeans we actually", "start": 1447.3, "duration": 2.28 }, { "text": "store a reference to this object\nthing, and we can actually", "start": 1449.58, "duration": 3.36 }, { "text": "change what is held there\nwithout actually changing", "start": 1452.94, "duration": 2.79 }, { "text": "where that thing is located in memory.", "start": 1455.73, "duration": 2.55 }, { "text": "We'll talk a little bit\nmore about that in a second.", "start": 1458.28, "duration": 4.06 }, { "text": "But the opposite of that\nwould be storing something", "start": 1462.34, "duration": 3.23 }, { "text": "by a value, which is what\nhappens when you have primitives.", "start": 1465.57, "duration": 2.76 }, { "text": "So, like I said earlier,\nprimitives are immutable,", "start": 1468.33, "duration": 2.26 }, { "text": "which means once you create a\nprimitive, it can't actually be changed.", "start": 1470.59, "duration": 3.2 }, { "text": "And when you want to change something,\nyou actually create a new primitive", "start": 1473.79, "duration": 3.63 }, { "text": "and replace the old one, whereas\nmutable things are actually", "start": 1477.42, "duration": 3.51 }, { "text": "stored by reference, and you\ncan actually change that object.", "start": 1480.93, "duration": 3.872 }, { "text": "And so let's play with\nthat a little bit.", "start": 1484.802, "duration": 1.708 }, { "text": "So there are a few different\nways to create an object.", "start": 1497.68, "duration": 2.25 }, { "text": "One would be this way.", "start": 1499.93, "duration": 2.06 }, { "text": "So just saying, give me a new object.", "start": 1506.01, "duration": 1.77 }, { "text": "And so now o is this a new object thing.", "start": 1510.4, "duration": 4.552 }, { "text": "And say we want to start\npopulating the object.", "start": 1514.952, "duration": 1.958 }, { "text": "We can do o.firstname\nand assign that a value.", "start": 1516.91, "duration": 7.03 }, { "text": "o.lastname, assign\nthat a different value.", "start": 1526.64, "duration": 4.424 }, { "text": "Notice that I have strings with\ndouble quotes and single quotes.", "start": 1531.064, "duration": 2.666 }, { "text": "It doesn't matter.", "start": 1533.73, "duration": 2.526 }, { "text": "So something other than a string.", "start": 1536.256, "duration": 1.374 }, { "text": "We can do a Boolean, so isTeaching.", "start": 1537.63, "duration": 4.89 }, { "text": "We can assign that to true.", "start": 1542.52, "duration": 3.05 }, { "text": "o.greet, and we can\ngive that a function.", "start": 1545.57, "duration": 4.255 }, { "text": "[TYPING]", "start": 1549.825, "duration": 2.265 }, { "text": "So that would be one way\nof creating a new object is", "start": 1562.904, "duration": 2.166 }, { "text": "to use this new keyword\nalong with capital objects.", "start": 1565.07, "duration": 2.682 }, { "text": "And that says, hey,\ngive me a new object.", "start": 1567.752, "duration": 1.708 }, { "text": "And I'm just going to fill it up with\nthese values using this dot notation.", "start": 1569.46, "duration": 3.989 }, { "text": "Another way to do that would be\nwhat's called an object literal.", "start": 1573.449, "duration": 2.666 }, { "text": "So I can just do open\ncurly, close curly, and that", "start": 1578.66, "duration": 3.63 }, { "text": "gives me basically a new object.", "start": 1582.29, "duration": 2.55 }, { "text": "This is actually the preferred way\nover that old new key word with object,", "start": 1584.84, "duration": 5.46 }, { "text": "mostly because a, it's easier\nto read, and well mostly just", "start": 1590.3, "duration": 3.21 }, { "text": "because it's easier to read.", "start": 1593.51, "duration": 2.44 }, { "text": "And so I can start filling those\nvalues in with o.firstname.", "start": 1595.95, "duration": 3.075 }, { "text": "And another way to index into\nthese objects is to do o.lastname.", "start": 1603.21, "duration": 5.59 }, { "text": "So notice I use brackets there,\nwhich means inside this bracket,", "start": 1611.32, "duration": 3.4 }, { "text": "I'm going to have some\nvalue, and that value", "start": 1614.72, "duration": 2.03 }, { "text": "is going to be that key of that object.", "start": 1616.75, "duration": 1.93 }, { "text": "So I can do the same thing here.", "start": 1618.68, "duration": 1.85 }, { "text": "And say I actually wanted to use not a\nstring literal inside these brackets.", "start": 1624.73, "duration": 6.63 }, { "text": "I could also do that.", "start": 1631.36, "duration": 1.36 }, { "text": "So I could do isTeaching and do\no and then pass in this variable", "start": 1632.72, "duration": 11.78 }, { "text": "here with a value of isTeaching,\nand that will set that key.", "start": 1644.5, "duration": 7.69 }, { "text": "And then say I wanted to do o of\ngreet and give that same function.", "start": 1655.26, "duration": 6.01 }, { "text": "Call and so those objects\nare basically the same.", "start": 1671.07, "duration": 4.57 }, { "text": "And last, we can actually\nput everything in line.", "start": 1675.64, "duration": 2.26 }, { "text": "So we can do this.", "start": 1677.9, "duration": 2.94 }, { "text": "[TYPING]", "start": 1680.84, "duration": 1.976 }, { "text": "Cool, and so those three objects\nare basically the same thing.", "start": 1715.47, "duration": 3.86 }, { "text": "It's just three different\nways of declaring objects.", "start": 1719.33, "duration": 3.93 }, { "text": "You can also nest objects.", "start": 1723.26, "duration": 1.22 }, { "text": "So say I wanted an\nobject within an object.", "start": 1724.48, "duration": 2.43 }, { "text": "That's also fine.", "start": 1726.91, "duration": 3.055 }, { "text": "Say I wanted to do something like this.", "start": 1729.965, "duration": 1.625 }, { "text": "[TYPING]", "start": 1731.59, "duration": 1.952 }, { "text": "That's also fine.", "start": 1742.83, "duration": 0.724 }, { "text": "So that's an object within an object.", "start": 1743.554, "duration": 1.541 }, { "text": "Any questions with that?", "start": 1748.1, "duration": 1.03 }, { "text": "Cool.", "start": 1752.995, "duration": 0.5 }, { "text": "That's actually--", "start": 1756.9, "duration": 3.494 }, { "text": "AUDIENCE: I have a question.", "start": 1760.394, "duration": 1.166 }, { "text": "JORDAN: Yeah.", "start": 1761.56, "duration": 0.798 }, { "text": "AUDIENCE: Do the elements of the objects\nall have to be labeled with strings?", "start": 1762.358, "duration": 6.474 }, { "text": "Like if a key were numbered,\nsay, would that work?", "start": 1768.832, "duration": 3.984 }, { "text": "JORDAN: Um, So anything, so anything\nhere, is interpreted as a string.", "start": 1772.816, "duration": 6.134 }, { "text": "So say, we were to do, like this,", "start": 1778.95, "duration": 5.51 }, { "text": "That would be, basically,\none as a string.", "start": 1784.46, "duration": 2.59 }, { "text": "So that, this value here\nwill be cast as a string.", "start": 1787.05, "duration": 2.5 }, { "text": "And that's what will be used as the key.", "start": 1789.55, "duration": 2.51 }, { "text": "So the question was, can\nwe use numbers, or anything", "start": 1792.06, "duration": 2.25 }, { "text": "other than strings as keys in objects?", "start": 1794.31, "duration": 2.25 }, { "text": "And the answer is, kind of,\nbecause everything will just", "start": 1796.56, "duration": 2.82 }, { "text": "be cast to a string.", "start": 1799.38, "duration": 1.81 }, { "text": "Yeah, great question.", "start": 1801.19, "duration": 1.73 }, { "text": "Let's actually copy and\npaste this into our browser.", "start": 1802.92, "duration": 3.982 }, { "text": "And we confirm that it works.", "start": 1806.902, "duration": 1.208 }, { "text": "And so, how might we go about\ngetting those values back out?", "start": 1814.24, "duration": 3.99 }, { "text": "So, it's basically the\nsame way we got them in.", "start": 1818.23, "duration": 2.13 }, { "text": "So if we do dot, we can see, oh, these\nare all, this is the browser saying,", "start": 1820.36, "duration": 4.34 }, { "text": "oh, these are all the\nkeys of the object.", "start": 1824.7, "duration": 2.05 }, { "text": "So I can do o3.address, and it\nwill give me back the object.", "start": 1826.75, "duration": 4.812 }, { "text": "And say we want to get\nthis number out of here.", "start": 1831.562, "duration": 1.958 }, { "text": "How might we do that?", "start": 1833.52, "duration": 0.875 }, { "text": "[TYPING]", "start": 1834.395, "duration": 2.803 }, { "text": "AUDIENCE: Dot and the number.", "start": 1837.198, "duration": 1.412 }, { "text": "JORDAN: Exactly, dot number.", "start": 1838.61, "duration": 2.79 }, { "text": "Alternatively, we could\nhave also done this.", "start": 1841.4, "duration": 1.92 }, { "text": "[TYPING]", "start": 1843.32, "duration": 2.34 }, { "text": "And gotten the same thing.", "start": 1845.66, "duration": 2.73 }, { "text": "Any questions with objects?", "start": 1848.39, "duration": 1.2 }, { "text": "Yeah.", "start": 1852.031, "duration": 0.499 }, { "text": "AUDIENCE: Is there a\nconventional way to do that?", "start": 1852.53, "duration": 3.325 }, { "text": "Or is it kind of like reference?", "start": 1855.855, "duration": 2.604 }, { "text": "JORDAN: To do what?", "start": 1858.459, "duration": 0.791 }, { "text": "AUDIENCE: Just between dot\nnumber and [INAUDIBLE]..", "start": 1859.25, "duration": 4.248 }, { "text": "JORDAN: Yes, so the question\nis, is there a conventional way", "start": 1865.75, "duration": 2.5 }, { "text": "to get values out of objects?", "start": 1868.25, "duration": 3.48 }, { "text": "Generally the convention is\nto use that dot notation.", "start": 1871.73, "duration": 2.52 }, { "text": "So say we wanted\no3.address.number, the convention", "start": 1874.25, "duration": 5.4 }, { "text": "would be to use the dot\nboth of these times.", "start": 1879.65, "duration": 2.78 }, { "text": "But say we didn't know exactly\nwhat we wanted out of it.", "start": 1882.43, "duration": 3.82 }, { "text": "We could, so say we had this--", "start": 1886.25, "duration": 3.25 }, { "text": "[TYPING]", "start": 1889.5, "duration": 3.5 }, { "text": "Something like that, where\nwe have some dynamic key.", "start": 1895.994, "duration": 2.166 }, { "text": "Where the key we didn't\nactually know, that's", "start": 1898.16, "duration": 1.89 }, { "text": "when you actually have to\nuse the bracket notation.", "start": 1900.05, "duration": 3.51 }, { "text": "Where we have o3.address, and\nthen we pass into it that key.", "start": 1903.56, "duration": 8.13 }, { "text": "Since key is a dynamic,\nsince it's a variable,", "start": 1911.69, "duration": 1.93 }, { "text": "we don't know what it is, we have to\nthen use that bracket notation rather", "start": 1913.62, "duration": 3.083 }, { "text": "than the dot.", "start": 1916.703, "duration": 1.277 }, { "text": "Great question.", "start": 1917.98, "duration": 0.96 }, { "text": "Yeah.", "start": 1918.94, "duration": 0.681 }, { "text": "AUDIENCE: Um-- You cannot\ndo 0.1 for example.", "start": 1919.621, "duration": 3.768 }, { "text": "[INAUDIBLE]", "start": 1923.389, "duration": 3.301 }, { "text": "JORDAN: So, if we did o.1, yeah,\nso, o.1, 1 here, since we're", "start": 1926.69, "duration": 5.22 }, { "text": "typing it here, is a number.", "start": 1931.91, "duration": 2.16 }, { "text": "And we cannot set keys to numbers.", "start": 1934.07, "duration": 2.67 }, { "text": "But if it were an object\nliteral like this,", "start": 1936.74, "duration": 2.16 }, { "text": "[TYPING]", "start": 1938.9, "duration": 2.16 }, { "text": "JORDAN: It considers this not to\nbe a number, but rather a string.", "start": 1941.06, "duration": 4.71 }, { "text": "AUDIENCE: You can the array assignment\nby putting brackets around the one", "start": 1945.77, "duration": 4.95 }, { "text": "and passing it as a\nstring, so you can do 0 dot", "start": 1950.72, "duration": 2.475 }, { "text": "[TYPING]", "start": 1953.195, "duration": 1.485 }, { "text": "AUDIENCE: Our brackets\naround [INAUDIBLE] o", "start": 1954.68, "duration": 5.692 }, { "text": "dot open bracket, open\nbraces, and then, quotes, one.", "start": 1960.372, "duration": 6.683 }, { "text": "And then set that to a value.", "start": 1967.055, "duration": 4.005 }, { "text": "JORDAN: So, I think you mean this?", "start": 1971.06, "duration": 1.85 }, { "text": "[TYPING]", "start": 1972.91, "duration": 5.0 }, { "text": "Yep.", "start": 1977.91, "duration": 0.5 }, { "text": "And I believe you can also do this.", "start": 1981.362, "duration": 1.458 }, { "text": "[TYPING]", "start": 1982.82, "duration": 1.62 }, { "text": "Because that will get cast to a number.", "start": 1984.44, "duration": 2.15 }, { "text": "I mean a string.", "start": 1986.59, "duration": 2.79 }, { "text": "Any other questions?", "start": 1989.38, "duration": 3.415 }, { "text": "Cool-- yeah?", "start": 1992.795, "duration": 0.956 }, { "text": "AUDIENCE: If you have want the number\none inside the bracket notation,", "start": 1993.751, "duration": 5.757 }, { "text": "versus the string one\ninside the bracket notation,", "start": 1999.508, "duration": 3.283 }, { "text": "it will be treated as the same keys?", "start": 2002.791, "duration": 1.879 }, { "text": "JORDAN: Yeah, so the question is, what\nis the difference between this here,", "start": 2004.67, "duration": 3.79 }, { "text": "and this here?", "start": 2008.46, "duration": 1.7 }, { "text": "The difference is, anything\nbetween the brackets", "start": 2010.16, "duration": 4.12 }, { "text": "will get coerced into a string.", "start": 2014.28, "duration": 1.99 }, { "text": "And so, since this is already a\nstring, it's just that string one.", "start": 2016.27, "duration": 3.756 }, { "text": "Since this is not of\ntype string, it actually", "start": 2020.026, "duration": 1.874 }, { "text": "gets implicitly coerced into a string.", "start": 2021.9, "duration": 3.04 }, { "text": "And so, like we saw earlier, if\nwe did one plus some empty string,", "start": 2024.94, "duration": 4.16 }, { "text": "we get back the string one.", "start": 2029.1, "duration": 1.59 }, { "text": "So this becomes a string one,\nand it will index into that.", "start": 2030.69, "duration": 3.06 }, { "text": "Whereas, this number\nhere, so if we did o3.1,", "start": 2033.75, "duration": 3.427 }, { "text": "[TYPING]", "start": 2037.177, "duration": 3.663 }, { "text": "That one does not actually get coerced,\nlike it does between the brackets here.", "start": 2040.84, "duration": 6.3 }, { "text": "So JavaScript is basically saying,\nhey, this doesn't really make sense.", "start": 2047.14, "duration": 3.99 }, { "text": "I need a string here, not a number.", "start": 2051.13, "duration": 3.7 }, { "text": "Cool, any other questions?", "start": 2054.83, "duration": 1.532 }, { "text": "Great-- So, let's talk a little\nbit about this thing, where", "start": 2063.742, "duration": 4.897 }, { "text": "I was talking about mutating objects.", "start": 2068.639, "duration": 5.127 }, { "text": "So say--", "start": 2073.766, "duration": 0.499 }, { "text": "[TYPING]", "start": 2074.265, "duration": 3.225 }, { "text": "So, say I had this object, so--", "start": 2083.38, "duration": 2.224 }, { "text": "In it we had, a gets a, b gets b.", "start": 2089.54, "duration": 5.28 }, { "text": "So say I wanted to change\na to be something else?", "start": 2097.42, "duration": 3.58 }, { "text": "How might I do that?", "start": 2101.0, "duration": 1.02 }, { "text": "Yes.", "start": 2105.95, "duration": 0.51 }, { "text": "AUDIENCE: dot a equals--", "start": 2106.46, "duration": 1.0 }, { "text": "JORDAN: Yes, so I can update\nthis to be anything else.", "start": 2107.46, "duration": 2.46 }, { "text": "[TYPING]", "start": 2109.92, "duration": 3.24 }, { "text": "Cool, but say I actually did this.", "start": 2113.16, "duration": 1.875 }, { "text": "[TYPING]", "start": 2115.035, "duration": 2.715 }, { "text": "Anybody care to guess what\nwould be console logged here?", "start": 2128.59, "duration": 2.84 }, { "text": "So basically, what he's doing\nis, we're creating a new object.", "start": 2133.99, "duration": 2.72 }, { "text": "And storing it in o.", "start": 2136.71, "duration": 0.96 }, { "text": "This object has two keys, a and b, where\ntheir values are a and b respectively.", "start": 2137.67, "duration": 4.41 }, { "text": "I'm creating this new object\ncalled o2, and assign a value of o.", "start": 2142.08, "duration": 5.49 }, { "text": "I then go reset o.", "start": 2147.57, "duration": 2.46 }, { "text": "Not o2 but o.a to be a new value.", "start": 2150.03, "duration": 2.67 }, { "text": "And I'm going to console log o2.a.", "start": 2152.7, "duration": 2.88 }, { "text": "Anybody care to guess what\nthis is going to console log?", "start": 2155.58, "duration": 2.59 }, { "text": "Yeah.", "start": 2158.17, "duration": 0.5 }, { "text": "AUDIENCE: a?", "start": 2158.67, "duration": 1.5 }, { "text": "JORDAN: So, a guess is a.", "start": 2160.17, "duration": 1.71 }, { "text": "What would be the alternative guess?", "start": 2161.88, "duration": 2.937 }, { "text": "AUDIENCE: New value.", "start": 2164.817, "duration": 0.833 }, { "text": "JORDAN: New value--", "start": 2165.65, "duration": 2.113 }, { "text": "[INAUDIBLE]", "start": 2167.763, "duration": 1.867 }, { "text": "JORDAN: Yeah, so let's run that.", "start": 2169.63, "duration": 2.27 }, { "text": "So you get new value.", "start": 2171.9, "duration": 0.93 }, { "text": "And so I talked about this thing\ncalled passing by reference,", "start": 2172.83, "duration": 2.541 }, { "text": "and passing by value.", "start": 2175.371, "duration": 2.659 }, { "text": "So, basically what's happening\nhere, is that o is being said,", "start": 2178.03, "duration": 6.08 }, { "text": "hey, give me a new object, somewhere.", "start": 2184.11, "duration": 4.14 }, { "text": "And then store inside of it, a and b.", "start": 2188.25, "duration": 2.64 }, { "text": "And then o2 says hey, give me\nanother object and set it to o.", "start": 2190.89, "duration": 4.11 }, { "text": "And rather then creating a new\nobject, with the same keys and values,", "start": 2195.0, "duration": 3.33 }, { "text": "it's actually pointing\nto that same object.", "start": 2198.33, "duration": 2.279 }, { "text": "So this is a case where\nthings are getting stored", "start": 2200.609, "duration": 2.041 }, { "text": "by reference, rather than by value.", "start": 2202.65, "duration": 2.15 }, { "text": "Meaning, so in CS50, we talked\na little bit about pointers,", "start": 2204.8, "duration": 3.22 }, { "text": "and this is the exact same concept.", "start": 2208.02, "duration": 2.37 }, { "text": "Where these objects are not stored\nas entire serialized objects,", "start": 2210.39, "duration": 4.96 }, { "text": "but rather as references\nto these objects in memory.", "start": 2215.35, "duration": 3.53 }, { "text": "So o and o2 are both referencing\nthat same exact object.", "start": 2218.88, "duration": 3.7 }, { "text": "So when we go back and say,\nhey update o.a to be new value,", "start": 2222.58, "duration": 4.71 }, { "text": "it's changing this\nobject here, and still", "start": 2227.29, "duration": 2.06 }, { "text": "o and o2 are both pointing\nto that same object.", "start": 2229.35, "duration": 3.43 }, { "text": "So if I were to have updated 02\nhere, and console logged o.a,", "start": 2232.78, "duration": 6.74 }, { "text": "we would get that same result. Because o\nand o2 are still both referring to that", "start": 2239.52, "duration": 4.26 }, { "text": "same object in memory, and we're\nstill updating that object here.", "start": 2243.78, "duration": 3.97 }, { "text": "Does that make sense?", "start": 2247.75, "duration": 2.65 }, { "text": "Yeah.", "start": 2250.4, "duration": 0.61 }, { "text": "AUDIENCE: So what if you\nwanted them to be different.", "start": 2251.01, "duration": 2.208 }, { "text": "Like, what if you wanted them\nto not point to the same thing?", "start": 2253.218, "duration": 2.662 }, { "text": "JORDAN: Yeah, so the\nquestion is, what if we", "start": 2255.88, "duration": 1.95 }, { "text": "wanted them to have the same\nvalue but be different references?", "start": 2257.83, "duration": 3.18 }, { "text": "How might we do that?", "start": 2261.01, "duration": 1.26 }, { "text": "There are two different ways.", "start": 2262.27, "duration": 2.14 }, { "text": "One, the more annoying way, would just\nbe to type the whole thing out again.", "start": 2264.41, "duration": 4.58 }, { "text": "[TYPING]", "start": 2268.99, "duration": 2.916 }, { "text": "So now we're guaranteed\nthat o and o2 are", "start": 2274.002, "duration": 1.708 }, { "text": "going to be different references to\nan object that is basically the same.", "start": 2275.71, "duration": 6.42 }, { "text": "And another way would be --", "start": 2282.13, "duration": 1.6 }, { "text": "there are multiple\ndifferent ways to do this.", "start": 2283.73, "duration": 2.25 }, { "text": "The most common way in pure\nJavaScript, would be to do this--", "start": 2285.98, "duration": 3.59 }, { "text": "[TYPING]", "start": 2289.57, "duration": 3.486 }, { "text": "Whereby, object assigned is\nbasically saying, hey, pass in to me", "start": 2297.54, "duration": 4.315 }, { "text": "a bunch of arguments.", "start": 2301.855, "duration": 0.875 }, { "text": "And every single argument, I'm going\nto merge into the previous one,", "start": 2302.73, "duration": 3.33 }, { "text": "those lose keys and values.", "start": 2306.06, "duration": 1.7 }, { "text": "And so this is saying,\ngive me a brand new object.", "start": 2307.76, "duration": 3.14 }, { "text": "So I'm using the object literal\nhere to mean a new object.", "start": 2310.9, "duration": 3.05 }, { "text": "And then merge into it, of the keys\nand values of this object called o.", "start": 2313.95, "duration": 4.4 }, { "text": "And so this is basically\nsaying, give me new object,", "start": 2318.35, "duration": 3.08 }, { "text": "and then set all of the keys\nand values of o to be in there.", "start": 2321.43, "duration": 3.92 }, { "text": "So this is the way of cloning an object.", "start": 2325.35, "duration": 3.6 }, { "text": "But say we actually did this", "start": 2328.95, "duration": 1.31 }, { "text": "[TYPING]", "start": 2330.26, "duration": 3.486 }, { "text": "So, what do we expect\nthis to now print out?", "start": 2369.17, "duration": 1.9 }, { "text": "So, we mentioned that line nine,\nwe're taking the keys and values of o,", "start": 2374.31, "duration": 7.38 }, { "text": "and merging those into a new object.", "start": 2381.69, "duration": 4.2 }, { "text": "And then that line 11 we're\ntaking o2, getting the dot object,", "start": 2385.89, "duration": 3.93 }, { "text": "so accessing the value\nwith the key called object.", "start": 2389.82, "duration": 2.94 }, { "text": "And then setting that object's\nkey called key to new value.", "start": 2392.76, "duration": 4.47 }, { "text": "And then now console\nlogging o to object dot key.", "start": 2397.23, "duration": 4.1 }, { "text": "Yeah.", "start": 2401.33, "duration": 1.44 }, { "text": "AUDIENCE: So, new value?", "start": 2402.77, "duration": 2.81 }, { "text": "JORDAN: Yes, so this, so\nthe guess is new value,", "start": 2405.58, "duration": 2.0 }, { "text": "and that is absolutely correct.", "start": 2407.58, "duration": 2.79 }, { "text": "So this, so-- line nine here is\ndoing what's called a shallow copy.", "start": 2410.37, "duration": 4.62 }, { "text": "Which is just grabbing the\nkeys and values of some object,", "start": 2414.99, "duration": 3.09 }, { "text": "and just setting those blindly\ninto some other object.", "start": 2418.08, "duration": 3.33 }, { "text": "As opposed to what would\nbe called a deep copy.", "start": 2421.41, "duration": 3.24 }, { "text": "Where that would take\nthe keys and values.", "start": 2424.65, "duration": 2.822 }, { "text": "And then if the values\nare objects, you'd", "start": 2427.472, "duration": 1.708 }, { "text": "also take those objects keys and values.", "start": 2429.18, "duration": 1.666 }, { "text": "Do that recursively, and basically\nget every single layer deep cloned.", "start": 2430.846, "duration": 5.234 }, { "text": "But since object assigned just\ntakes the keys and values dumbly,", "start": 2436.08, "duration": 4.23 }, { "text": "if we have an object in there,\nupdate that object's key.", "start": 2440.31, "duration": 5.34 }, { "text": "o.obj and o2.obj are still referencing\nthat same object in memory,", "start": 2445.65, "duration": 4.38 }, { "text": "so since we updated--", "start": 2450.03, "duration": 1.23 }, { "text": "we mutated that object, it\nwould update in both o2 and o.", "start": 2451.26, "duration": 5.13 }, { "text": "Does that makes sense?", "start": 2456.39, "duration": 3.385 }, { "text": "Great, any questions about this?", "start": 2459.775, "duration": 3.495 }, { "text": "Yeah.", "start": 2463.27, "duration": 0.5 }, { "text": "AUDIENCE: How would you do a deep copy?", "start": 2463.77, "duration": 1.74 }, { "text": "JORDAN: So, how would you do deep copy?", "start": 2465.51, "duration": 2.16 }, { "text": "That's a great question.", "start": 2467.67, "duration": 1.0 }, { "text": "There are multiple different ways.", "start": 2468.67, "duration": 2.91 }, { "text": "So most people would say use a library.", "start": 2471.58, "duration": 3.32 }, { "text": "Meaning, rather than implementing\nthis thing on your own,", "start": 2474.9, "duration": 2.85 }, { "text": "just take somebody\nelse's implementation.", "start": 2477.75, "duration": 3.63 }, { "text": "But lets, let's actually do that.", "start": 2481.38, "duration": 1.56 }, { "text": "That's a good question.", "start": 2482.94, "duration": 1.06 }, { "text": "So, how would we do a deep copy?", "start": 2484.0, "duration": 2.116 }, { "text": "[TYPING]", "start": 2486.116, "duration": 3.332 }, { "text": "So, I know we haven't talked\na ton about JavaScript yet,", "start": 2490.88, "duration": 3.0 }, { "text": "but lets actually tried\nto do this together.", "start": 2493.88, "duration": 2.35 }, { "text": "So, let's call a function, deep copy.", "start": 2496.23, "duration": 4.217 }, { "text": "And we're going to pass\ninto it some object.", "start": 2500.447, "duration": 1.833 }, { "text": "And how would we implement\nthis, if we're guaranteed", "start": 2506.17, "duration": 3.1 }, { "text": "that no objects have values of objects?", "start": 2509.27, "duration": 4.5 }, { "text": "Meaning we are guaranteed not\nto have objects within objects.", "start": 2513.77, "duration": 4.435 }, { "text": "How might we do this?", "start": 2518.205, "duration": 0.875 }, { "text": "AUDIENCE: Check for the type of the key.", "start": 2519.08, "duration": 2.42 }, { "text": "JORDAN: Yes, so we can check\nfor the type of every key.", "start": 2521.5, "duration": 2.291 }, { "text": "But if we're guaranteed that no\nvalues are going to be objects,", "start": 2523.791, "duration": 2.939 }, { "text": "we can just do a shallow copy right?", "start": 2526.73, "duration": 3.81 }, { "text": "Yeah.", "start": 2530.54, "duration": 1.631 }, { "text": "AUDIENCE: So check every value?", "start": 2532.171, "duration": 1.491 }, { "text": "And if it's, the value is [INAUDIBLE]\nwe have to recursively deep copy that.", "start": 2533.662, "duration": 5.478 }, { "text": "JORDAN: Yeah.", "start": 2539.14, "duration": 0.58 }, { "text": "So hold that thought.", "start": 2539.72, "duration": 0.874 }, { "text": "Let's actually implement\nthis as if we know that there", "start": 2540.594, "duration": 3.596 }, { "text": "are no objects inside of objects.", "start": 2544.19, "duration": 2.14 }, { "text": "So if that were true, we could\njust return the shallow copy right?", "start": 2546.33, "duration": 3.22 }, { "text": "So object.assign", "start": 2549.55, "duration": 2.515 }, { "text": "[TYPING]", "start": 2552.065, "duration": 3.123 }, { "text": "So this would be a perfectly\nvalid implementation,", "start": 2557.527, "duration": 2.083 }, { "text": "if we knew that there's no such\nthing as objects within objects.", "start": 2559.61, "duration": 4.08 }, { "text": "But since there are, we're going\nto have to do some magic here.", "start": 2563.69, "duration": 4.66 }, { "text": "So, can you repeat your\nrecommendation again?", "start": 2568.35, "duration": 4.116 }, { "text": "AUDIENCE: So, check if the--", "start": 2572.466, "duration": 3.992 }, { "text": "any of the values is an object.", "start": 2576.458, "duration": 3.992 }, { "text": "Then you can't just take\nthat value into a new object,", "start": 2580.45, "duration": 5.489 }, { "text": "just to reference the same object,\nwe need to deep copy that object,", "start": 2585.939, "duration": 7.984 }, { "text": "instead of just taking [INAUDIBLE].", "start": 2593.923, "duration": 3.992 }, { "text": "JORDAN: So basically, so\ncheck every single value,", "start": 2612.96, "duration": 3.537 }, { "text": "and see if it's an object.", "start": 2616.497, "duration": 1.083 }, { "text": "If it is an object, then go\nahead and deep copy that object.", "start": 2617.58, "duration": 3.3 }, { "text": "Otherwise, return that value.", "start": 2620.88, "duration": 3.206 }, { "text": "Cool.", "start": 2624.086, "duration": 1.316 }, { "text": "So let's do that.", "start": 2625.402, "duration": 0.708 }, { "text": "So-- the way to get the keys of\nan object, is this function called", "start": 2626.11, "duration": 6.65 }, { "text": "object.keys", "start": 2632.76, "duration": 1.11 }, { "text": "[TYPING]", "start": 2633.87, "duration": 3.318 }, { "text": "So, so by doing object.keys\nand passing in the object,", "start": 2641.46, "duration": 3.39 }, { "text": "we now have an array full of the string\nvalues of the keys in that object.", "start": 2644.85, "duration": 5.68 }, { "text": "And so what we're going to do is, we're\ngoing to iterate through those keys.", "start": 2650.53, "duration": 3.39 }, { "text": "Check to see if the\nvalue is of an object.", "start": 2653.92, "duration": 3.29 }, { "text": "If so, we'll go ahead and clone that.", "start": 2657.21, "duration": 2.01 }, { "text": "Let's not worry about functions, or\nany of those other things for now.", "start": 2659.22, "duration": 3.96 }, { "text": "Otherwise, just return that value.", "start": 2663.18, "duration": 3.61 }, { "text": "So, let's have this for loop--", "start": 2666.79, "duration": 1.25 }, { "text": "[TYPING]", "start": 2668.04, "duration": 3.479 }, { "text": "And do that.", "start": 2680.47, "duration": 0.52 }, { "text": "So first, let's define--", "start": 2680.99, "duration": 1.49 }, { "text": "[TYPING]", "start": 2682.48, "duration": 3.246 }, { "text": "The new objects that\nwe're going to return.", "start": 2689.449, "duration": 1.791 }, { "text": "And let's just start with\nan empty object for now.", "start": 2691.24, "duration": 4.18 }, { "text": "And so, now we have to check to see\nif each of these values is an object.", "start": 2695.42, "duration": 4.07 }, { "text": "If so, copy it, otherwise return.", "start": 2699.49, "duration": 2.11 }, { "text": "So how are we going to check\nthe type of a particular key?", "start": 2701.6, "duration": 5.345 }, { "text": "AUDIENCE: Type of operator.", "start": 2706.945, "duration": 1.125 }, { "text": "JORDAN: Yeah, exactly,\nthat type of operator.", "start": 2708.07, "duration": 1.875 }, { "text": "So we can do if the type of\nobj, and then pass in by keys.", "start": 2709.945, "duration": 6.877 }, { "text": "[TYPING]", "start": 2716.822, "duration": 2.892 }, { "text": "So what do we want to check against?", "start": 2724.331, "duration": 1.499 }, { "text": "[TYPING]", "start": 2725.83, "duration": 3.199 }, { "text": "All right, what am I doing wrong here?", "start": 2730.4, "duration": 2.122 }, { "text": "AUDIENCE: Three equals.", "start": 2732.522, "duration": 1.608 }, { "text": "JORDAN: Yeah, we should always\nuse that three equal signs.", "start": 2734.13, "duration": 1.53 }, { "text": "In this case it would matter.", "start": 2735.66, "duration": 1.208 }, { "text": "But, we should just get in\nthe habit of doing that.", "start": 2736.868, "duration": 2.512 }, { "text": "I mean so if you notice here, we\nactually have bracket notation", "start": 2739.38, "duration": 3.09 }, { "text": "within bracket notation.", "start": 2742.47, "duration": 1.08 }, { "text": "That is totally fine.", "start": 2743.55, "duration": 2.361 }, { "text": "Cool.", "start": 2745.911, "duration": 0.499 }, { "text": "So, if something is an object\nto what are we going to turn?", "start": 2746.41, "duration": 3.5 }, { "text": "We can do object, new\nobject with that key.", "start": 2749.91, "duration": 6.59 }, { "text": "[TYPING]", "start": 2756.5, "duration": 3.458 }, { "text": "Equals-- what?", "start": 2766.39, "duration": 1.48 }, { "text": "AUDIENCE: The value?", "start": 2774.11, "duration": 1.44 }, { "text": "Oh no, I'm sorry, deep copy.", "start": 2775.55, "duration": 1.45 }, { "text": "JORDAN: Yeah, let's actually\ndeep copy that value to two.", "start": 2777.0, "duration": 2.374 }, { "text": "[TYPING]", "start": 2779.374, "duration": 3.206 }, { "text": "Otherwise--", "start": 2787.421, "duration": 0.499 }, { "text": "[TYPING]", "start": 2787.92, "duration": 3.19 }, { "text": "We can just set it\nequal to the other key.", "start": 2793.01, "duration": 2.472 }, { "text": "[TYPING]", "start": 2795.482, "duration": 3.297 }, { "text": "And then at the very end, we\ncan just return that new object.", "start": 2801.61, "duration": 3.44 }, { "text": "[TYPING]", "start": 2805.05, "duration": 3.283 }, { "text": "Cool.", "start": 2812.56, "duration": 1.66 }, { "text": "Anybody see any bugs?", "start": 2814.22, "duration": 2.29 }, { "text": "Candy opportunity.", "start": 2816.51, "duration": 2.516 }, { "text": "All right, let's just\ngo ahead and test this.", "start": 2819.026, "duration": 3.714 }, { "text": "So-- let's do--", "start": 2822.74, "duration": 4.26 }, { "text": "Do copy o--", "start": 2833.776, "duration": 0.934 }, { "text": "Then update o--", "start": 2838.646, "duration": 2.46 }, { "text": "[TYPING]", "start": 2841.106, "duration": 3.444 }, { "text": "o.obj.key Let's say of to new key.", "start": 2846.03, "duration": 4.36 }, { "text": "[TYPING]", "start": 2850.39, "duration": 3.43 }, { "text": "o3.obj.key.", "start": 2856.76, "duration": 0.98 }, { "text": "Get rid of that.", "start": 2863.82, "duration": 0.75 }, { "text": "All right, so moment of truth--", "start": 2868.38, "duration": 2.163 }, { "text": "[TYPING]", "start": 2870.543, "duration": 3.247 }, { "text": "Key, rather than new key.", "start": 2873.79, "duration": 1.06 }, { "text": "So we did it.", "start": 2874.85, "duration": 2.675 }, { "text": "Whew.", "start": 2877.525, "duration": 1.875 }, { "text": "All right, any other questions about--", "start": 2879.4, "duration": 1.86 }, { "text": "objects, mutating,\nreferences, any of that stuff?", "start": 2883.9, "duration": 2.97 }, { "text": "No, great.", "start": 2890.55, "duration": 1.03 }, { "text": "So, arrays as well, are\nalso stored by reference.", "start": 2891.58, "duration": 4.3 }, { "text": "So if we were to do\nthe same exact example,", "start": 2895.88, "duration": 2.01 }, { "text": "and rather than updating the\nobject, we updated that array,", "start": 2897.89, "duration": 4.054 }, { "text": "we'd end up with the same exact results.", "start": 2901.944, "duration": 1.666 }, { "text": "And so, if we were to update our\ndeep copy function to also take", "start": 2903.61, "duration": 5.58 }, { "text": "care of arrays, all we have to\ndo is also check, rather than", "start": 2909.19, "duration": 3.761 }, { "text": "checking object, also check against\narrays or any other data types", "start": 2912.951, "duration": 2.749 }, { "text": "that we're going to check.", "start": 2915.7, "duration": 1.083 }, { "text": "Let's move on to prototype inheritance.", "start": 2920.5, "duration": 3.57 }, { "text": "So what exactly is\nprototype inheritance?", "start": 2924.07, "duration": 1.96 }, { "text": "Well, so non-primitive types\nhave a few properties and methods", "start": 2926.03, "duration": 3.26 }, { "text": "associated with them.", "start": 2929.29, "duration": 1.68 }, { "text": "So, array, we have this\nthing called array.push,", "start": 2930.97, "duration": 2.58 }, { "text": "which will add values to an array.", "start": 2933.55, "duration": 1.54 }, { "text": "So, say we have something like--", "start": 2935.09, "duration": 2.076 }, { "text": "[TYPING]", "start": 2937.166, "duration": 2.294 }, { "text": "An empty array, if we did array.push--", "start": 2939.46, "duration": 2.73 }, { "text": "some value, then array\nnow has something in it.", "start": 2942.19, "duration": 5.631 }, { "text": "If we were to push\nanother value into it--", "start": 2947.821, "duration": 1.749 }, { "text": "It now has two values in it.", "start": 2952.435, "duration": 1.315 }, { "text": "So array.prototype.push\nis a method that we", "start": 2953.75, "duration": 3.77 }, { "text": "have available on all arrays, that\njust adds new values to an array.", "start": 2957.52, "duration": 4.96 }, { "text": "Another one would be like,\nstring.prototype.toUpperCase.", "start": 2962.48, "duration": 3.175 }, { "text": "So, say we were to\nhave some string, so--", "start": 2965.655, "duration": 2.545 }, { "text": "[TYPING]", "start": 2968.2, "duration": 2.591 }, { "text": "If you do str.toUpperCase--", "start": 2971.29, "duration": 3.86 }, { "text": "Now we, we're left with a new\nstring with all uppercase.", "start": 2975.15, "duration": 3.45 }, { "text": "So, these are just functions that we\ncan invoke on any non-primitive that", "start": 2978.6, "duration": 4.31 }, { "text": "gives us something else.", "start": 2982.91, "duration": 2.64 }, { "text": "That is available to all\nnon-primitives of a given type.", "start": 2985.55, "duration": 5.13 }, { "text": "So each object stores a\nreference to its prototype.", "start": 2990.68, "duration": 2.49 }, { "text": "Meaning, it has all the-- it\nknows about all of these methods.", "start": 2993.17, "duration": 4.38 }, { "text": "And it stores a reference to the object\nin order to know where these methods--", "start": 2997.55, "duration": 6.51 }, { "text": "the code to actually the run that lies.", "start": 3004.06, "duration": 4.41 }, { "text": "And say we have a\nprototype chain where there", "start": 3008.47, "duration": 3.78 }, { "text": "are a bunch of different\nmethods of the same name.", "start": 3012.25, "duration": 2.46 }, { "text": "Whichever one is bound most tightly\nto the instance has the priority.", "start": 3014.71, "duration": 4.3 }, { "text": "So say we have an object in\nan array, where array is the--", "start": 3019.01, "duration": 9.68 }, { "text": "So say we have a value that is of\ntype array, up the prototype chain", "start": 3028.69, "duration": 3.3 }, { "text": "we have arrays, its prototype is\narray, that prototype is object.", "start": 3031.99, "duration": 4.96 }, { "text": "Say we have the same named\nmethod on both of these.", "start": 3036.95, "duration": 2.717 }, { "text": "If we call that method, the one\nthat's bound most tightly the,", "start": 3039.667, "duration": 2.583 }, { "text": "array will take priority.", "start": 3042.25, "duration": 2.08 }, { "text": "So let's actually show that.", "start": 3044.33, "duration": 1.25 }, { "text": "So, say we have something like--", "start": 3045.58, "duration": 2.64 }, { "text": "So array, it has a\nreference to its prototypes.", "start": 3052.69, "duration": 4.0 }, { "text": "So if you did array dot, double\nunderscore, proto, double underscore,", "start": 3056.69, "duration": 5.3 }, { "text": "we see this large object with\na bunch of different functions.", "start": 3061.99, "duration": 6.0 }, { "text": "And so we see down here, push, which\nis that one that we invoked earlier.", "start": 3067.99, "duration": 7.45 }, { "text": "So this is exactly how it knows\nwhere that push implementation is.", "start": 3075.44, "duration": 4.13 }, { "text": "And so we can do\narr.__proto__.__proto__,", "start": 3079.57, "duration": 5.68 }, { "text": "and go even farther up the chain.", "start": 3085.25, "duration": 2.15 }, { "text": "So this one has a bunch of other\nones toString value of, whatever.", "start": 3087.4, "duration": 4.66 }, { "text": "And if you notice, both the arrays\nprototype, and it's arrays prototypes", "start": 3092.06, "duration": 6.14 }, { "text": "prototype, have this\nmethod called toString.", "start": 3098.2, "duration": 4.32 }, { "text": "And if I were to invoke\narr.toString which one of these", "start": 3102.52, "duration": 5.22 }, { "text": "is going to actually get called?", "start": 3107.74, "duration": 1.47 }, { "text": "AUDIENCE: The second one.", "start": 3113.575, "duration": 1.287 }, { "text": "JORDAN: The second one?", "start": 3114.862, "duration": 0.958 }, { "text": "Which one is the second one?", "start": 3115.82, "duration": 0.96 }, { "text": "AUDIENCE: Dot proto, dot proto.", "start": 3116.78, "duration": 1.291 }, { "text": "[INAUDIBLE]", "start": 3118.071, "duration": 0.631 }, { "text": "JORDAN: Dot proto, dot proto.", "start": 3118.702, "duration": 1.208 }, { "text": "So, actually the opposite.", "start": 3119.91, "duration": 2.17 }, { "text": "So, since the toString,\non the array prototype,", "start": 3122.08, "duration": 5.98 }, { "text": "is more specific than the toSting\nmethod on the object prototype,", "start": 3128.06, "duration": 4.14 }, { "text": "this one is going to get invoked.", "start": 3132.2, "duration": 3.21 }, { "text": "Because its, just because\nits more specific.", "start": 3135.41, "duration": 2.49 }, { "text": "Because an array is an\narray and its an object.", "start": 3137.9, "duration": 3.666 }, { "text": "But its more specific to call it an\narray, than to call it an object.", "start": 3141.566, "duration": 2.874 }, { "text": "So its going to invoke\nthe one on the array.", "start": 3144.44, "duration": 2.754 }, { "text": "Does that makes sense?", "start": 3147.194, "duration": 0.916 }, { "text": "Anybody have a question about that?", "start": 3150.392, "duration": 1.458 }, { "text": "It's an important concept, and\na little bit confusing at first.", "start": 3151.85, "duration": 3.46 }, { "text": "Cool.", "start": 3160.4, "duration": 0.5 }, { "text": "Most primitive types\nhad object wrappers.", "start": 3160.9, "duration": 2.91 }, { "text": "And so we talked about\nhow primitive types don't", "start": 3163.81, "duration": 3.24 }, { "text": "have any methods associated with them.", "start": 3167.05, "duration": 4.46 }, { "text": "But primitive types\nalso have wrappers that", "start": 3171.51, "duration": 2.44 }, { "text": "have prototypes associated with them.", "start": 3173.95, "duration": 3.78 }, { "text": "What the heck does that mean?", "start": 3177.73, "duration": 1.27 }, { "text": "So if I were to do\n42.toString, It's going", "start": 3179.0, "duration": 3.189 }, { "text": "to be like what the heck do you mean?", "start": 3182.189, "duration": 1.541 }, { "text": "42.toString--", "start": 3183.73, "duration": 0.541 }, { "text": "[TYPING]", "start": 3184.271, "duration": 2.309 }, { "text": "Right I told you that these\nprimitive values don't have methods.", "start": 3186.58, "duration": 5.85 }, { "text": "And so 42.toString\ndoesn't really make sense.", "start": 3192.43, "duration": 3.25 }, { "text": "But say I were to do this thing\nconst num = 42 and did num.toString,", "start": 3195.68, "duration": 5.33 }, { "text": "[TYPING]", "start": 3201.01, "duration": 1.05 }, { "text": "That will actually do something.", "start": 3202.06, "duration": 1.407 }, { "text": "And that's a little bit strange.", "start": 3203.467, "duration": 1.333 }, { "text": "This is another one of those\nJavaScript interesting behaviors.", "start": 3204.8, "duration": 4.67 }, { "text": "Because all of the primitive\nvalues have wrappers", "start": 3209.47, "duration": 3.45 }, { "text": "that give them access\nto a bunch of methods.", "start": 3212.92, "duration": 3.69 }, { "text": "And JavaScript will automatically\ndo what's called boxing for you.", "start": 3216.61, "duration": 3.98 }, { "text": "Which it says, hey, like\nI know 42 is a primitive,", "start": 3220.59, "duration": 3.222 }, { "text": "but if you call this toString\nmethod, I know what you mean.", "start": 3223.812, "duration": 2.458 }, { "text": "I'm going to box this 42\nnumber with this prototype that", "start": 3226.27, "duration": 3.93 }, { "text": "has a bunch of these methods on it.", "start": 3230.2, "duration": 2.272 }, { "text": "So if I were to do 42.toString\nthat would make sense.", "start": 3232.472, "duration": 2.208 }, { "text": "And if I num.__proto__,", "start": 3234.68, "duration": 2.81 }, { "text": "[TYPING]", "start": 3237.49, "duration": 3.13 }, { "text": "That actually exists.", "start": 3240.62, "duration": 1.31 }, { "text": "But 42.__proto__", "start": 3241.93, "duration": 0.75 }, { "text": "[TYPING]", "start": 3242.68, "duration": 3.3 }, { "text": "Does not.", "start": 3245.98, "duration": 0.878 }, { "text": "Does that make sense?", "start": 3246.858, "duration": 1.464 }, { "text": "Another way to find out if a\nvalue is an instance of some type,", "start": 3256.14, "duration": 5.23 }, { "text": "you can do this thing\ncalled x instance of number.", "start": 3261.37, "duration": 3.054 }, { "text": "And that would return false.", "start": 3264.424, "duration": 1.166 }, { "text": "Because x is actually not\nof type capital number,", "start": 3265.59, "duration": 2.87 }, { "text": "it's just boxed around that\nnumber object for your reference.", "start": 3268.46, "duration": 6.906 }, { "text": "Does that make sense?", "start": 3275.366, "duration": 0.874 }, { "text": "Again not something you're going to\nuse everyday, just something that", "start": 3279.966, "duration": 2.874 }, { "text": "is helpful to know in case you\nrun into the strange corner cases.", "start": 3282.84, "duration": 2.708 }, { "text": "Cool.", "start": 3289.52, "duration": 0.91 }, { "text": "So why would we use a\nreference to the prototype?", "start": 3290.43, "duration": 3.21 }, { "text": "And what is the alternative there?", "start": 3293.64, "duration": 1.74 }, { "text": "Anybody care to give a shot at that?", "start": 3295.38, "duration": 1.5 }, { "text": "So this is going back a little bit too\ndeep copying versus shallow copying.", "start": 3306.6, "duration": 3.9 }, { "text": "AUDIENCE: So, maybe if the\ninitial object is massive.", "start": 3310.5, "duration": 3.8 }, { "text": "And then you just want to do\nsomething, like, after it?", "start": 3314.3, "duration": 4.28 }, { "text": "JORDAN: Yeah, so if the initial object\nis massive, like, what happens then?", "start": 3318.58, "duration": 4.13 }, { "text": "So the alternative is basically\nto clone every single--", "start": 3322.71, "duration": 3.695 }, { "text": "to deep copy every single\nprototype every single time", "start": 3326.405, "duration": 2.665 }, { "text": "you couldn't new value.", "start": 3329.07, "duration": 1.29 }, { "text": "Which is, safe, because that\nnumber and all of its methods", "start": 3330.36, "duration": 5.07 }, { "text": "are all encapsulated within\nthat specific variable.", "start": 3335.43, "duration": 3.69 }, { "text": "But it's also a little bit\nexpensive in both performance,", "start": 3339.12, "duration": 2.64 }, { "text": "because you have to do that\ndeep copy every single time.", "start": 3341.76, "duration": 2.43 }, { "text": "And also of memory, because the\nobject starts to get pretty large.", "start": 3344.19, "duration": 3.16 }, { "text": "And if you have an array of\nlike 100 different things,", "start": 3347.35, "duration": 2.93 }, { "text": "all hundreds of those, deep copying\nevery single prototype gets pretty big.", "start": 3350.28, "duration": 3.9 }, { "text": "And so what is the danger of storing\na reference to the prototype,", "start": 3356.75, "duration": 3.19 }, { "text": "rather than copying the whole thing?", "start": 3359.94, "duration": 5.605 }, { "text": "AUDIENCE: I guess if\nchange it, [INAUDIBLE]..", "start": 3365.545, "duration": 3.985 }, { "text": "JORDAN: Yeah exactly.", "start": 3369.53, "duration": 1.11 }, { "text": "If you change it, then it changes\nfor every single value of that type.", "start": 3370.64, "duration": 3.66 }, { "text": "So let's do that really quick.", "start": 3376.83, "duration": 1.52 }, { "text": "So, so say we have, so we solved\nnum, which is equal to 42, right?", "start": 3378.35, "duration": 7.93 }, { "text": "And if you do num.toString,\nwhat so we expect to come out?", "start": 3386.28, "duration": 4.49 }, { "text": "42 is a string right?", "start": 3390.77, "duration": 1.67 }, { "text": "But say, some devious\nprogrammer was doing this thing", "start": 3392.44, "duration": 2.807 }, { "text": "where he did number.prototype.toString", "start": 3395.247, "duration": 1.583 }, { "text": "[TYPING]", "start": 3396.83, "duration": 2.644 }, { "text": "And you actually override that to be\nsome function that will return 100.", "start": 3402.7, "duration": 10.0 }, { "text": "Now what happens if I\ncall a num.toString?", "start": 3412.7, "duration": 3.36 }, { "text": "Wait a second.", "start": 3416.06, "duration": 1.03 }, { "text": "[TYPING]", "start": 3417.09, "duration": 1.907 }, { "text": "So that could have some\ndangerous penalties right?", "start": 3418.997, "duration": 2.083 }, { "text": "So if I were to change the\nprototype of the number class,", "start": 3421.08, "duration": 3.67 }, { "text": "even though num was declared 100\nlines prior to be the number 42.", "start": 3424.75, "duration": 6.2 }, { "text": "And we tried num.toString\nhere and it returned 42.", "start": 3430.95, "duration": 3.81 }, { "text": "If we were to change\nthe prototype later,", "start": 3434.76, "duration": 2.1 }, { "text": "it affects everything\nthat has ever happened.", "start": 3436.86, "duration": 2.97 }, { "text": "So num.toString now\nstarts returning 100.", "start": 3439.83, "duration": 2.37 }, { "text": "And everything that will happen.", "start": 3442.2, "duration": 1.36 }, { "text": "So if I were to do--", "start": 3443.56, "duration": 0.833 }, { "text": "[TYPING]", "start": 3444.393, "duration": 3.288 }, { "text": "Everything in the future\nalso has those consequences.", "start": 3452.04, "duration": 2.86 }, { "text": "So changing the prototype is\nsomething that is very dangerous,", "start": 3454.9, "duration": 2.99 }, { "text": "and is recommended against doing.", "start": 3457.89, "duration": 3.032 }, { "text": "Does that make sense?", "start": 3460.922, "duration": 1.488 }, { "text": "Cool.", "start": 3466.374, "duration": 0.5 }, { "text": "So let's actually take a short break.", "start": 3472.83, "duration": 3.1 }, { "text": "All right welcome back.", "start": 3475.93, "duration": 1.78 }, { "text": "So let's talk about scope now.", "start": 3477.71, "duration": 3.47 }, { "text": "So what the heck is scope?", "start": 3481.18, "duration": 1.2 }, { "text": "So Scope is a term that's\ntalking about variable lifetime,", "start": 3482.38, "duration": 3.42 }, { "text": "and how long these\nvariables actually exist.", "start": 3485.8, "duration": 1.875 }, { "text": "And so there are a couple\ndifferent types of scoping.", "start": 3490.162, "duration": 2.208 }, { "text": "There's lexical scoping, which is\nthat keyword var that you might", "start": 3492.37, "duration": 2.85 }, { "text": "see if you're reading old JavaScript.", "start": 3495.22, "duration": 2.84 }, { "text": "And there's block scoping, which\nrefers to how things like const or let", "start": 3498.06, "duration": 5.23 }, { "text": "are scoped.", "start": 3503.29, "duration": 1.05 }, { "text": "So lexical scoping is basically\nsaying, hey, give me a variable", "start": 3504.34, "duration": 4.27 }, { "text": "and it will exist for\nas long as since it", "start": 3508.61, "duration": 2.27 }, { "text": "was declared all the way until the\nend of a function ends or the file", "start": 3510.88, "duration": 4.8 }, { "text": "if it's in a file.", "start": 3515.68, "duration": 1.5 }, { "text": "Whereas the analog\nwould be block scoping", "start": 3517.18, "duration": 1.92 }, { "text": "where it behaves a lot like\nsomething behaves in C.", "start": 3519.1, "duration": 3.62 }, { "text": "Where basically a\nvariable will be around", "start": 3522.72, "duration": 3.19 }, { "text": "from when it's declared until the\nnext end curly brace is reached.", "start": 3525.91, "duration": 4.6 }, { "text": "And so that there is the big difference\nbetween var and const and let.", "start": 3530.51, "duration": 3.65 }, { "text": "And the difference\nbetween const and let is", "start": 3534.16, "duration": 2.01 }, { "text": "that const is something\nthat can't be updated,", "start": 3536.17, "duration": 2.7 }, { "text": "meaning if I set a variable\nto be a constant, it", "start": 3538.87, "duration": 2.82 }, { "text": "means I'm not going to\nupdate that reference later,", "start": 3541.69, "duration": 3.51 }, { "text": "whereas let can be updated.", "start": 3545.2, "duration": 2.01 }, { "text": "So if I were to do const thisIsAConst\nand set about equal to something", "start": 3547.21, "duration": 6.81 }, { "text": "like 50, if I were to say thisIsAConst\nand try to update that to be 51,", "start": 3554.02, "duration": 8.79 }, { "text": "I'm going to get an error actually\nthat says, hey, you call that a const?", "start": 3562.81, "duration": 4.49 }, { "text": "But you're trying to change it.", "start": 3567.3, "duration": 1.49 }, { "text": "That's not OK.", "start": 3568.79, "duration": 1.83 }, { "text": "And so no matter how\nI want to change it--", "start": 3570.62, "duration": 1.75 }, { "text": "say I do that plus-plus or\nsomething, that's also going to fail.", "start": 3572.37, "duration": 3.74 }, { "text": "Whereas if I did something like\nlet thisIsALet equal to 50,", "start": 3576.11, "duration": 7.26 }, { "text": "I can go ahead-- or 51, I made\na typo, but I can just say,", "start": 3583.37, "duration": 4.32 }, { "text": "hey, it was a typo, thisIsALet, let me\nchange that to 50 and we're all good.", "start": 3587.69, "duration": 7.36 }, { "text": "I mean, I can also do that plus-plus\nor change it however I want", "start": 3595.05, "duration": 3.12 }, { "text": "and it will actually update that.", "start": 3598.17, "duration": 6.16 }, { "text": "Note that I said the\nreference can't be updated.", "start": 3604.33, "duration": 4.434 }, { "text": "I did not say anything about\nthings being immutable.", "start": 3608.764, "duration": 2.166 }, { "text": "So if I get const obj equals\nthis empty object, if I then--", "start": 3610.93, "duration": 9.71 }, { "text": "I can't update it to be--", "start": 3620.64, "duration": 2.19 }, { "text": "point to a different object.", "start": 3622.83, "duration": 1.23 }, { "text": "If I try to do obj is something\nelse, I'm going to say,", "start": 3624.06, "duration": 4.32 }, { "text": "hey, you call this a const and\nyou're trying to change it.", "start": 3628.38, "duration": 3.78 }, { "text": "But I can still do obj.a and set\nthat equal to a, because why?", "start": 3632.16, "duration": 6.39 }, { "text": "Anybody care to tell me why?", "start": 3638.55, "duration": 1.65 }, { "text": "Exactly.", "start": 3646.95, "duration": 0.57 }, { "text": "So the pointer is still\npointing to the same object.", "start": 3647.52, "duration": 2.43 }, { "text": "The reference has not changed.", "start": 3649.95, "duration": 2.83 }, { "text": "So we mutated that\nobject, but it's still", "start": 3652.78, "duration": 2.577 }, { "text": "pointing to that same place in memory.", "start": 3655.357, "duration": 1.583 }, { "text": "It's still pointing to the\nobject that exists over here", "start": 3656.94, "duration": 3.26 }, { "text": "and that reference has not been changed.", "start": 3660.2, "duration": 2.44 }, { "text": "Does that distinction\nmake sense to people?", "start": 3662.64, "duration": 2.374 }, { "text": "It's a pretty important one.", "start": 3665.014, "duration": 1.166 }, { "text": "Cool So let's play a little bit\nwith these variable lifetimes.", "start": 3669.13, "duration": 4.953 }, { "text": "So I said before that if I tried to do\nsomething like this, what happens here?", "start": 3689.88, "duration": 12.04 }, { "text": "Error.", "start": 3706.275, "duration": 0.5 }, { "text": "Why?", "start": 3709.54, "duration": 0.5 }, { "text": "Because it's a constant and\nwe can't update a constant.", "start": 3710.04, "duration": 3.2 }, { "text": "And we can confirm this.", "start": 3713.24, "duration": 2.718 }, { "text": "Oh.", "start": 3715.958, "duration": 1.104 }, { "text": "It'll actually tell\nus, hey, a TypeError.", "start": 3717.062, "duration": 1.708 }, { "text": "You call this a constant variable\nbut you're trying assign it.", "start": 3718.77, "duration": 2.37 }, { "text": "That's not OK.", "start": 3721.14, "duration": 0.583 }, { "text": "But if we did--", "start": 3728.51, "duration": 0.82 }, { "text": "this is OK because the reference\nto that object did not change,", "start": 3741.05, "duration": 3.58 }, { "text": "it's just mutated it.", "start": 3744.63, "duration": 1.82 }, { "text": "On the other side, if we did something\nlike let thisIsALet A Let equal to 51,", "start": 3746.45, "duration": 7.92 }, { "text": "and then want to change that\nlater, we go 50, that's totally OK.", "start": 3754.37, "duration": 6.04 }, { "text": "But if I actually tried to\nreassign-- so let thisIsALet.", "start": 3763.3, "duration": 5.87 }, { "text": "If I tried to do this again, it's going\nto yell at me because it's saying, hey,", "start": 3769.17, "duration": 3.78 }, { "text": "you already declared\nsomething called thisIsALet.", "start": 3772.95, "duration": 2.22 }, { "text": "You cannot declare that again.", "start": 3775.17, "duration": 2.64 }, { "text": "And so const and let\nactually protect you", "start": 3777.81, "duration": 1.71 }, { "text": "from declaring something with the same\nvariable name twice, which is something", "start": 3779.52, "duration": 4.8 }, { "text": "that var does not do.", "start": 3784.32, "duration": 0.97 }, { "text": "Cool.", "start": 3804.37, "duration": 0.69 }, { "text": "What do you guys think would\nhappen if I tried to do this?", "start": 3805.06, "duration": 2.416 }, { "text": "Anybody care to guess?", "start": 3817.83, "duration": 1.03 }, { "text": "Undefined?", "start": 3821.85, "duration": 1.66 }, { "text": "So let's try to run it.", "start": 3823.51, "duration": 2.8 }, { "text": "Error.", "start": 3826.31, "duration": 1.03 }, { "text": "So since these things\nare block scoped, it", "start": 3827.34, "duration": 2.55 }, { "text": "means the variable is declared\nat the line that it is written.", "start": 3829.89, "duration": 4.59 }, { "text": "And if we try to use it before then,\nit actually does not even exist at all.", "start": 3834.48, "duration": 3.64 }, { "text": "So if I tried to console.log something\ncalled thisIsAConst here, remember,", "start": 3838.12, "duration": 3.14 }, { "text": "the JavaScript interpreter\nis just reading down", "start": 3841.26, "duration": 3.24 }, { "text": "and it won't see like, hey,\nwhat the heck is thisIsAConst?", "start": 3844.5, "duration": 2.42 }, { "text": "I have no idea what that is so\nI'm just going to Error here.", "start": 3846.92, "duration": 2.5 }, { "text": "Same thing if I tried to\ndo this as a let here.", "start": 3852.66, "duration": 3.6 }, { "text": "That will also Error\nwith the same error.", "start": 3856.26, "duration": 2.246 }, { "text": "AUDIENCE: So what's line 14 error on?", "start": 3858.506, "duration": 4.444 }, { "text": "JORDAN: Why does it error?", "start": 3862.95, "duration": 2.16 }, { "text": "Because we're trying to declare two\nvariables with the same variable", "start": 3865.11, "duration": 4.59 }, { "text": "name twice.", "start": 3869.7, "duration": 1.02 }, { "text": "So we have let thisIsALet equal\n51 here, and we try to do it again", "start": 3870.72, "duration": 3.6 }, { "text": "and it's saying, hey, you already\nhave a variable called thisIsALet,", "start": 3874.32, "duration": 3.06 }, { "text": "so I'm not going to allow\nyou to do that again.", "start": 3877.38, "duration": 2.5 }, { "text": "So it's going to be\nthis error right here.", "start": 3879.88, "duration": 3.398 }, { "text": "AUDIENCE: So it's the\ninstantiation of it?", "start": 3883.278, "duration": 2.891 }, { "text": "JORDAN: Yeah.", "start": 3886.169, "duration": 0.541 }, { "text": "It's saying like-- basically I'm\nsaying, hey, give me a variable to call,", "start": 3886.71, "duration": 3.219 }, { "text": "this is a let here, and then a\ncouple lines later I'm saying,", "start": 3889.929, "duration": 2.541 }, { "text": "hey, give me a variable\ncalled thisIsALet,", "start": 3892.47, "duration": 1.77 }, { "text": "and JavaScript says,\nhey, like, you already", "start": 3894.24, "duration": 2.19 }, { "text": "have a block scope\nvariable called thisIsALet,", "start": 3896.43, "duration": 4.23 }, { "text": "so I can't give you another one.", "start": 3900.66, "duration": 3.359 }, { "text": "However, if I tried to just update\nit like this, that's totally OK.", "start": 3904.019, "duration": 2.791 }, { "text": "Any other questions here?", "start": 3909.489, "duration": 1.041 }, { "text": "So the other thing we said, we can\nhave these things called a var.", "start": 3913.79, "duration": 3.27 }, { "text": "If I did var thisIsAVar equal to\n50, that's fine, I can update it.", "start": 3917.06, "duration": 8.01 }, { "text": "That's also fine.", "start": 3929.21, "duration": 0.87 }, { "text": "I can also do this, and\nthat won't yell at me.", "start": 3930.08, "duration": 8.35 }, { "text": "So vars are older ways\nto declare variables", "start": 3938.43, "duration": 4.437 }, { "text": "and they don't have the same\nprotection that let and const do.", "start": 3942.867, "duration": 2.583 }, { "text": "You can override them like\nthis and it's totally OK.", "start": 3945.45, "duration": 4.532 }, { "text": "And you can actually declare a whole\nnew variable with the same variable name", "start": 3949.982, "duration": 3.208 }, { "text": "and that's also OK.", "start": 3953.19, "duration": 2.67 }, { "text": "And something-- yeah?", "start": 3955.86, "duration": 0.991 }, { "text": "It just-- yeah, so that's--", "start": 3960.08, "duration": 3.11 }, { "text": "So if I try to run this,\nit will update the old one.", "start": 3983.27, "duration": 4.02 }, { "text": "It'll just replace that value.", "start": 3987.29, "duration": 1.25 }, { "text": "It's actually called shadowing,\nwhere you create a new variable", "start": 3988.54, "duration": 2.71 }, { "text": "with the same variable name\nwhich basically just overshadows", "start": 3991.25, "duration": 2.88 }, { "text": "that old one.", "start": 3994.13, "duration": 0.66 }, { "text": "So it's as if the\nother one didn't exist.", "start": 3994.79, "duration": 1.77 }, { "text": "Cool, so check this out.", "start": 4002.63, "duration": 1.17 }, { "text": "So this errors and we know this errors.", "start": 4008.22, "duration": 2.87 }, { "text": "But say I were to do this.", "start": 4011.09, "duration": 3.73 }, { "text": "You'd expect this to also\nerror, but actually it does not.", "start": 4014.82, "duration": 6.11 }, { "text": "It returns undefined.", "start": 4020.93, "duration": 2.361 }, { "text": "So this is another weird\nthing about JavaScript.", "start": 4023.291, "duration": 1.999 }, { "text": "This is called hoisting.", "start": 4025.29, "duration": 2.129 }, { "text": "And so certain things\nare hoisted, which it", "start": 4027.419, "duration": 1.791 }, { "text": "means basically it takes\nthe definition of something", "start": 4029.21, "duration": 3.63 }, { "text": "and hoists it to the very top\nof the file and does that first.", "start": 4032.84, "duration": 3.06 }, { "text": "Get", "start": 4039.07, "duration": 0.742 }, { "text": "And a few things are hoisted.", "start": 4039.812, "duration": 1.648 }, { "text": "Var the-- actually, the declaration\nof the creation of an empty variable", "start": 4041.46, "duration": 5.04 }, { "text": "are hoisted.", "start": 4046.5, "duration": 1.38 }, { "text": "Function definitions are hoisted\nas well, but const and let", "start": 4047.88, "duration": 3.28 }, { "text": "are not, as we saw if we tried to access\nthe variable name of the const or let,", "start": 4051.16, "duration": 4.01 }, { "text": "then it errors.", "start": 4055.17, "duration": 0.86 }, { "text": "But with a var, the declaration of\nthat variable is actually hoisted.", "start": 4056.03, "duration": 5.289 }, { "text": "And we can talk a little bit more\nabout how that works in a second,", "start": 4061.319, "duration": 2.791 }, { "text": "but let's actually play\nwith it a little bit more.", "start": 4064.11, "duration": 2.23 }, { "text": "So function definitions are hoisted.", "start": 4066.34, "duration": 2.33 }, { "text": "So let's clean up this\nfile a little bit.", "start": 4068.67, "duration": 3.92 }, { "text": "So let's call this\nnew function, called--", "start": 4079.332, "duration": 1.708 }, { "text": "so this is console.log.", "start": 4087.262, "duration": 1.768 }, { "text": "So I defined this function\ncalled thisIsHoisted", "start": 4104.302, "duration": 1.958 }, { "text": "at the very bottom of the file.", "start": 4106.26, "duration": 1.5 }, { "text": "And all it does is it\nconsole.logs, there's", "start": 4107.76, "duration": 2.4 }, { "text": "a function declared at\nthe bottom of the file.", "start": 4110.16, "duration": 2.01 }, { "text": "But something very interesting is at the\nvery top of this file, I can call it.", "start": 4112.17, "duration": 3.689 }, { "text": "And that will actually work.", "start": 4120.884, "duration": 1.166 }, { "text": "And so that is what's\ncalled function hoisting.", "start": 4125.229, "duration": 4.88 }, { "text": "The behavior whereby\na function definition", "start": 4130.109, "duration": 3.221 }, { "text": "declared at the very bottom of a\nfile is actually available for use", "start": 4133.33, "duration": 3.84 }, { "text": "at the very top of the file as well.", "start": 4137.17, "duration": 1.5 }, { "text": "But this does not work in other cases.", "start": 4141.34, "duration": 2.523 }, { "text": "So say I were to do something like this.", "start": 4143.863, "duration": 1.666 }, { "text": "So who can tell me what the difference\nis between line 21 and line 25?", "start": 4166.26, "duration": 4.485 }, { "text": "They look pretty similar, right?", "start": 4173.42, "duration": 1.559 }, { "text": "Yeah?", "start": 4174.979, "duration": 0.499 }, { "text": "OK.", "start": 4180.62, "duration": 0.5 }, { "text": "Yep.", "start": 4189.279, "duration": 0.5 }, { "text": "So repeating for the camera, line 25,\nthisIsHoisted is declared as a constant", "start": 4189.779, "duration": 4.921 }, { "text": "so it cannot be changed, whereas line 21\nis declared as a function and so it can", "start": 4194.7, "duration": 5.13 }, { "text": "be changed, which is absolutely correct.", "start": 4199.83, "duration": 2.59 }, { "text": "What happens if I try\nto do this up here?", "start": 4202.42, "duration": 1.98 }, { "text": "Yeah, so we're going to get an error.", "start": 4212.74, "duration": 4.38 }, { "text": "thisIsHoisted is not defined, but why?", "start": 4217.12, "duration": 4.2 }, { "text": "Because as we talked about\nearlier, these things", "start": 4221.32, "duration": 2.19 }, { "text": "called consts are not available for\nuse until they're actually declared.", "start": 4223.51, "duration": 4.74 }, { "text": "So certain things are hoisted.", "start": 4228.25, "duration": 2.11 }, { "text": "Var.", "start": 4230.36, "duration": 0.65 }, { "text": "So the declaration of these\nvariables are hoisted.", "start": 4231.01, "duration": 2.7 }, { "text": "Function definitions if they're\ndeclared like this are hoisted.", "start": 4233.71, "duration": 3.39 }, { "text": "But if we create what's\ncalled an anonymous function,", "start": 4237.1, "duration": 2.28 }, { "text": "a function without any name,\nand set that equal to--", "start": 4239.38, "duration": 4.89 }, { "text": "or assign that to be a constant,\nthen that constant is not created.", "start": 4244.27, "duration": 7.08 }, { "text": "Does that make sense?", "start": 4251.35, "duration": 3.25 }, { "text": "So what happens if I try to do this?", "start": 4254.6, "duration": 3.69 }, { "text": "Yeah, so same thing.", "start": 4258.29, "duration": 1.985 }, { "text": "It errors.", "start": 4260.275, "duration": 3.06 }, { "text": "What happens if I try to do this?", "start": 4263.335, "duration": 1.375 }, { "text": "What's hoisted?", "start": 4268.165, "duration": 0.625 }, { "text": "So notice-- so notice the\ndifference in the two errors.", "start": 4279.038, "duration": 12.062 }, { "text": "They're actually not\nthe exact same error.", "start": 4291.1, "duration": 3.07 }, { "text": "So who wants to give a guess\nat what's going on here?", "start": 4294.17, "duration": 4.45 }, { "text": "So when I declared it with a let\nthat's saying reference error,", "start": 4298.62, "duration": 3.53 }, { "text": "thisIsHoisted is not defined,\nwhereas when I use a var,", "start": 4302.15, "duration": 3.22 }, { "text": "it says TypeError,\nthisIsHoisted is not a function.", "start": 4305.37, "duration": 3.871 }, { "text": "Why might that be?", "start": 4309.241, "duration": 0.749 }, { "text": "Yeah?", "start": 4309.99, "duration": 0.5 }, { "text": "Uh huh.", "start": 4317.07, "duration": 0.99 }, { "text": "Exactly.", "start": 4324.02, "duration": 1.266 }, { "text": "So repeating for the camera., and\nI'll bring up the code as well.", "start": 4325.286, "duration": 2.708 }, { "text": "So down here, so the first time\nwhen we declared this with the let,", "start": 4333.91, "duration": 4.77 }, { "text": "it's not declared at all.", "start": 4338.68, "duration": 2.47 }, { "text": "This variable does not exist at all.", "start": 4341.15, "duration": 1.98 }, { "text": "So this is not hoisted,\nthe JavaScript does not", "start": 4343.13, "duration": 2.81 }, { "text": "know what that means on line 1.", "start": 4345.94, "duration": 2.53 }, { "text": "But when I use a var\nhere, remember, it hoists", "start": 4348.47, "duration": 3.26 }, { "text": "the declaration of this variable.", "start": 4351.73, "duration": 1.44 }, { "text": "So it creates a variable\ncalled thisIsHoisted.", "start": 4353.17, "duration": 2.58 }, { "text": "However, it does not assign it a\nvalue until line 25 is executed.", "start": 4355.75, "duration": 4.05 }, { "text": "And so at line 1, thisIsHoisted exists.", "start": 4359.8, "duration": 3.33 }, { "text": "It's just equal to undefined.", "start": 4363.13, "duration": 1.87 }, { "text": "And if I try to invoke\nit like a function,", "start": 4365.0, "duration": 2.2 }, { "text": "it says, hey, this is\nan undefined variable,", "start": 4367.2, "duration": 2.62 }, { "text": "I can't invoke it like a\nfunction, this is a TypeError.", "start": 4369.82, "duration": 3.21 }, { "text": "And so even though both of these things\nerrored, the reason that they errored", "start": 4373.03, "duration": 4.11 }, { "text": "is slightly different.", "start": 4377.14, "duration": 1.11 }, { "text": "In case one when they're\ndeclared using a const or a let,", "start": 4378.25, "duration": 4.04 }, { "text": "that variable just\ndoes not exist at all.", "start": 4382.29, "duration": 2.45 }, { "text": "However, when we declared\nusing var, the variable exists.", "start": 4384.74, "duration": 3.83 }, { "text": "It's just undefined, and so if we\ntry to invoke it like a function,", "start": 4388.57, "duration": 3.18 }, { "text": "and it says, hey, like, this is\nundefined, it's not a function.", "start": 4391.75, "duration": 4.08 }, { "text": "Does that make sense to everyone?", "start": 4395.83, "duration": 2.079 }, { "text": "So why does this happen?", "start": 4401.29, "duration": 2.84 }, { "text": "How does this happen?", "start": 4404.13, "duration": 2.03 }, { "text": "And the reason is actually\nhow JavaScript is executed.", "start": 4406.16, "duration": 3.71 }, { "text": "So there's two phases in the\nexecution of a JavaScript file.", "start": 4409.87, "duration": 4.89 }, { "text": "So before executing any code, it\nhas all of the text in front of it,", "start": 4414.76, "duration": 4.47 }, { "text": "but it hasn't executed anything,\nit just reads the entire file.", "start": 4419.23, "duration": 3.837 }, { "text": "And what's it looking for?", "start": 4423.067, "duration": 1.083 }, { "text": "Well one, it's looking for\nanything wrong with the file.", "start": 4424.15, "duration": 2.91 }, { "text": "So say like my very first example\nwhen I had an array and it was missing", "start": 4427.06, "duration": 5.22 }, { "text": "a comma, that's something that's\ncaught in the first reading.", "start": 4432.28, "duration": 2.73 }, { "text": "It says, hey, like, this looks like\nan array but it's not quite right.", "start": 4435.01, "duration": 3.66 }, { "text": "Like, I see this thing that I'm not\nexpecting, I'm respecting a comma here.", "start": 4438.67, "duration": 3.894 }, { "text": "So that's one thing that's caught.", "start": 4442.564, "duration": 1.416 }, { "text": "Other things, maybe it will\nadd some semi-colons for you", "start": 4443.98, "duration": 3.51 }, { "text": "and stuff like that.", "start": 4447.49, "duration": 2.67 }, { "text": "And then any function definitions\njust get saved in memory.", "start": 4450.16, "duration": 4.014 }, { "text": "It says, hey, this is a function,\nlet's put this in memory", "start": 4454.174, "duration": 2.416 }, { "text": "so if somebody wants to use\nit at line 1 they're able to.", "start": 4456.59, "duration": 4.1 }, { "text": "Variable initializations, if\nthey're lexically scope-- or I mean,", "start": 4460.69, "duration": 4.35 }, { "text": "if they're lexically-scoped\nthey will be declared,", "start": 4465.04, "duration": 2.52 }, { "text": "but they will not be initialized.", "start": 4467.56, "duration": 1.56 }, { "text": "Meaning anything\ndeclared with var will be", "start": 4469.12, "duration": 2.76 }, { "text": "declared-- like there's\na variable that exists,", "start": 4471.88, "duration": 2.28 }, { "text": "but it's not going to be set\nequal to anything until later.", "start": 4474.16, "duration": 4.71 }, { "text": "And then there's a second phase called\nthe execution phase, whereby the code", "start": 4478.87, "duration": 3.57 }, { "text": "is actually run, it's executed.", "start": 4482.44, "duration": 1.89 }, { "text": "And so that is when things\nlike const or let get invoked--", "start": 4484.33, "duration": 4.95 }, { "text": "or get both declared and initialized.", "start": 4489.28, "duration": 4.14 }, { "text": "Does that make sense to everyone?", "start": 4493.42, "duration": 2.124 }, { "text": "Anybody have any questions on scope?", "start": 4498.82, "duration": 2.216 }, { "text": "So why might we take\nadvantage of this scoping?", "start": 4505.9, "duration": 3.443 }, { "text": "Does it seem like a feature\nor does it seem like a bug?", "start": 4509.343, "duration": 2.291 }, { "text": "Anybody care to guess?", "start": 4516.96, "duration": 1.314 }, { "text": "There's no correct answer to this.", "start": 4518.274, "duration": 1.416 }, { "text": "It's completely opinion.", "start": 4519.69, "duration": 1.38 }, { "text": "Yeah?", "start": 4521.07, "duration": 0.5 }, { "text": "AUDIENCE: So you've got a controller\nwith math functions in it.", "start": 4521.57, "duration": 2.886 }, { "text": "JORDAN: Uh huh.", "start": 4524.456, "duration": 0.962 }, { "text": "AUDIENCE: You can call the\nones there and lay down", "start": 4525.418, "duration": 2.084 }, { "text": "a file so that you can write all\nyour code in one giant function?", "start": 4527.502, "duration": 4.188 }, { "text": "JORDAN: Yes So, one\nthing might be we can", "start": 4531.69, "duration": 2.52 }, { "text": "have these function\ndeclarations at the very bottom", "start": 4534.21, "duration": 2.454 }, { "text": "and then we can use it at the top.", "start": 4536.664, "duration": 1.416 }, { "text": "And so that might be good\nfor code organization.", "start": 4538.08, "duration": 2.88 }, { "text": "Like somebody reading\nyour code would know, hey,", "start": 4540.96, "duration": 2.16 }, { "text": "if I'm looking for\nthese functions, they're", "start": 4543.12, "duration": 2.01 }, { "text": "all going to be declared\ntogether at the bottom.", "start": 4545.13, "duration": 2.22 }, { "text": "And they might be used everywhere,\nbut all the function declarations", "start": 4547.35, "duration": 3.06 }, { "text": "are all going to be here.", "start": 4550.41, "duration": 1.041 }, { "text": "So that might be a feature because\nit's good for organization.", "start": 4551.451, "duration": 4.209 }, { "text": "So say we're to play devil's advocate.", "start": 4555.66, "duration": 2.91 }, { "text": "Who might see this as a bug?", "start": 4558.57, "duration": 2.346 }, { "text": "Uh huh.", "start": 4574.038, "duration": 3.162 }, { "text": "OK.", "start": 4577.2, "duration": 0.5 }, { "text": "So the question is, like, why\ncan we declare two variables", "start": 4577.7, "duration": 2.872 }, { "text": "with the same name when they look\nlike they're in the same scope,", "start": 4580.572, "duration": 2.708 }, { "text": "specifically with this var keyword?", "start": 4583.28, "duration": 2.82 }, { "text": "Which is another thing where it's a\nbug/feature that a lot of people use.", "start": 4586.1, "duration": 5.66 }, { "text": "And so if JavaScript were to be updated\nand that bug/feature were to disappear,", "start": 4591.76, "duration": 4.99 }, { "text": "a lot of code would break.", "start": 4596.75, "duration": 2.52 }, { "text": "So a lot of people have\ntook advantage of this.", "start": 4599.27, "duration": 1.98 }, { "text": "And basically it's the same thing as\nlike, why is typeof a null object?", "start": 4603.862, "duration": 2.958 }, { "text": "Just because it is.", "start": 4609.77, "duration": 1.08 }, { "text": "And we can't change that because\npeople rely on that behavior.", "start": 4610.85, "duration": 4.39 }, { "text": "I know it's kind of an anti-answer,\nbut does that kind of makes sense?", "start": 4615.24, "duration": 3.86 }, { "text": "Yeah, you should-- so there's not\nreally a good reason to use var anymore.", "start": 4622.34, "duration": 4.99 }, { "text": "With ES6, everything\nsupports const and let.", "start": 4627.33, "duration": 3.97 }, { "text": "And so I've been using them in all\nof the examples except for these.", "start": 4631.3, "duration": 5.065 }, { "text": "And I think you should\ndefinitely use them as well.", "start": 4636.365, "duration": 2.125 }, { "text": "The reason I'm getting var\nis one, so if you see this,", "start": 4638.49, "duration": 3.68 }, { "text": "you know what's kind of going on, and\nbecause a lot of legacy code-- a lot", "start": 4642.17, "duration": 3.54 }, { "text": "of code written two, five\nyears ago, 10 years ago", "start": 4645.71, "duration": 3.18 }, { "text": "uses var just because\nit was the only option.", "start": 4648.89, "duration": 1.92 }, { "text": "One thing that I didn't mention\nearlier is that you can also", "start": 4653.76, "duration": 3.612 }, { "text": "declare a variable like this.", "start": 4657.372, "duration": 1.208 }, { "text": "So this is another way\nof declaring a variable.", "start": 4668.47, "duration": 4.08 }, { "text": "This creates a global variable.", "start": 4672.55, "duration": 2.517 }, { "text": "It's something that you probably\nwon't see that often, there's", "start": 4675.067, "duration": 2.583 }, { "text": "really no reason to do it.", "start": 4677.65, "duration": 1.77 }, { "text": "But in case you ever see\nthat, that's what it is.", "start": 4679.42, "duration": 4.27 }, { "text": "So if you declare a variable without\ngiving it a keyword like let, const,", "start": 4683.69, "duration": 3.555 }, { "text": "or var, it creates it globally.", "start": 4687.245, "duration": 2.18 }, { "text": "But there's no reason for\nyou to use this, really.", "start": 4692.23, "duration": 3.21 }, { "text": "It's just if you ever see\nit, that's what it is.", "start": 4695.44, "duration": 2.398 }, { "text": "Any questions at all about scoping?", "start": 4701.19, "duration": 2.38 }, { "text": "It's a pretty important concept.", "start": 4703.57, "duration": 3.95 }, { "text": "And for online folks as well,\nremember, you can post in Slack", "start": 4707.52, "duration": 3.18 }, { "text": "and the staff will either field\nthose questions or pass them to me.", "start": 4710.7, "duration": 4.846 }, { "text": "Cool So let's move on to\nour final topic of the day.", "start": 4719.21, "duration": 2.72 }, { "text": "So the global object.", "start": 4726.37, "duration": 2.11 }, { "text": "So the way these things all work\nis basically in any given runtime,", "start": 4728.48, "duration": 7.46 }, { "text": "there's this thing\ncalled a global object.", "start": 4735.94, "duration": 2.65 }, { "text": "And so all variables, all\nfunctions are actually", "start": 4738.59, "duration": 2.63 }, { "text": "keys or parameters or\nmethods on the global object.", "start": 4741.22, "duration": 3.9 }, { "text": "So in browser, there's\nthis thing called a window", "start": 4745.12, "duration": 3.18 }, { "text": "which is a global object of a window.", "start": 4748.3, "duration": 1.75 }, { "text": "So this is a browser environment.", "start": 4750.05, "duration": 5.78 }, { "text": "And if I type window, I see this thing.", "start": 4755.83, "duration": 3.92 }, { "text": "And it has a lot in it.", "start": 4759.75, "duration": 4.13 }, { "text": "But basically the window\nis this global object,", "start": 4777.01, "duration": 2.27 }, { "text": "and on it there is all sorts of stuff.", "start": 4779.28, "duration": 3.01 }, { "text": "So we see $0, $0a.", "start": 4782.29, "duration": 3.17 }, { "text": "So these-- so any variable that\nyou declare in this console", "start": 4785.46, "duration": 4.47 }, { "text": "gets put onto this global\nobject called window.", "start": 4789.93, "duration": 3.96 }, { "text": "And so actually, let me open\na brand new browser tab.", "start": 4793.89, "duration": 2.83 }, { "text": "And we'll get a little\nbit of a cleaner window.", "start": 4803.212, "duration": 1.958 }, { "text": "So the reason that we saw these\nthings called $0, $whatever,", "start": 4805.17, "duration": 3.13 }, { "text": "is because I open these dev tools on\nthe same tab as my lecture slides.", "start": 4808.3, "duration": 7.03 }, { "text": "And the way that Google\nSlides works is since it's", "start": 4815.33, "duration": 5.79 }, { "text": "something dynamic on the web,\nit has to be using JavaScript.", "start": 4821.12, "duration": 3.09 }, { "text": "And since it's using\nJavaScript, obviously it's", "start": 4824.21, "duration": 2.01 }, { "text": "creating a bunch of functions,\nit's creating a bunch of variables.", "start": 4826.22, "duration": 2.75 }, { "text": "And all of these things end\nup on the window object.", "start": 4828.97, "duration": 2.32 }, { "text": "And so when we check out--\nwhen we inspected the window", "start": 4831.29, "duration": 2.7 }, { "text": "object of that particular tab,\nthere was a bunch of stuff", "start": 4833.99, "duration": 2.79 }, { "text": "on it because that's how\nGoogle Slides is working.", "start": 4836.78, "duration": 3.72 }, { "text": "And when we created a new\ntab, this is a brand new tab", "start": 4840.5, "duration": 6.66 }, { "text": "and therefore no JavaScript\nhas been executed,", "start": 4847.16, "duration": 2.08 }, { "text": "so there's a lot fewer variables\nand stuff on this new tab.", "start": 4849.24, "duration": 5.7 }, { "text": "And so whenever we\ncreate new variables-- so", "start": 4854.94, "duration": 2.09 }, { "text": "say we do const x equals this is\na new variable, we see x is this.", "start": 4857.03, "duration": 7.98 }, { "text": "We can also do this window.x--", "start": 4865.01, "duration": 3.76 }, { "text": "I declare it as a const.", "start": 4868.77, "duration": 1.5 }, { "text": "So the way that browser windows\nhandle these blocks of variables", "start": 4879.24, "duration": 2.71 }, { "text": "is a little bit different.", "start": 4881.95, "duration": 1.91 }, { "text": "So we'll use var for\nthe sake of explanation.", "start": 4883.86, "duration": 2.92 }, { "text": "So when I create this new\nvariable called var y,", "start": 4886.78, "duration": 3.65 }, { "text": "it is the exact same thing as window.y\nAnd so if we inspect the window,", "start": 4890.43, "duration": 9.03 }, { "text": "we see a bunch of things that are part\nof the JavaScript API, the browser", "start": 4899.46, "duration": 3.38 }, { "text": "API, all of these things.", "start": 4902.84, "duration": 2.23 }, { "text": "And if we go all the\nway to the very bottom,", "start": 4905.07, "duration": 7.03 }, { "text": "so this is stuff that is\ncreated in the new tab.", "start": 4912.1, "duration": 5.258 }, { "text": "L, M, N, O, P, Q, R,\nS. We see this y here.", "start": 4917.358, "duration": 5.162 }, { "text": "So part of that window object\nis that variable called y", "start": 4922.52, "duration": 3.41 }, { "text": "that we stuck on the window.", "start": 4925.93, "duration": 1.55 }, { "text": "And so even though we declared\nit with var y equals something,", "start": 4927.48, "duration": 3.31 }, { "text": "it ended up on that\nglobal object, and that's", "start": 4930.79, "duration": 2.19 }, { "text": "how JavaScript keeps track of\nall of your valuables and stuff.", "start": 4932.98, "duration": 4.98 }, { "text": "And so what happens when we're in the\nnode environment and we type window?", "start": 4937.96, "duration": 7.64 }, { "text": "up window's not defined.", "start": 4945.6, "duration": 1.692 }, { "text": "And it's because in\nthe node environment,", "start": 4947.292, "duration": 1.708 }, { "text": "the global variable is not called\nwindow, it's actually called global.", "start": 4949.0, "duration": 3.13 }, { "text": "And if we type global, we\nnow see all of these things.", "start": 4952.13, "duration": 5.0 }, { "text": "And a lot of this will overlap with\nthat window object in the browser,", "start": 4959.87, "duration": 3.76 }, { "text": "but since the browser API has\nthings in it that is not necessarily", "start": 4963.63, "duration": 4.0 }, { "text": "used in the command line\nstuff like, give me a DOM node", "start": 4967.63, "duration": 3.6 }, { "text": "or give me CSS on this DOM node,\nstuff like that, that doesn't really", "start": 4971.23, "duration": 5.01 }, { "text": "make sense in a command line\ninterface, therefore those things", "start": 4976.24, "duration": 5.43 }, { "text": "are not on this global object.", "start": 4981.67, "duration": 4.26 }, { "text": "And then if we try to type\nglobal, here in the window,", "start": 4985.93, "duration": 6.03 }, { "text": "that doesn't really make sense either.", "start": 4991.96, "duration": 3.42 }, { "text": "There's another thing that's\njust kind of important to know", "start": 4995.38, "duration": 2.67 }, { "text": "but you might not ever\ntake advantage of it.", "start": 4998.05, "duration": 3.589 }, { "text": "But does that-- does that make sense?", "start": 5001.639, "duration": 1.541 }, { "text": "Cool.", "start": 5006.18, "duration": 0.5 }, { "text": "So let's move onto something that we'll\ndiscuss a lot more in the next lecture", "start": 5009.62, "duration": 4.18 }, { "text": "but I'll go ahead and introduce\nthe concept in this lecture", "start": 5013.8, "duration": 2.81 }, { "text": "and leave you with a\nlittle bit of a teaser.", "start": 5016.61, "duration": 2.52 }, { "text": "So who here has heard\nof a closure before?", "start": 5019.13, "duration": 1.86 }, { "text": "So closures are actually\none of the things", "start": 5023.69, "duration": 2.94 }, { "text": "that JavaScript\nprogrammers hate the most", "start": 5026.63, "duration": 3.252 }, { "text": "because it's a very, very\ndifficult concept to learn.", "start": 5029.882, "duration": 2.208 }, { "text": "Mostly because it's always\ntaught somewhat poorly.", "start": 5035.12, "duration": 5.07 }, { "text": "And so next time I'm going to try\nto do a good job of teaching you,", "start": 5040.19, "duration": 2.8 }, { "text": "but I'm going to show you right now\nthe problem that a lot of people face.", "start": 5042.99, "duration": 4.98 }, { "text": "And so what closures are are functions--", "start": 5047.97, "duration": 2.74 }, { "text": "is a behavior whereby functions\nthat refer to variables", "start": 5050.71, "duration": 2.35 }, { "text": "declared by a parent\nfunction still exist,", "start": 5053.06, "duration": 3.22 }, { "text": "and it's possibly because of scoping.", "start": 5056.28, "duration": 2.592 }, { "text": "What the heck does that mean?", "start": 5058.872, "duration": 1.208 }, { "text": "I'll explain it in the next\nlecture, but let's go ahead", "start": 5060.08, "duration": 2.291 }, { "text": "and explore what this actually means.", "start": 5062.371, "duration": 4.537 }, { "text": "All right.", "start": 5076.2, "duration": 2.48 }, { "text": "So let's do this.", "start": 5078.68, "duration": 1.135 }, { "text": "So first thing I'm\ngoing to do is I'm going", "start": 5095.714, "duration": 1.791 }, { "text": "to declare an empty array\ncalled array, and I'm", "start": 5097.505, "duration": 2.889 }, { "text": "going to fill that with a few values.", "start": 5100.394, "duration": 1.541 }, { "text": "That's And I'm using var intentionally.", "start": 5107.6, "duration": 5.354 }, { "text": "And that's push on a\nfunction that does this.", "start": 5117.07, "duration": 3.22 }, { "text": "All right So what the\nheck did I do here?", "start": 5143.85, "duration": 2.38 }, { "text": "So I declared a function\ncalled makeFunctionArray,", "start": 5146.23, "duration": 3.54 }, { "text": "and all it does is it creates an\narray, fill it with functions,", "start": 5149.77, "duration": 3.33 }, { "text": "and then invoke one of them.", "start": 5153.1, "duration": 2.05 }, { "text": "And so if I were to call this--", "start": 5155.15, "duration": 1.291 }, { "text": "if I were to call this, I'd get back\nan array full of functions, right?", "start": 5170.68, "duration": 4.84 }, { "text": "And what do we expect those\nfunctions to do? each of them?", "start": 5175.52, "duration": 3.054 }, { "text": "Print a number, right?", "start": 5182.56, "duration": 0.93 }, { "text": "And so what should\nhappen is I should just--", "start": 5183.49, "duration": 3.122 }, { "text": "I should be able to march through this\narray, invoke each of those functions,", "start": 5186.612, "duration": 3.208 }, { "text": "and get back something\nthat counts, right?", "start": 5189.82, "duration": 2.86 }, { "text": "Basically?", "start": 5192.68, "duration": 1.547 }, { "text": "So let's see what happens.", "start": 5194.227, "duration": 1.083 }, { "text": "So I'm just going to access\nthat first one and invoke it,", "start": 5198.53, "duration": 2.9 }, { "text": "and we expect it to print out 0.", "start": 5201.43, "duration": 2.41 }, { "text": "So that there is one, a JavaScript\nprogrammer's worst enemy,", "start": 5207.57, "duration": 5.98 }, { "text": "because it's somewhat unexpected,\nand as we'll see in coming lectures,", "start": 5213.55, "duration": 3.59 }, { "text": "that is actually as expected.", "start": 5217.14, "duration": 2.75 }, { "text": "So I'll leave you guys on that\ncliffhanger for next time,", "start": 5219.89, "duration": 2.84 }, { "text": "and we'll go ahead and\nofficially end lecture.", "start": 5222.73, "duration": 2.6 }, { "text": "I'll stick around if you any questions.", "start": 5225.33, "duration": 3.27 } ]