Preface:
I look up previous chapters of this book, and detailed records and expand the relevant knowledge points. Some of the later chapters, in part, I've known, some are relatively old, so I can not then be read and recorded.
1, grammar
whitespace
whitespace usually does not make sense, and sometimes need to separate sequence of characters, such as: var a = 1, the identifier var and a space in the middle.
Note
Use //
to annotate with respect to /* */
annotate will be more secure, because there may be expressions of positive *
problems together.
Personally feel /* */
this way split blocks will be more clear, and //
dedicated to the comment period will be more convenient.
Identifier
Identifier is used for statements, arguments, parameters, attribute name, operator and markers (e.g. markers for loop). When not using reserved words JS named variables, parameters, using the dot operator to extract the object attributes, object attributes can not be used as a reserved word, but the use of object[property]
extraction is possible.
digital
JS only one digital type, based on the double-precision 64-bit IEEE 754 standard value in binary format. JS not isolated since the integral type, in all the JS1 === 1.0
NaN is a numerical value that identifies a result can not produce a normal result. NaN is not equal to any value, even itself.
String
JS string is encased in a pile in ''
or ""
which may contain zero or more characters.
Backslash is an escape character.
Some allowed to escape to the escape character is inserted directly into the string of characters, such as
/
, , '
, "
tabs and the like.
Statement
Statement: an executable code, and not the total value
In the WEB browser, each <script>
tag will be compiled and executed to provide a compilation unit immediately. A compilation unit contains a set of executable statements.
Compilation unit is like a building block, usually "linker" to let them pile up one by one. But the lack of "linker" in JS, JS so choose to compile all units have thrown a global public space in order to achieve the effect of "linker" is.
JS statements usually runs from top to bottom. By conditional statements if
switch
, loop statements while
for
do
, forced jump statements break
return
throw
and to change the execution sequence of function calls.
while (expression) {block}
do {blcok} while(expression);
for (initalization;condition;increment) {block}
for (prop in property){block}
while
And do
the difference between that while
statement may not have time to perform, but do
will at least once.
for in
Statement can be used to enumerate the properties of an object.
JS is not allowed return
between expression and line breaks, are not allowed in break
between the label and line breaks.
try {block} catch(name) {block}
throw(expression)
throw
Statement can throw an exception if the throw
statement is used in the function, the function call will be abandoned, the control flow jumps to catch statement calling the function of the try statement.
expression
Expression: code values can be evaluated, and the results of the total
String, numeric, a variable, the value of the built-in to new
the beginning of the invocation expression, to delete
attribute the beginning of extraction expression, including expression in parentheses, with a front operator expression for the preamble ...
The JS expressions and operators
Basic keywords and common expressions
this
: Execution context pointers to functions.
fuction
: function
Keyword defines the function expression.
class
: class
Keyword defines a class expression.
function*
: Function * keyword defines a generator function expression.
yield
: Pause and resume function generator.
yield*
: Further objects assigned to a function generator or iterations.
[]
: Array literal syntax.
{}
: Object literal syntax.
/ab+c/i
: Regular literal syntax.
()
: Used to control the operation priority, the expression in parentheses will be evaluated and returned expression.
Left-hand-side expression
new
: Create an object new constructor[([arguments])]
instance .
super
: Call the constructor of the parent class.
.
[]
: Access object property or method.
Note: When we refer to an object's attributes, comparison will be between properties and methods. However, the difference between the properties and methods is not large. One way is a property that can be called nothing.
...obj
: Expand an operator may be deployed position iteration object into a plurality of function call parameters, or unfolded into a plurality of array elements in the array literals.
Increment and decrement operators
A++
: Post-increment operator returns the value before the increment.
++A
: Preincrement operator, returns a value after increment.
A--
: Post decrement operator returns the value before the decrement.
--A
: Pre-decrement operator, returns a value after decrement.
Unary operators
Unary operators: the operation is performed only for an expression.
delete
: Deleted object's properties.
void
: The expression is evaluated, and then return undefined
.
typeof
: Determining an object type.
+
: Unary plus operator, the numerical value is converted to operate with this type of non-numeric symbols can be converted to a numerical value.
-
: Unary minus operator, the value of the operation type and converted to Number negated.
~
: Bitwise operators, for a value of x for any bitwise operation is a non - (x + 1), the result is -6 to 5, and indexOf
used together ~str.indexOf(searchFor)
.
!
: Logical NOT operator
Arithmetic operators
+
, -
, *
, /
, %
: Add, subtract, multiply, divide, modulo, may be used to add string concatenation.
Relational Operators
in
: Determining whether the object with a given property.
instanceof
: Determining whether an object is an instance of another object.
<
, >
, <=
, >=
: Less than, greater than, less than or equal, greater than or equal.
Comma operator
,
: Each of its operands are evaluated (left to right), and a return value of the last operand. var
Statement is not a comma comma operator, which is the var
statement of a special symbol, a plurality of the variable declaration into a binding.
Literals
Literal constant is defined by the expression syntax
There are a variety of literals in JS, fixed values Literals are literally set forth, but not a variable.
-
Array literal
Array literal is wrapped in square brackets in the list of zero or more expressions. When you create an array using an array literal, the array is initialized to a value as an array element is specified and the length is the number of elements.
Array literal is also an array object initializer, further comprising object initialization
{}
. -
Object literal
Object literal use
{}
to represent, braces can be written as 0 or more "property: value" pairs. -
Regular literal
Use
/abc+c/
double slashes to represent -
Integer literals
Integer can be used in decimal, hexadecimal, octal, binary representation
-
Floating-point literal
Float syntax:
[(+|-)][digits][.digits][(E|e)[(+|-)]digits]
-
Boolean literal value
true
withfalse
-
String literal
Use
''
and""
wrapping zero or more characters, which is defined between the quotation marks isotype.
2, Object
JS objects in the collection is variable keying (What does this mean?).
Object two separate declarations will never be the same, even if they have the same attributes.
JS method to create an object:
-
Object literal
{}
can easily create an object, this is an easier method that appears later. -
Constructors can define the type of object, you need
new
to create the object key -
Object.create()
grammar:Object.create(proto, [ propertiesObject ])
prototype
Each object is linked to a prototype object, and inherit properties from the prototype object.
All through the object object literal creation of a prototype point Object.prototype
, which is the basis of JS objects.
When updating the prototype chain it does not work, when we modify an object without touching the prototype of the object. Prototype chain will only be used when searching, when the object is not the attribute name, retrieves the image down on the prototype chain until found Object.prototype
so far, if you still will not return undefined
. This process is called trust
delete
Use delete
can help you to delete the object's properties
3, function
Function contains a set of statements that are JS module unit basis, for code reuse, information hiding, and combinations call. Function is used to specify the behavior of objects. In general, the so-called programming, a set of requirements is decomposed into a set of functions and data structures skills.
JS is a function object.
Object is the "name / value" for the collection and has a hidden connection is connected to the prototype object.
Literal object is connected to Object.prototype
'
{}
–> Object.prototype
Connected to the function objectFunction.prototype
function foo(){}
–> Function.prototype
–> Object.prototype
Closure: object creation function by function literal comprising a connector connected to the external context, this is referred to as a closure.
this call manifested in different ways
-
Method is called when a function mode is saved as attributes of the object, which we call the method. When a method is called,
this
it points to the object.this
Subject to binding occurs when the method is called. Bythis
the method of access to the object context, we called the public methods. -
Property function call mode function if not an object, it will be treated as a function call. It is treated as a function call when the
this
defaults to the global object. -
Constructor calls a function if combined mode
new
to use, and that is the constructor function . JS is based on a prototype inheritance language, but most object-oriented languages are based on the type of language. The constructorthis
, throughnew
the call, will create a connection to the function of theprototype
new members of the object (object instance),this
it will be binding on this subject. -
apply
Calling patternapply
can dynamically changethis
point, which is a function of the method object.apply
This value can receive two parameters, the first one is to be bound, the second is an array of parameters. Its approach is very similarcall
,call
theapply
only difference is the way the incoming parameters.apply
Is passed in parameter array, andcall
is in turn passed parameters separated by commas.
parameter
When the function is called, the function will be assigned a default arguments
parameter, this parameter we can be orderly access to the parameter passed in the function. arguments
Is an array-like objects with length
properties, methods, but there is no array, but can be arguments
converted to a real array.
return
The function executes, if specified return
, will be the result of the implementation of the return. If not specified return
, the default is returned undefined
. When new
the function is called, if not specified return
, the default is returned this
, that is, this
the object is pointing.
Expansion of types of functions
Because JS is based on the prototype inheritance , so we can expand its functionality by adding methods to prototype objects.
Recursion
Put a problem into a set of sub-problems similar, each one with an unusual solution to resolve.
Scope
Scoped variables and parameters control the visibility and lifecycle. In many languages have block-level scope, defined at the block level in scope as the outer code blocks is not visible. And after the code block, code block variable is released.
In JS, there is no concept of block-level scope. JS only function scope of this concept.
Closure
Closure refers to a scope, which includes a function that can be invoked this closed scope variables, functions, and other closures. We generally to gain access to the closure by the function corresponding closures.
Callback
Is a callback function as a parameter to another function call, when the external function completes and then perform an internal function passed as a parameter, this process is a callback. Function is as a parameter is the callback function.
Module
Is a module that provides an interface function with or has hidden the object implementation.
Module mode is the general form: a function private variables and functions defined; use closures to create access to the private variables and functions of the privileged function; and finally return to this privileged function, or save them to a place where you can access to.
Template methods generally used in conjunction with single-mode embodiment. JS singleton is to create objects using object literal, and the properties of the object does not change over the life cycle. It usually provides support functions for the rest of the program as a tool.
Security modules can also be used to produce objects, written in private variables closure scope, and back out through the closure function. In addition to this method return visit is a private variable can not be changed in other ways.
cascade
Returns this object in the process, so that a cascade operating method to achieve object.do1().do2().do3()
the effect.
Currying
Currying allows us to function and the parameters passed to it combined to produce a new function. Converting multi-parameter function as a series of single-parameter function and technical calls.
memory
Do not repeat the calculation results have been obtained, the results have been calculated cached.
4, inheritance
Pseudo-classes
Other class-based language, an object is an instance of the class, and the class may inherit from another class. JS is a prototype-based language, which means that objects inherit directly from other objects.
Because JS language does not provide a way to determine which function is intended to make the constructor, all each function prototype
object.
Original: Big Box "Javascript language essence" notes