Just for self review.
The (*) section means it’s come from Internet (stackOverFlow, MDN or Medium).
returns the primitive value of whatever the object is: for Number, String, and boolean, their primitive values; for Function, the function text, and so on.
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:
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.
one scope that is created for the outermost application environment. All global variables, functions, and objects are
contained within this outer scope.
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.
reducing the need to repeat complex and CPU-intensive computations.
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.
could be validated before the data was sent to the server, saving the reader time and the website extra processing.
modify form elements based on the data the web reader provides, such as filling a selection list with the names of cities when a certain state is selected.
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.
it’s also a simple way to process all form elements in a loop:
Whenever you’re accessing data from a text or other field where the user can input whatever value he wants, before sending to the database or displaying in the page, you will want to