Test Driven JavaScript Development- P10
Số trang: 20
Loại file: pdf
Dung lượng: 194.88 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Test Driven JavaScript Development- P10:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P10 8.3 Strict Mode 173 results in only the last one to be reachable inside the function (except through arguments, in which all parameters are always reachable). Listing 8.19 shows the new behavior compared to the current one. Listing 8.19 Using the same identifier for more than one formal parameter test repeated identifiers in parameters: function () { // Syntax error in ES5 strict mode function es3VsEs5(a, a, a) { use strict; return a; } // true in ES3 assertEquals(6, es3VsEs5(2, 3, 6)); } Attempts to access the caller or callee properties of the arguments object will throw a TypeError in strict mode. In ES3 (and non-strict ES5), the arguments object shares a dynamic rela- tionship with formal parameters. Modify a formal parameter, and the value in the corresponding index of the argument object is modified too. Modify a value of the arguments object, and the corresponding parameter changes. In strict mode, this relationship goes away and arguments is immutable, as Listing 8.20 exemplifies. Listing 8.20 Relationship between arguments and formal parameters function switchArgs(a, b) { use strict; var c = b; b = a; a = c; return [].slice.call(arguments); } TestCase(ArgumentsParametersTest, { test should switch arguments: function () { // Passes on ES5 strict mode assertEquals([3, 2], switchArgs(2, 3)); // Passes on ES3 // assertEquals([2, 3], switchArgs(2, 3)); } });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 174 ECMAScript 5th Edition this is no longer coerced to an object in strict mode. In ES3 and non-strict ES5, this will be coerced to an object if it is not one already. For instance, when using call or apply with function objects, passing in null or undefined will no longer cause this inside the called function to be coerced into the global object. Neither will primitive values used as this be coerced to wrapper objects. 8.3.2.3 Objects, Properties, and Variables eval and arguments cannot be used as identifiers in ES5 strict mode. Formal parameters, variables, the exception object in a try-catch statement, and object property identifiers are all affected by this restriction. In ES3 implementations, defining an object literal with repeated property iden- tifiers causes the latest one to overwrite the value of previous properties sharing the identifier. In strict mode, repeating an identifier in an object literal will cause a syntax error. As we already saw, strict mode does not allow implicit globals. Not only will im- plicit globals cause errors, but writing to any property of an object whose writable attribute is false, or writing to a non-existent property of an object whose internal [[Extensible]] property is false will throw TypeError as well. The delete operator will no longer fail silently in strict mode. In ES3 and non-strict ES5, using the delete operator on a property whose configurable attribute is false will not delete the property, and the expression will return false to indicate that the deletion was not successful. In strict mode, such deletion causes a TypeError. 8.3.2.4 Additional Restrictions The with statement no longer exists in strict mode. Using it will simply produce a syntax error. Some developers are less than impressed by this change, but the truth is that it is too easy to use wrong, and easily makes code unpredictable and hard to follow. Octal number literals, such as 0377 (255 decimal), are not allowed in strict mode, this also applies to parseInt(09). 8.4 Various Additions and Improvements We have already seen most of the additions to the Object, but there is more to ECMAScript 5 than empowered objects.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 8.4 Various Additions and Improvements 175 8.4.1 Native JSON ES5 introduces native JSON support, in form of the JSON object. It supports two methods, JSON.stringify and JSON.parse to dump and load JSON respectively. Douglas Crockford’s json2.js provides a compatible interface for browsers that does not yet implement the new JSON interface. This means that by loading this library, we can start using this particular feature today. In fact, json2.js has been widely used for some time, and several browsers already support the native JSON object. Both ES5 and json2.js also adds Date.prototype.toJSON, which seri- alizes date objects as JSON by way of Date.prototype.toISOString, which in turn uses a simplification of the ISO 8601 Extended Format. The format is as ...
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P10 8.3 Strict Mode 173 results in only the last one to be reachable inside the function (except through arguments, in which all parameters are always reachable). Listing 8.19 shows the new behavior compared to the current one. Listing 8.19 Using the same identifier for more than one formal parameter test repeated identifiers in parameters: function () { // Syntax error in ES5 strict mode function es3VsEs5(a, a, a) { use strict; return a; } // true in ES3 assertEquals(6, es3VsEs5(2, 3, 6)); } Attempts to access the caller or callee properties of the arguments object will throw a TypeError in strict mode. In ES3 (and non-strict ES5), the arguments object shares a dynamic rela- tionship with formal parameters. Modify a formal parameter, and the value in the corresponding index of the argument object is modified too. Modify a value of the arguments object, and the corresponding parameter changes. In strict mode, this relationship goes away and arguments is immutable, as Listing 8.20 exemplifies. Listing 8.20 Relationship between arguments and formal parameters function switchArgs(a, b) { use strict; var c = b; b = a; a = c; return [].slice.call(arguments); } TestCase(ArgumentsParametersTest, { test should switch arguments: function () { // Passes on ES5 strict mode assertEquals([3, 2], switchArgs(2, 3)); // Passes on ES3 // assertEquals([2, 3], switchArgs(2, 3)); } });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 174 ECMAScript 5th Edition this is no longer coerced to an object in strict mode. In ES3 and non-strict ES5, this will be coerced to an object if it is not one already. For instance, when using call or apply with function objects, passing in null or undefined will no longer cause this inside the called function to be coerced into the global object. Neither will primitive values used as this be coerced to wrapper objects. 8.3.2.3 Objects, Properties, and Variables eval and arguments cannot be used as identifiers in ES5 strict mode. Formal parameters, variables, the exception object in a try-catch statement, and object property identifiers are all affected by this restriction. In ES3 implementations, defining an object literal with repeated property iden- tifiers causes the latest one to overwrite the value of previous properties sharing the identifier. In strict mode, repeating an identifier in an object literal will cause a syntax error. As we already saw, strict mode does not allow implicit globals. Not only will im- plicit globals cause errors, but writing to any property of an object whose writable attribute is false, or writing to a non-existent property of an object whose internal [[Extensible]] property is false will throw TypeError as well. The delete operator will no longer fail silently in strict mode. In ES3 and non-strict ES5, using the delete operator on a property whose configurable attribute is false will not delete the property, and the expression will return false to indicate that the deletion was not successful. In strict mode, such deletion causes a TypeError. 8.3.2.4 Additional Restrictions The with statement no longer exists in strict mode. Using it will simply produce a syntax error. Some developers are less than impressed by this change, but the truth is that it is too easy to use wrong, and easily makes code unpredictable and hard to follow. Octal number literals, such as 0377 (255 decimal), are not allowed in strict mode, this also applies to parseInt(09). 8.4 Various Additions and Improvements We have already seen most of the additions to the Object, but there is more to ECMAScript 5 than empowered objects.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 8.4 Various Additions and Improvements 175 8.4.1 Native JSON ES5 introduces native JSON support, in form of the JSON object. It supports two methods, JSON.stringify and JSON.parse to dump and load JSON respectively. Douglas Crockford’s json2.js provides a compatible interface for browsers that does not yet implement the new JSON interface. This means that by loading this library, we can start using this particular feature today. In fact, json2.js has been widely used for some time, and several browsers already support the native JSON object. Both ES5 and json2.js also adds Date.prototype.toJSON, which seri- alizes date objects as JSON by way of Date.prototype.toISOString, which in turn uses a simplification of the ISO 8601 Extended Format. The format is as ...
Tìm kiếm theo từ khóa liên quan:
thiết kế web giáo trình css kĩ thuật cắt html flash cơ bản giáo trình photoshopTài liệu liên quan:
-
Báo cáo thực tập: Đề tài thiết kế Web
77 trang 569 2 0 -
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 269 2 0 -
182 trang 177 0 0
-
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 113 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 103 0 0 -
Giáo trình Nhập môn thiết kế website
58 trang 83 0 0 -
Tài liệu giảng dạy Thiết kế giao diện Web - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
88 trang 72 0 0 -
81 trang 69 0 0
-
112 trang 64 0 0
-
Hướng dân sử dụng Navicat để Create , Backup , Restore Database
7 trang 63 0 0