Ameba Ownd

アプリで簡単、無料ホームページ作成

Calvin Harrington's Ownd

Eloquent javascript 3rd edition pdf download

2021.12.18 17:58






















You may think of objects as octopuses with any number of tentacles, each of which has a name tattooed on it. The delete operator cuts off a tentacle from such an octopus. It is a unary operator that, when applied to an object property, will remove the named property from the object.


This is not a common thing to do, but it is possible. To find out what properties an object has, you can use the Object. If you evaluate typeof [], it produces "object". You can see them as long, flat octopuses with all their tentacles in a neat row, labeled with numbers. We will represent the journal that Jacques keeps as an array of objects. We saw that object values can be modified. The types of values discussed in earlier chapters, such as numbers, strings, and Booleans, are all immutable—it is impossible to change values of those types.


You can combine them and derive new values from them, but when you take a specific string value, that value will always remain the same. The text inside it cannot be changed. If you have a string that contains "cat", it is not possible for other code to change a character in your string to make it spell "rat". Objects work differently. You can change their properties, causing a single object value to have different content at different times.


When we have two numbers, and , we can consider them precisely the same number, whether or not they refer to the same physical bits. With objects, there is a difference between having two references to the same object and having two different objects that contain the same properties. They are said to have the same identity. The binding object3 points to a different object, which initially contains the same properties as object1 but lives a separate life.


Bindings can also be changeable or constant, but this is separate from the way their values behave. Similarly, though a const binding to an object can itself not be changed and will continue to point at the same object, the contents of that object might change.


Comparing different objects will return false, even if they have identical prop- erties. The lycanthrope's log So, Jacques starts up his JavaScript interpreter and sets up the environment he needs to keep his journal. Instead of declaring properties like events: events, it just gives a property name. So then, every evening at 10 p. Correlation is a measure of dependence between statistical variables. A sta- tistical variable is not quite the same as a programming variable. In statistics you typically have a set of measurements, and each variable is measured for every measurement.


Correlation between variables is usually expressed as a value that ranges from -1 to 1. Zero correlation means the variables are not related. A correlation of one indicates that the two are perfectly related—if you know one, you also know the other. Negative one also means that the variables are perfectly related but that they are opposites—when one is true, the other is false. This is a formula whose input is a frequency table containing the number of times the different combinations of the variables were observed.


The output of the formula is a number between -1 and 1 that describes the correlation. And even with this one, all we do is turn it into JavaScript.


The notation n01 indicates the number of measurements where the first vari- able squirrelness is false 0 and the second variable pizza is true 1. In the pizza table, n01 is 9. Eating pizza does not appear to have influence on the transformations. Computing correlation We can represent a two-by-two table in JavaScript with a four-element array [76, 9, 4, 1].


We could also use other representations, such as an array con- taining two two-element arrays [[76, 9], [4, 1]] or an object with property names like "11" and "01", but the flat array is simple and makes the expres- sions that access the table pleasantly short. This happened four times. And since binary 10 is 2 in decimal notation, we will store this number at index 2 of the array. Jacques kept his journal for three months. To extract a two-by-two table for a specific event from the journal, we must loop over all the entries and tally how many times the event occurs in relation to squirrel transformations.


The function uses that to determine whether the event name it is interested in is part of the event list for a given day. The loop then adds one to the correct box in the table. We now have the tools we need to compute individual correlations. The only step remaining is to find a correlation for every type of event that was recorded and see whether anything stands out. There is a simpler way to write such loops in modern JavaScript.


This works not only for arrays but also for strings and some other data structures. The final analysis We need to compute a correlation for every type of event that occurs in the data set. To do that, we first need to find every type of event.


Using that, we can see all the correlations. Most correlations seem to lie close to zero. Eating carrots, bread, or pudding apparently does not trigger squirrel-lycanthropy. It does seem to occur some- what more often on weekends. Eating peanuts has a strong positive effect on the chance of turning into a squirrel, whereas brushing his teeth has a significant negative effect.


The phenomenon occurs precisely when Jacques eats peanuts and fails to brush his teeth. For a few years, things go great for Jacques. But at some point he loses his job. Because he lives in a nasty country where having no job means having no medical services, he is forced to take employment with a circus where he performs as The Incredible Squirrelman, stuffing his mouth with peanut butter before every show. One day, fed up with this pitiful existence, Jacques fails to change back into his human form, hops through a crack in the circus tent, and vanishes into the forest.


He is never seen again. Further arrayology Before finishing the chapter, I want to introduce you to a few more object- related concepts. We saw push and pop, which add and remove elements at the end of an array, earlier in this chapter.


The corresponding methods for adding and removing things at the start of an array are called unshift and shift. The rememberUrgently function also adds a task but adds it to the front instead of the back of the queue. To search for a specific value, arrays provide an indexOf method. Another fundamental array method is slice, which takes start and end in- dices and returns an array that has only the elements between them.


The start index is inclusive, the end index exclusive. You can also omit the start index to copy the entire array. It takes an array and an index, and it returns a new array that is a copy of the original array with the element at the given index removed. Strings and their properties We can read properties like length and toUpperCase from string values. As mentioned earlier, such values are immutable and cannot be changed.


But these types do have built-in properties. Every string value has a number of methods. Some very useful ones are slice and indexOf, which resemble the array methods of the same name.


It is called padStart and takes the desired length and padding character as arguments. For example, Math. When, as in max, it is the only parameter, it will hold all arguments.


You can use a similar three-dot notation to call a function with an array of arguments. It is possible to include an array like that along with other arguments, as in max 9, Square bracket array notation similarly allows the triple-dot operator to spread another array into the new array.


The Math object is used as a container to group a bunch of related function- ality. There is only one Math object, and it is almost never useful as a value. Rather, it provides a namespace so that all these functions and values do not have to be global bindings.


The more names have been taken, the more likely you are to accidentally overwrite the value of some existing binding. Many languages will stop you, or at least warn you, when you are defining a binding with a name that is already taken.


JavaScript does this for bindings you declared with let or const but—perversely—not for standard bindings nor for bindings declared with var or function. Back to the Math object. If you need to do trigonometry, Math can help. It contains cos cosine , sin sine , and tan tangent , as well as their inverse functions, acos, asin, and atan, respectively. There is an old programming tradition of writing the names of constant values in all caps.


The previous example used Math. This is a function that returns a new pseudorandom number between zero inclusive and one exclusive every time you call it. To do that, the machine keeps some hidden value, and whenever you ask for a new random number, it performs complicated com- putations on this hidden value to create a new value.


It stores a new value and returns some number derived from it. That way, it can produce ever new, hard-to-predict numbers in a way that seems random. If we want a whole random number instead of a fractional one, we can use Math. Since Math. There are also the functions Math. Fortunately, there is a succinct way to do this in JavaScript.


A similar trick works for objects, using braces instead of square brackets. So an array with another array inside of it consists of at least one memory region for the inner array, and another for the outer array, containing among other things a binary number that represents the position of the inner array.


If you want to save data in a file for later or send it to another computer over the network, you have to somehow convert these tangles of memory addresses to a description that can be stored or sent. What we can do is serialize the data. That means it is converted into a flat description. It is widely used as a data storage and communication format on the Web, even in languages other than JavaScript. All property names have to be surrounded by double quotes, and only simple data expressions are allowed—no function calls, bindings, or anything that involves actual computation.


Comments are not allowed in JSON. The second takes such a string and converts it to the value it encodes. Conceptually, this allows us to put a bunch of related things in a bag and run around with the bag, instead of wrapping our arms around all of the individual things and trying to hold on to them separately.


Most values in JavaScript have properties, the exceptions being null and undefined. Properties are accessed using value. Ob- jects tend to use names for their properties and store more or less a fixed set of them.


Arrays, on the other hand, usually contain varying amounts of con- ceptually identical values and use numbers starting from 0 as the names of their properties. There are some named properties in arrays, such as length and a number of methods.


You can iterate over arrays using a special kind of for loop—for let element of array. Exercises The sum of a range The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers: console. Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the example program and see whether it does indeed return If no step is given, the elements go up by increments of one, corresponding to the old behavior.


The function call range 1, 10, 2 should return [1, 3, 5, 7, 9]. Make sure it also works with negative step values so that range 5, 2, -1 produces [5, 4, 3, 2]. Reversing an array Arrays have a reverse method that changes the array by inverting the order in which its elements appear.


For this exercise, write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument by reversing its elements.


Neither may use the standard reverse method. Thinking back to the notes about side effects and pure functions in the previous chapter, which variant do you expect to be useful in more situations? Which one runs faster? A common data structure is the list not to be confused with array.


A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on. The original list is also still a valid three-element list. Write a function arrayToList that builds up a list structure like the one shown when given [1, 2, 3] as argument.


Also write a listToArray function that produces an array from a list. Then add a helper function prepend, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth, which takes a list and a number and returns the element at the given position in the list with zero referring to the first element or undefined when there is no such element.


If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: because of a historical accident, typeof null also produces "object". The Object. Size almost always involves complexity, and complexity confuses programmers.


Confused programmers, in turn, introduce mistakes bugs into programs. A large program then provides a lot of space for these bugs to hide, making them hard to find. The first is self-contained and six lines long. If we count the size of the definitions of sum and range, the second program is also big—even bigger than the first.


It is more likely to be correct because the solution is expressed in a vocabulary that corresponds to the problem being solved. It is about ranges and sums. The definitions of this vocabulary the functions sum and range will still involve loops, counters, and other incidental details.


But because they are expressing simpler concepts than the program as a whole, they are easier to get right. Abstractions hide details and give us the ability to talk about problems at a higher or more abstract level. As an analogy, compare these two recipes for pea soup. The first one goes like this: Put 1 cup of dried peas per person into a container. Add water until the peas are well covered. Leave the peas in water for at least 12 hours. Take the peas out of the water and put them in a cooking pan.


Add 4 cups of water per person. Cover the pan and keep the peas simmering for two hours. Take half an onion per person. Cut it into pieces with a knife. Add it to the peas. Take a stalk of celery per person. Take a carrot per person.


Cut it into pieces. With a knife! Cook for 10 more minutes. And this is the second recipe: Per person: 1 cup dried split peas, half a chopped onion, a stalk of celery, and a carrot. Soak peas for 12 hours. Simmer for 2 hours in 4 cups of water per person. Chop and add vegetables. The second is shorter and easier to interpret.


But you do need to understand a few more cooking-related words such as soak, simmer, chop, and, I guess, vegetable. Thus, we might fall into the pattern of the first recipe— work out the precise steps the computer has to perform, one by one, blind to the higher-level concepts that they express. It is a useful skill, in programming, to notice when you are working at too low a level of abstraction.


But sometimes they fall short. It is common for a program to do something a given number of times. Often, it is easier to create a function value on the spot instead. However, the body is now written as a function value, which is wrapped in the parentheses of the call to repeat.


This is why it has to be closed with the closing brace and closing parenthesis. Higher-order functions Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions. Since we have already seen that functions are regular values, there is nothing particularly remarkable about the fact that such functions exist. The term comes from mathemat- ics, where the distinction between functions and other values is taken more seriously.


Higher-order functions allow us to abstract over actions, not just values. They come in several forms. For example, we can have functions that create new functions.


This chapter will use a data set about scripts—writing systems such as Latin, Cyrillic, or Arabic. Remember Unicode from Chapter 1, the system that assigns a number to each character in written language? Most of these characters are associated with a specific script. The standard contains different scripts—81 are still in use today, and 59 are historic.


The binding contains an array of objects, each of which describes a script. The direction may be "ltr" for left to right, "rtl" for right to left the way Arabic and Hebrew text are written , or "ttb" for top to bottom as with Mongolian writing. The ranges property contains an array of Unicode character ranges, each of which is a two-element array containing a lower bound and an upper bound.


Any character codes within these ranges are assigned to the script. Filtering arrays To find the scripts in the data set that are still in use, the following function might be helpful. Note how the filter function, rather than deleting elements from the ex- isting array, builds up a new array with only the elements that pass the test.


This function is pure. It does not modify the array it is given. Like forEach, filter is a standard array method. But we want an array of names, which is easier to inspect. The map method transforms an array by applying a function to all of its elements and building a new array from the returned values.


The new array will have the same length as the input array, but its content will have been mapped to a new form by the function. Summarizing with reduce Another common thing to do with arrays is to compute a single value from them. Our recurring example, summing a collection of numbers, is an instance of this. Another example is finding the script with the most characters.


The higher-order operation that represents this pattern is called reduce some- times also called fold. It builds a value by repeatedly taking a single element from the array and combining it with the current value.


The parameters to reduce are, apart from the array, a combining function and a start value. If your array contains at least one element, you are allowed to leave off the start argument. The method will take the first element of the array as its start value and start reducing at the second element.


Note the use of destructuring in the parameter list of the reducer function. The second call to reduce then uses this to find the largest script by repeatedly comparing two scripts and returning the larger one. Han is a script sometimes used for Chinese, Japanese, and Korean text.


Those languages share a lot of characters, though they tend to write them differently. The U. This is called Han unification and still makes some people very angry. Composability Consider how we would have written the previous example finding the biggest script without higher-order functions. The code is not that much worse. But it is still very readable. Higher-order functions start to shine when you need to compose operations. You can see it as a pipeline: we start with all scripts, filter out the living or dead ones, take the years from those, average them, and round the result.


You could definitely also write this computation as one big loop. In terms of what the computer is actually doing, these two approaches are also quite different. The first will build up new arrays when running filter and map, whereas the second computes only some numbers, doing less work. Strings and character codes One use of the data set would be figuring out what script a piece of text is using. Remember that each script has an array of character code ranges associated with it. It takes a test function and tells you whether that function returns true for any of the elements in the array.


But how do we get the character codes in a string? In Chapter 1 I mentioned that JavaScript strings are encoded as a sequence of bit numbers.


These are called code units. A Unicode character code was initially supposed to fit within such a unit which gives you a little over 65, characters. It describes most common characters using a single bit code unit but uses a pair of two such units for others. UTF is generally considered a bad idea today. It seems almost inten- tionally designed to invite mistakes.


But as soon as some- one tries to use such a program with some less common Chinese characters, it breaks. Fortunately, with the advent of emoji, everybody has started us- ing two-unit characters, and the burden of dealing with such problems is more fairly distributed.


Unfortunately, obvious operations on JavaScript strings, such as getting their length through the length property and accessing their content using square brackets, deal only with code units.


The codePointAt method, added later, does give a full Unicode character. So we could use that to get characters from a string. But the argument passed to codePointAt is still an index into the sequence of code units.


Like codePointAt, this type of loop was introduced at a time where people were acutely aware of the problems with UTF When you use it to loop over a string, it gives you real characters, not code units. Recognizing text We have a characterScript function and a way to correctly loop over charac- ters.


The next step is to count the characters that belong to each script. It returns an array of objects, each of which names a group and tells you the number of elements that were found in that group. It uses another array method—findIndex. This method is somewhat like indexOf, but instead of looking for a specific value, it finds the first value for which the given function returns true. Like indexOf, it returns -1 when no such element is found. Using countBy, we can write the function that tells us which scripts are used in a piece of text.


To be able to compute percentages, we first need the total number of char- acters that belong to a script, which we can compute with reduce. If no such characters are found, the function returns a specific string.


Otherwise, it trans- forms the counting entries into readable strings with map and then combines them with join. With examples, illustrations, and insightful explanations, readers will benefit from the collective wisdom of seasoned experts John Resig, Bear Bibeault, and Josip Maras.


Grokking Algorithms Author : Aditya Y. You'll start with sorting and searching and, as you build up your skills in thinking algorithmically, you'll tackle more complex concerns such as data compression and artificial intelligence.


Each carefully presented example includes helpful diagrams and fully annotated code samples in Python. Learning about algorithms doesn't have to be boring!


Get a sneak peek at the fun, illustrated, and friendly examples you'll find in Grokking Algorithms on Manning Publications' YouTube channel.


About the Technology An algorithm is nothing more than a step-by-step procedure for solving a problem. The algorithms you'll use most often as a programmer have already been discovered, tested, and proven. If you want to understand them but refuse to slog through dense multipage proofs, this is the book for you.


This fully illustrated and engaging guide makes it easy to learn how to use the most important algorithms effectively in your own programs. About the Book Grokking Algorithms is a friendly take on this core computer science topic. In it, you'll learn how to apply common algorithms to the practical programming problems you face every day. You'll start with tasks like sorting and searching.


As you build up your skills, you'll tackle more complex problems like data compression and artificial intelligence. By the end of this book, you will have mastered widely applicable algorithms as well as how and when to use them. What's Inside Covers search, sort, and graph algorithms Over pictures with detailed walkthroughs Performance trade-offs between algorithms Python-based code samples About the Reader.


Popular Books. Fear No Evil by James Patterson. From This Moment by Melody Grace. JavaScript for impatient programmers. This book makes JavaScript less challenging to learn for newcomers, by offering a modern view that is as consistent as possible.


Highlights: - Get started quickly, by initially focusing on modern features. The JavaScript Beginner's Handbook. The author find this approach gives a well-rounded overview. This book does not try to cover everything under the sun related to JavaScript. It focuses on the core of the language, trying to simplify the more complex topics. Working knowledge of ES is required to grasp the concepts covered in the book easily. You'll cover special utility classes, data structures, and algorithms, and learn how to manipulate file systems and build high-performance programs that communicate over networks.


Bug Bounty Bootcamp teaches you how to hack web applications. You will learn how to perform reconnaissance on a target, how to identify vulnerabilities, and how to exploit them.


Bug bounty programs are company-sponsored programs that invite researchers to search for vulnerabilities on their applications and reward them for their findings. This book is designed to help beginners with little to no security experience learn web hacking, find bugs, and stay competitive in this booming and lucrative industry. Finally, the book touches on advanced techniques rarely covered in introductory hacking books but that are crucial to understand to hack web applications.


A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers. More than ever, the web is a universal platform for all types of applications, and JavaScript is the language of the web. For anyone serious about web development, it's not enough to be a decent JavaScript coder.


They need to be ninja-stealthy, efficient, and ready for anything. Secrets of the JavaScript Ninja, Second Edition dives below the surface and helps readers understand the deceptively-complex world of JavaScript and browser-based application development. It skips the basics, and dives into core JavaScript concepts such as functions, closures, objects, prototypes, promises, and so on.


With examples, illustrations, and insightful explanations, readers will benefit from the collective wisdom of seasoned experts John Resig, Bear Bibeault, and Josip Maras. Summary Grokking Algorithms is a fully illustrated, friendly guide that teaches you how to apply common algorithms to the practical problems you face every day as a programmer.


You'll start with sorting and searching and, as you build up your skills in thinking algorithmically, you'll tackle more complex concerns such as data compression and artificial intelligence. Each carefully presented example includes helpful diagrams and fully annotated code samples in Python.


Learning about algorithms doesn't have to be boring! Get a sneak peek at the fun, illustrated, and friendly examples you'll find in Grokking Algorithms on Manning Publications' YouTube channel. About the Technology An algorithm is nothing more than a step-by-step procedure for solving a problem.


The algorithms you'll use most often as a programmer have already been discovered, tested, and proven.