Just for self review.
The (*) section means it’s come from Internet (stackOverFlow, MDN or Medium).
trim the unwanted whitespace from around a string.
Regular expressions are search patterns that can be used to find text that matches a given pattern.
Though the regular expressions seem complex, they’re really nothing more than a way to describe a pattern.
Convert a number to a hexadecimal string with:
and reverse the process with:
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1)
range [0, 254] === [0, 255)
parseInt() & parseFloat()
An array is an ordered collection of elements.
To create an array of several undefined elements, you can provide an array length when creating an array:
Use the Array methods indexOf and splice to find and remove/replace array elements:
If the index is negative, the elements will be spliced from the end, not from the beginning of the array
When we use an Array object to create an associative array, what we’re really doing is adding new properties to the array object, rather than adding new array elements.
what you’re really doing is adding a new object property:
However, using this syntax is not as efficient as using the function statement, because using a function constructor requires that the function be parsed each time it’s called.
Functions defined with the function statement are parsed once, when the code is loaded.
Variable names can be any combination of characters, numbers, and underscores, as long as the variable name starts with a character or underscore and case-sensitivity is preserved.
You need to create a function, but you won’t know its structure until runtime.
Use an anonymous function, created using the Function object constructor:
Anonymous functions are parsed at runtime, which makes them inefficient for general purposes.
However, they allow us to define both parameters and function body at runtime, which is handy if you’re not sure what the function body is going to be until runtime.
When you create a function, you create a new scope that exists as long as the function exists. The function has access to all variables in its scope, as well as all of the variables from the outer scope, but the outer scope does not have access to the variables in the function.
Because of these scoping rules, we can access window and document objects in all of our browser applications, and the inner function in the solution can also access the data passed to, or originating in, the outer function that wraps it.
However, the outer function cannot access the inner function’s arguments or local data, because they exist in a different scope.
So even though the outer function’s application scope no longer exists, the inner function’s scope exists at the time the function was returned, including a snapshot of the outer function’s data. It will continue to exist until the application is finished, and the outer, global scope is released.
The problem with this circular reference is exacerbated in earlier versions of IE, because IE does not release memory associated with DOM objects (such as the doc element) if application scope is released. Even leaving the page does not reclaim the memory: you have to close the browser.
Luckily, newer versions of IE don’t have this problem. However, function closures should be deliberate, rather than accidental.
instance from cookbook:
When you want to curry a function:
Adding currying to your coding practice will encourage the use of partially applied functions throughout your code, avoiding a lot of potential repetition, and may help get you into better habits about naming and dealing with function arguments.
calculate time consume
You can also remove event listeners, as well as cancel the events themselves.
You can also prevent the event from propagating if the element receiving the event is nested in another element.
Canceling an event is helpful when dealing with form validation, and preventing event propagation is helpful when processing click events.
It’s up to you to decide which level of event handling meets your needs.
Canceling an Event Based on Changed Circumstance
You need to cancel an event, such as a form submission, before the event propagates to other elements.
Preventing an Event from Propagating Through a Set of Nested Elements
We don’t want to indiscriminately throw up pop ups, or intervene with links or people going about their business, but there are times when we want to ensure that people know that when they click a link, type in a new domain, or even close the browser, they’re leaving the current site.
You want people to be aware of the fact that they’re leaving the previously secure website and going somewhere that may be less than secure.
DOM Level 0 form collections:
you can also use an array index:
Using an array index is tricky, since it may be difficult to determine the location of a specific form and element.
However, it’s also a simple way to process all form elements in a loop:
In the form event handler function, you can access both the event and the element to get information about both. How you do this depends on your browser, and also how you assign the events.