`
leonzhx
  • 浏览: 793386 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 3. Data Structures: Objects and Arrays

阅读更多

 

1. Every string has a property called length, which refers to an integer, the amount of characters in that string.

 

2. Properties can be accessed in two ways, either with brackets or using dot notation. The second way is a shorthand for the first, and it works only when the name of the property is a valid variable name—when it doesn’t have any spaces or symbols in it and does not start with a digit character.

 

3. Trying to read a property from the values null and undefined will cause an error.

 

4. In most value types, if they have properties at all, they are fixed, and you are not allowed to change them. However, there is one type of value, objects, where properties can be freely added, removed, and changed. The main role of objects, in fact, is to be a collection of properties.

 

5. Trying to read a nonexistent property gives the value undefined. The keyword delete is used to cut off properties.

 

6. If a property that does not yet exist is set with the = operator, it is added to the object.

 

7. Properties whose names are not valid variable names cannot be accessed with the dot notation, but only using brackets. When creating an object, these have to be quoted, unless they are numbers.

 

8. The part between the brackets can be any expression. It is converted to a string to determine the property name it refers to.

 

9. The operator in can be used to test whether an object has a certain property. It produces a Boolean.

 

10. Removing a name from the set is done by deleting the property. 

 

11. Object values can apparently change. The other types of values are all immutable—it is impossible to change an existing value of those types. You can combine them and derive new values from them, but when you take a specific string value, the text inside it cannot change. With objects, on the other hand, the content of a value can be modified, by changing its properties.

 

12. JavaScript’s === operator, when comparing objects, will return true only if both values given to it are the precise same value.

 

13. There is a special kind of objects called arrays, and they provide some conveniences, such as a length property that tells us how many values the array holds. New arrays can be created using brackets ([ and ]).

 

14. The length property of the array will automatically get updated when elements are added or removed—it always holds the highest index that contains an element, plus one.

 

15. Every string has a toUpperCase property. When called, it will return a copy of the string, in which all letters have been converted to uppercase. There is also toLowerCase.

 

16. The method push, which is associated with arrays, can be used to insert values at the end of the array. Then there is pop, the inverse of push: It takes out and returns the last value in the array. join builds a single big string from an array of strings. The parameter it is given is pasted between the values in the array.

 

17. Strings conveniently have a method named split, which is (almost) the opposite of the join method of arrays. It splits a string into an array, using the string given as its argument to determine in which places to cut. 

 

18. The method charAt can be used to get a specific character from a string.

 

19. Strings also have a method called slice. This copies out a piece of the string, starting from the character at the position given by the first argument and ending before (not including) the character at the position given by the second one. 

 

20. charAt will return "" when there is no character at the given position, and slice will simply leave out the part of the new string that does not exist.

 

21. Strings have an indexOf method that can be used to find the (first) position of a character or substring within that string. Also, when slice is given only one argument, it will return the part of the string from the given position all the way to the end.

 

22. The in keyword has a somewhat different meaning when it is used together with for:

    

for (var cat in livingCats) print(cat);

 A loop like that will go over the names of the properties in an object, which allows us to enumerate all the names in our set.

 

 

23. new is a way to create object values. Instead of specifying all the property names and values, a function is used to build up the object. This makes it possible to define a kind of standard procedure for creating objects. Functions like this are called constructors.

 

24. The Date constructor can be used in different ways:

// Produces a date object for the current time.
new Date();
// February (!) 1st, 1980
new Date(1980, 1, 1);
// March 30th, 2007, 30 seconds past 8:20
new Date(2007, 2, 30, 8, 20, 30);

 Inside the object, a date is represented by the amount of milliseconds it is away from January 1, 1970. You can get that amount by getTime method.

 

25. Date objects can be inspected with a number of get methods:

var today = new Date();
print("Year: ", today.getFullYear(), ", month: ", today.getMonth(), ", day: ", today.getDate());
print("Hour: ", today.getHours(), ", minutes: ", today.getMinutes(), ", seconds: ", today.getSeconds());
print("Day of week: ", today.getDay());

 All of these, except for getDay, also have a set... variant that can be used to change the value of the date object.

 

26. Comparing dates with <, >, <=, and >= does exactly what you would expect. == will return false when comparing two different objects, even if they contain the same properties. Testing whether two dates are equal can be done by comparing the value returned by getTime method.

 

27. The getTimezoneOffset function of a Date can be used to find out how many minutes it differs from Greenwich mean time (GMT).

 

28. Whenever a function is called, a special “magic” variable named arguments is added to the environment in which the function body runs. This variable refers to an object that resembles an array. It has a property 0 for the first argument, 1 for the second, and so on, for every argument the function was given. It also has a length property. However, the arguments object is not a real array—it does not have methods like push, and it does not automatically update its length property when you add something to it.

 

29. One can find a whole outfit of mathematical functions and constants inside Math. All the trigonometric functions are there — Math.cos, sin, tan, acos, asin, atan. π and e are there, written in all capital letters (Math.PI and Math.E). Math.pow also accepts negative and fractional exponents. Math.sqrt takes square roots. Math.round, Math.floor, and Math.ceil will round numbers to the closest whole number, the whole number below it, and the one above it, respectively.

 

30. It seems some properties of objects are hidden from in loops, or, as this is officially called, not enumerable. (such as length, push, join of array) All properties your programs add to objects are visible. There is no way to make them hidden.

 

31. When a line gets ridiculously long, it can be spread over multiple lines. The result can be made easier to read by indenting the second line to show that it belongs together with the one above it.

 

分享到:
评论

相关推荐

    Data.Structures.and.Other.Objects.Using.C++.4th.Edition

    Data Structures and Other Objects Using C++ takes a gentle approach to the data structures course in C++. Providing an early, self-contained review of object-oriented programming and C++, this text ...

    Prentice.Hall.C++.for.Business.Programming.2nd.Edition.2005.chm

    Section A.3. Data RepresentationIntegers 797 Section A.4. Data RepresentationReal Numbers 799 Exercises Appendix A 800 Appendix B. Program Control 802 Section B.1. Sequence 802 Section B.2. ...

    Guide to Data Structures_A Concise Introduction Using Java-Springer(2017).pdf

    • Chapter 3 illustrates queues using arrays. • Chapter 4 discusses lists using arrays. • Chapter 5 introduces lists using references and objects. • Chapter 6 examines linked lists. • Chapter 7 ...

    The C programming Language(chm格式完整版)

    Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4: Functions and Program Structure ...

    Programming in C++ for Engineering and Science

    Chapter 3: Types in C++ Chapter 4: Getting Started with Expressions Chapter 5: Control Structures Chapter 6: Functions and Libraries Chapter 7: Using Classes Chapter 8: More Selection Control ...

    Algorithms in C++, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching

    2. **Abstract Objects and Collections of Objects**: This section delves into the concept of abstract objects and collections of objects, providing a framework for organizing and manipulating data. ...

    Everyday Data Structures

    Add/remove objects and nest arrays and dictionaries within another dictionary and understand why such architecture is often preferred or necessary Get acquainted with the tree structures such as heap,...

    Javascript.Object.Oriented.Programming.pdf

    Chapter 3: Data Structures And Manipulation Chapter 4: Object-Oriented Javascript Chapter 5: Javascript Patterns Chapter 6: Testing And Debugging Chapter 7: Ecmascript 6 Chapter 8: Dom Manipulation ...

    Beginning PHP 5.3

    - **Multidimensional Arrays:** Discussion on creating and working with multidimensional arrays. - **Array Iteration:** Explanation of different ways to iterate over arrays, including foreach loops and...

    Problem Solving with C++ (7th edition)

    #### Chapter 3: Control Structures This chapter introduces control structures that allow for more complex logic in programs: - **switch Statement Example**: An explanation of the switch statement, a...

    php_6_fast_and_easy_web_development.pdf

    Creating a Table and Storing Data with SQLite. . . . . . . . . . . . 525 Retrieving Items with SQLite. . . . . . . . . . . . . . . . . . . . . . . . . . 526 Performing Other Tasks with SQLite. . . . ....

    The way to go

    Chapter 4—Basic constructs and elementary data types.......................................................49 4.1. Filenames—Keywords—Identifiers......................................................

Global site tag (gtag.js) - Google Analytics