The variable in question already exists as a global

This preview shows page 221 - 225 out of 676 pages.

the variable in question already exists as a global variable, but only when assigning to it would have created it. Another change in strict mode is that the this binding holds the value undefined in functions that are not called as methods. When making such a call outside of strict mode, this refers to the global scope object. So if you accidentally call a method or constructor incorrectly in strict mode, JavaScript will produce an error as soon as it tries to read something from this , rather than happily working with the global object, creating and reading global variables. 206
Image of page 221

Subscribe to view the full document.

For example, consider the following code, which calls a constructor without the new keyword so that its this will not refer to a newly con- structed object: function Person(name) { this.name = name; } var ferdinand = Person (" Ferdinand "); // oops console.log(name); // Ferdinand So the bogus call to Person succeeded but returned an undefined value and created the global variable name . In strict mode, the result is differ- ent. "use strict "; function Person(name) { this.name = name; } // Oops , forgot new var ferdinand = Person (" Ferdinand "); // TypeError: Cannot set property name of undefined We are immediately told that something is wrong. This is helpful. Strict mode does a few more things. It disallows giving a function multiple parameters with the same name and removes certain problem- atic language features entirely (such as the with statement, which is so misguided it is not further discussed in this book). In short, putting a "use strict" at the top of your program rarely hurts and might help you spot a problem. 207
Image of page 222
Testing If the language is not going to do much to help us find mistakes, we’ll have to find them the hard way: by running the program and seeing whether it does the right thing. Doing this by hand, again and again, is a sure way to drive yourself insane. Fortunately, it is often possible to write a second program that automates testing your actual program. As an example, we once again use the Vector type. function Vector(x, y) { this.x = x; this.y = y; } Vector.prototype.plus = function(other) { return new Vector(this.x + other.x, this.y + other.y); }; We will write a program to check that our implementation of Vector works as intended. Then, every time we change the implementation, we follow up by running the test program so that we can be reasonably confident that we didn’t break anything. When we add extra function- ality (for example, a new method) to the Vector type, we also add tests for the new feature. function testVector () { var p1 = new Vector (10, 20); var p2 = new Vector(-10, 5); var p3 = p1.plus(p2); 208
Image of page 223

Subscribe to view the full document.

if (p1.x !== 10) return "fail: x property "; if (p1.y !== 20) return "fail: y property "; if (p2.x !== -10) return "fail: negative x property "; if (p3.x !== 0) return "fail: x from plus"; if (p3.y !== 25) return "fail: y from plus"; return "everything ok"; } console.log(testVector ()); // everything ok Writing tests like this tends to produce rather repetitive, awkward code.
Image of page 224
Image of page 225

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern