JavaScript – walk-through – Part 1

In most languages, it is generally best to declare variables at the site of first use. That turns out to be a bad practice in JavaScript because it does not have block scope. It is better to declare all variables at the top of each function.
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !== . All of the comparisons just shown produce false with the === operator.

It is recommended that you always use JSON.parse instead of eval to defend against server incompetence. It is also good practice for the day when the
browser provides safe data access to other servers.

undefined detection

When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:

 

let vs var

Variable hoisting
Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception.

const keyword

operators

Property names that are not valid identifiers also cannot be accessed as a dot (.) property, but can be accessed and set with the array-like notation(“[]”).

If the finally block returns a value, this value becomes the return value of the entire try-catch-finally production, regardless of any return statements in the try and catch blocks.

Starting with ECMAScript 6, JavaScript gains support for Promise objects allowing you to control the flow of deferred and asynchronous operations.

A simple example using Promise and XMLHttpRequest

The following function takes as its argument an object and the object’s name. It then iterates over all the object’s properties and returns a string that lists the property names and their values. dump_props

for…of statement

A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case).

It is possible to convert any recursive algorithm to a non-recursive one, but often the logic is much more complex and doing so requires the use of a stack. In fact, recursion itself uses a stack: the function stack.

You can nest a function within a function. The nested (inner) function is private to its containing (outer) function. It also forms a closure.
A closure is an expression (typically a function) that can have free variables together with an environment that binds those variables (that “closes” the expression).

Using the arguments object
The arguments of a function are maintained in an array-like object. Within a function, you can address the arguments passed to it as follows:

Lexical this 

Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an “object method”, etc.). This proved to be annoying with an object-oriented style of programming.

Alternatively, a bound function could be created so that the proper this value would be passed to the growUp() function.

Arrow functions capture the this value of the enclosing context, so the following code works as expected.

Bitwise logical operators

delete

The delete operator deletes an object, an object’s property, or an element at a specified index in an array. The syntax is:

array, delete vs undefined

The void operator specifies an expression to be evaluated without returning a value. expression is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them.

in

The in operator returns true if the specified property is in the specified object.

Use instanceof when you need to confirm the type of an object at runtime. For example, when catching exceptions, you can branch to different exception-handling code depending on the type of exception thrown

Date in javascript
In the following example, the function JSClock() returns the time in the format of a digital clock.

Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:

Methods that use regular expressions

When you want to know whether a pattern is found in a string, use the ​test or search ​​method; for more information (but slower execution) use the exec or
match methods.

Advanced searching with flags
Regular expressions have four optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any
order, and are included as part of the regular expression.

foreach
Since JavaScript elements are saved as standard object properties, ​it is not advisable to iterate through JavaScript arrays using for…in loops
because normal elements and all enumerable properties will be listed​​.

array and sort

map​​(callback[, thisObject]) ​returns a new array of the return value from executing callback on every array item​​.
Apply

filter​​(callback[, thisObject]) ​returns a new array containing the items for which callback returned true​​.
Where

reduce​​(callback[, initialValue]) applies ​callback(firstValue, secondValue) to reduce the list of items down to a single value​​.
Group

Map object
ECMAScript 6 introduces a new data structure to map values to values. A Map object is a simple key/value map and can iterate its elements in insertion
order

Use maps ​​over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.
Use objects ​​when there is logic that operates on individual elements.

One use case of WeakMap objects is to store private data for an object or tohide implementation details.

Set objects are collections of values. You can iterate its elements in insertion order.
A value in a Set may only occur once; it is unique in the Set’s collection.

WeakSet ​​objects are collections of objects. An object in the WeakSet may only occur once; it is unique in the WeakSet’s collection and objects are not
enumerable. The use cases of WeakSet objects are limited. ​They will not leak memory ​​so it can be safe to use DOM elements as a key and mark them
for tracking purposes.

Before ECMAScript 5, there was no native way to list all properties of an object. However, this can be achieved with the following function:

Using the Object.create method

The following illustrates how getters and setters could work for a user­defined object o.

A prototype­based language, such as JavaScript, does not make this distinction: it simply has objects. A prototype­ based language has the notion of a
prototypical object, ​an object used as a template from which to get the initial properties for a new object​​. ​Any object can specify its own
properties, either when you create it or at run time​​. In addition, any object can be associated as the prototype for another object, allowing the second
object to share the first object’s properties.

You can use call to chain constructors for an object, similar to Java.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *