Download PDF defindate guide of java script






















Your email address will not be published. Save my name, email, and website in this browser for the next time I comment. How to Visualize Data with D3 [Video]. How to Visualize Data with R [Video]. Business Today May 05, If the value is null or undefined, the expression throws a TypeError, since these are the two JavaScript values that cannot have properties. If the object expression is followed by a dot and an identifier, the value of the property named by that identifier is looked up and becomes the overall value of the expression.

If the object expression is followed by another expression in square brackets, that second expression is evaluated and converted to a string. The overall value of the expression is then the value of the property named by that string.

In either case, if the named property does not exist, then the value of the property access expression is undefined. If the property name is a reserved word or includes spaces or punctuation characters, or when it is a number for arrays , you must use the square bracket notation. Objects and their properties are covered in detail in Chapter 6, and arrays and their elements are covered in Chapter 7.

It starts with a function expression that identifies the function to be called. The function expression is followed by an open parenthesis, a comma-separated list of zero or more argument expressions, and a close parenthesis. When an invocation expression is evaluated, the function expression is evaluated first, and then the argument expressions are evaluated to produce a list of argument values. If the value of the function expression is not a callable object, a TypeError is thrown.

All functions are callable. Host objects may also be callable even if they are not functions. Next, the argument values are assigned, in order, to the parameter names specified when the function was defined, and then the body of the function is executed. If the function uses a return statement to return a value, then that value becomes the value of the invocation expression.

Otherwise, the value of the invocation expression is undefined. Complete details on function invocation, including an explanation of what happens when the number of argument expressions does not match the number of parameters in the function definition, are in Chapter 8. Every invocation expression includes a pair of parentheses and an expression before the open parenthesis. If that expression is a property access expression, then the invocation is known as a method invocation.

In method invocations, the object or array that is the subject of the property access becomes the value of the this parameter while the body of the function is being executed. See Chapter 9 for details.

Invocation expressions that are not method invocations normally use the global object as the value of the this keyword. In ECMAScript 5, however, functions that are defined in strict mode are invoked with undefined as their this value rather than the global object. Object creation expressions are like invocation expressions except that they are prefixed with the keyword new: new Object new Point 2,3 4.

Next, it invokes the specified function with the specified arguments, passing the new object as the value of the this keyword. The function can then use this to initialize the properties of the newly created object.

Functions written for use as constructors do not return a value, and the value of the object creation expression is the newly created and initialized object. If a constructor does return an object value, that value becomes the value of the object creation expression and the newly created object is discarded.

Constructors are explained in more detail in Chapter 9. Table summarizes the operators and serves as a convenient reference. Some, however, are represented by keywords such as delete and instanceof. Keyword operators are regular operators, just like those expressed with punctuation; they simply have a less succinct syntax.

Table is organized by operator precedence. The operators listed first have higher precedence than those listed last. Operators separated by a horizontal line have different precedence levels. The column labeled A gives the operator associativity, which can be L left-to-right or R right-to-left , and the column N specifies the number of operands.

The subsections that follow the table explain the concepts of precedence, associativity, and operand type. The operators themselves are individually documented following that discussion. That is, they expect two operands. JavaScript also supports a number of unary operators, which convert a single expression into a single, more complex expression.

Finally, JavaScript supports one ternary operator, the conditional operator? The Types column in Table specifies operand types before the arrow and result type after the arrow for the operators.

Some operators behave differently depending on the type of the operands used with them. Similarly, the comparison operators such as 4. The ECMAScript specification allows built-in functions to return lvalues but does not define any functions that behave that way. Some expressions, however, have side effects, and their evaluation may affect the result of future evaluations.

The assignment operators are the most obvious example: if you assign a value to a variable or property, that changes the value of any expression that uses that variable or property.

The delete operator also has side effects: deleting a property is like but not the same as assigning undefined to the property. Operator precedence controls the order in which operations are performed. Operators with higher precedence nearer the top of the table are performed before those with lower precedence nearer to the bottom. Operator precedence can be overridden with the explicit use of parentheses. Consider this expression: typeof my. In practice, if you are at all unsure about the precedence of your operators, the simplest thing to do is to use parentheses to make the evaluation order explicit.

The rules that are important to know are these: multiplication and division are performed before addition and subtraction, and assignment has very low precedence and is almost always performed last. A value of L specifies left-to-right associativity, and a value of R specifies right-to-left associativity. The associativity of an operator specifies the order in which operations of the same precedence are performed.

Left-to-right associativity means that operations are performed from left to right. JavaScript always evaluates expressions in strictly leftto-right order. Then the values of y and z are multiplied, added to the value of x, and assigned to the variable or property specified by expression w. Adding parentheses to the expressions can change the relative order of the multiplication, addition, and assignment, but not the left-to-right order of evaluation.

Order of evaluation only makes a difference if any of the expressions being evaluated has side effects that affect the value of another expression. If expression x increments a variable that is used by expression z, then the fact that x is evaluated before z is important.

The multiplication, division, and subtraction operators are straightforward and are covered first. The addition operator gets a subsection of its own because it can also perform string concatenation and has some unusual type conversion rules. The unary operators and the bitwise operators are also covered in subsections of their own. The other basic four operators simply evaluate their operands, convert the values to numbers if necessary, and then compute the product, quotient, remainder, or difference between the values.

Non-numeric operands that cannot convert to numbers convert to the NaN value. If either operand is or converts to NaN, the result of the operation is also NaN. In other words, it returns the remainder after whole-number division of the first operand by the second operand. The sign of the result is the same as the sign of the first operand. While the modulo operator is typically used with integer operands, it also works for floating-point values.

For example, 6. In any other case, however, type conversion is necessary, and the operation to be performed depends on the conversion performed. Addition is performed only if neither operand is string-like. Most objects do not have a useful valueOf method, however, so they are converted via toString as well.

If you are used to programming languages that distinguish between integer and floating-point numbers, you might expect to get an integer result when you divide one integer by another. That is, the result may depend on the order in which operations are performed. In the second line, parentheses alter this order of operations: the number 2 is concatenated with the string to produce a new string. Then the number 1 is concatenated with the new string to produce the final result.

In JavaScript, the unary operators all have high precedence and are all right-associative. Unary minus - When - is used as a unary operator, it converts its operand to a number, if necessary, and then changes the sign of the result. The operator converts its operand to a number, adds 1 to that number, and assigns the incremented value back into the variable, element, or property.

When used before the operand, where it is known as the pre-increment operator, it increments the operand and evaluates to the incremented value of that operand. When used after the operand, where it is known as the post-increment operator, it increments its operand but evaluates to the unincremented value of that operand. Although they do not perform traditional arithmetic operations, they are categorized as arithmetic operators here because they operate on numeric operands and return a numeric value.

These operators are not commonly used in JavaScript programming, and if you are not familiar with the binary representation of decimal integers, you can probably skip this section.

The other three bitwise operators are used to shift bits left and right. The bitwise operators expect integer operands and behave as if those values were represented as bit integers rather than bit floating-point values. These operators convert their operands to numbers, if necessary, and then coerce the numeric values to bit integers by dropping any fractional part and any bits beyond the 32nd.

The shift operators require a right-side operand between 0 and After converting this operand to an unsigned bit integer, they drop any bits beyond the 5th, which yields a number in the appropriate range. Surprisingly, NaN, Infinity, and -Infinity all convert to 0 when used as operands of these bitwise operators. A bit is set in the result only if the corresponding bit is set in both operands.

If you do so, JavaScript will treat the operand as a complete statement by itself and insert a semicolon before it. Decrement -- The -- operator expects an lvalue operand. It converts the value of the operand to a number, subtracts 1, and assigns the decremented value back to the operand. When used before the operand, it decrements and returns the decremented value.

When used after the operand, it decrements the operand but returns the undecremented value. When used after its operand, no line break is allowed between the operand and the operator.

A bit is set in the result if the corresponding bit is set in one or both of the operands. For example, 0x 0x00FF evaluates to 0x12FF. Exclusive OR means that either operand one is true or operand two is true, but not both. It operates by reversing all bits in the operand. Relational expressions always evaluate to a boolean value, and that value is often used to control the flow of program execution in if, while, and for statements see Chapter 5.

Both operators accept operands of any type, and both return true if their operands are the same and false if they are different. Be sure you understand the differences between these assignment, equality, and strict equality operators, and be careful to use the correct one when coding! This makes it easy to remember that! An object is equal to itself, but not to any other object. If two distinct objects have the same number of properties, with the same names and values, they are still not equal.

Two arrays that have the same elements in the same order are not equal to each other. The NaN value is never equal to any other value, including itself! To check whether a value x is NaN, use x! NaN is the only value of x for which this expression will be true. If one value is 0 and the other is -0, they are also equal. If the strings differ in length or content, they are not equal.

Two strings may have the same meaning and the same visual appearance, but still be encoded using different sequences of bit values. See String. If they refer to different objects they are not equal, even if both objects have identical properties. If they are strictly equal, they are equal. If they are not strictly equal, they are not equal. Use the following rules and type conversions to check for equality: — If one value is null and the other is undefined, they are equal.

If either value is false, convert it to 0 and try the comparison again. An object is converted to a primitive value by either its toString method or its valueOf method. The built-in classes of core JavaScript attempt valueOf conversion before toString conversion, except for the Date class, which performs toString conversion.

Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way. The operands of these comparison operators may be of any type.

Comparison can be performed only on numbers and strings, however, so operands that are not numbers or strings are converted. Otherwise, the return value of its toString method is used. Infinity is larger than any number other than itself, and -Infinity is smaller than any number other than itself. If either operand is or converts to NaN, then the comparison operator always returns false.

Remember that JavaScript strings are sequences of bit integer values, and that string comparison is just a numerical comparison of the values in the two strings. The numerical encoding order defined by Unicode may not match the traditional collation order used in any particular language or locale. The boolean value true is first converted to the number 1, and the comparison is done again. Next, the string "1" is converted to the number 1.

Since both values are now the same, the comparison returns true. This rule can cause confusing results if you do not expect it. Result is 3. Result is "12". Numeric comparison.

Result is false. String comparison. Result is true. It expects a right-side operand that is an object. It evaluates to true if the left-side value is the name of a property of the right-side object. The operator evaluates to true if the left-side object is an instance of the right-side class and evaluates to false otherwise. If the left-side operand of instanceof is not an object, instanceof returns false.

If the right-hand side is not a function, it throws a TypeError. To evaluate the expression o instanceof f, JavaScript evaluates f. If it finds it, then o is an instance of f or of a superclass of f and the operator returns true. These operators are described in the subsections that follow. If one or both of these operands is false, it returns false. The falsy values are false, 4. Thus, the right-side operand of instanceof should be a function.

Here are examples: null, undefined, 0, -0, NaN, and "". All other values, including all objects, are truthy. If both operands are truthy, the operator returns a truthy value. Otherwise, one or both operands must be falsy, and the operator returns a falsy value. This operator starts by evaluating its first operand, the expression on its left.

On the other hand, if the value on the left is truthy, then the overall value of the expression depends on the value on the right-hand side. If the value on the right is truthy, then the overall value must be truthy, and if the value on the right is falsy, then the overall value must be falsy.

In the code above, the variable p is set to null, and the expression p. Whether those side effects occur depends on the value of the left-hand side. Despite the somewhat complex way that this operator actually works, it is most commonly used as a simple Boolean algebra operator that works on truthy and falsy values.

If one or both operands is truthy, it returns a truthy value. If both operands are falsy, it returns a falsy value. If that is not defined use a hard-coded constant. Its purpose is to invert the boolean value of its operand. For example, if x is truthy! If x is falsy, then! This means that! As a unary operator,! It starts by evaluating its first operand, the expression on its left. If the value of this first operand is truthy, it returns that truthy value.

Otherwise, it evaluates its second operand, the expression on its right, and returns the value of that expression. It expects its right-side operand to be an arbitrary value of any type. The value of an assignment expression is the value of the right-side operand. Although assignment expressions are usually quite simple, you may sometimes see the value of an assignment expression used as part of a larger expression.

The assignment operator has right-to-left associativity, which means that when multiple assignment operators appear in an expression, they are evaluated from right to left. For numeric operands, it performs addition and assignment; for string operands, it performs concatenation and assignment. Table lists them all. In the second it is evaluated twice. The two cases will differ only if a includes side effects such as a function call or an increment operator.

If you find yourself using eval , you should think carefully about whether you really need to use it. The subsections below explain the basic use of eval and then explain two restricted versions of it that have less impact on the optimizer. Is eval a Function or an Operator?

The earliest versions of the language defined an eval function, and ever since then language designers and interpreter writers have been placing restrictions on it that make it more and more operator-like. Modern JavaScript interpreters perform a lot of code analysis and optimization. The problem with eval is that the code it evaluates is, in general, unanalyzable.

Generally speaking, if a function calls eval , the interpreter cannot optimize that function. This issue could have been avoided if eval was an operator and a reserved word.

If you pass any value other than a string, it simply returns that value. If you pass a string, it attempts to parse the string as JavaScript code, throwing a SyntaxError if it fails. If it successfully parses the string, then it evaluates the code and returns the value of the last expression or statement in the string or undefined if the last expression or statement had no value.

If the string throws an exception, the eval propagates that expression. The key thing about eval when invoked like this is that it uses the variable environment of the code that calls it. That is, it looks up the values of variables and defines new variables and functions in the same way that local code does. If a function defines a local variable x and then calls eval "x" , it will obtain the value of the local variable.

Note that the string of code you pass to eval must make syntactic sense on its own— you cannot use it to paste code fragments into a function. It makes no sense to write eval "return;" , for example, because return is only legal within functions, and the fact that the evaluated string uses the same variable environment as the calling function does not make it part of that function.

Otherwise eval will throw a SyntaxError. As a workaround, however, interpreters simply do less optimization on any function that calls eval. But what should a JavaScript interpreter do, however, if a script defines an alias for eval and then calls that function by another name? In order to simplify the job of JavaScript implementors, the ECMAScript 3 standard declared that interpreters did not have to allow this.

In practice, most implementors did something else. When invoked by any other name, eval would evaluate the string as if it were top-level global code.

Direct calls to eval use the variable environment of the calling context. Any other call—an indirect call—uses the global object as its variable environment and cannot read, write, or define local variables or functions. As noted at the beginning of this section, it is rare to truly need to evaluate a string of code. But if you do find it necessary, you are more likely to want to do a global eval than a local eval.

Before IE9, IE differs from other browsers: it does not do a global eval when eval is invoked by a different name. But IE does define a global function named execScript that executes its string argument as if it were a top-level script. Unlike eval , however, execScript always returns null. This means that in strict mode, evaluated code can query and set local variables, but it cannot define new variables or functions in the local scope.

You are not allowed to overwrite the eval function with a new value. This operator is sometimes written? Because this operator has three operands, the first goes before the?

The first operand is evaluated and interpreted as a boolean. If the value of the first operand is truthy, then the second operand is evaluated, and its value is returned. Otherwise, if the first operand is falsy, then the third operand is evaluated and its value is returned. Only one of the second and third operands is evaluated, never both. Its value is a string that specifies the type of the operand.

Although functions in JavaScript are a kind of object, the typeof operator considers functions to be sufficiently different that they have their own return value. Most browser vendors use native JavaScript function objects for the methods of their host objects. In IE9 these client-side methods are now true native function objects. When a property is deleted, the property ceases to exist.

If it is not an lvalue, the operator takes no action and returns true. Otherwise, delete attempts to delete the specified lvalue. Not all properties can be deleted, however: some built-in core and client-side properties are immune from deletion, and user-defined variables declared with the var statement cannot be deleted. Functions defined with the function statement and declared function parameters cannot be deleted either.

Outside of strict mode, no exception occurs in these cases and delete simply returns false to indicate that the operand could not be deleted.

Would raise an exception in strict mode. Argument is not an lvalue: returns true Define a property of the a global object without var Try to delete it: returns true in non-strict mode 1. In JavaScript, memory deallocation is handled automatically by garbage collection, and you never have to worry about explicitly freeing up memory. Use 'delete this. This operator is unusual and infrequently used: it evaluates its operand, then discards the value and returns undefined.

Since the operand value is discarded, using the void operator makes sense only if the operand has side effects. The most common use for this operator is in a client-side javascript: URL, where it allows you to evaluate an expression for its side effects without the browser displaying the value of the evaluated expression.

It evaluates its left operand, evaluates its right operand, and then returns the value of the right operand. By that analogy, statements are JavaScript sentences or commands.

Expressions are evaluated to produce a value, but statements are executed to make something happen. Expressions with side effects, such as assignments and function invocations, can stand alone as statements, and when used this way they are known as expression statements. A similar category of statements are the declaration statements that declare new variables and define new functions.

JavaScript programs are nothing more than a sequence of statements to execute. By default, the JavaScript interpreter executes these statements one after another in the order they are written. The sections that follow describe the various statements in JavaScript and explain their syntax. Table , at the end of the chapter, summarizes the syntax. A JavaScript program is simply a sequence of statements, separated from one another with semicolons, so once you are familiar with the statements of JavaScript, you can begin writing JavaScript programs.

This sort of statement was shown in Chapter 4. Assignment statements are one major category of expression statements. Thus, it is almost always used as a statement, rather than as part of a larger expression: delete o. For example: alert greeting ; window. If a function does not have any side effects, there is no sense in calling it, unless it is part of a larger expression or an assignment statement.

A statement block is simply a sequence of statements enclosed within curly braces. Just as expressions often contain subexpressions, many JavaScript statements contain substatements.

Formally, JavaScript syntax usually allows a single substatement. For example, the while loop syntax includes a single statement that serves as the body of the loop.

Using a statement block, you can place any number of statements within this single allowed substatement. A compound statement allows you to use multiple statements where JavaScript syntax expects a single statement.

The empty statement is the opposite: it allows you to include no statements where one is expected. The empty statement looks like this: ; The JavaScript interpreter takes no action when it executes an empty statement. The empty statement is occasionally useful when you want to create a loop that has an empty body. JavaScript syntax requires a statement as a loop body, however, so an empty statement—just a bare semicolon—is used.

Note that the accidental inclusion of a semicolon after the right parenthesis of a for loop, while loop, or if statement can cause frustrating bugs that are difficult to detect. This line does nothing When you intentionally use the empty statement, it is a good idea to comment your code in a way that makes it clear that you are doing it on purpose.

These statements define identifiers variable and function names that can be used elsewhere in your program and assign values to those identifiers. First, it does not end with a semicolon. The primitive statements within the block end in semicolons, but the block itself does not. Second, the lines inside the block are indented relative to the curly braces that enclose them.

This is optional, but it makes the code easier to read and understand. The subsections that follow explain the var statement and the function statement, but do not cover variables and functions comprehensively.

And see Chapter 8 for complete details on functions. When var is used in top-level code, it declares global variables, visible throughout the JavaScript program. Unlike other global properties, however, properties created with var cannot be deleted.

Initialization, however, occurs at the location of the var statement, and the value of the variable is undefined before that point in the code. These variables are hoisted, just like variables declared outside of a loop. It can also be used in statement form.

The function name is followed by a comma-separated list of parameter names in parentheses. These identifiers can be used within the body of the function to refer to the argument values passed when the function is invoked. The body of the function is composed of any number of JavaScript statements, contained within curly braces.

These statements are not executed when the function is defined. Instead, they are associated with the new function object for execution when the function is invoked. Note that the curly braces are a required part of the function statement. Unlike statement blocks used with while loops and other statements, a function body requires curly braces, even if the body consists of only a single statement. If o already has an own noninherited property named x, then the assignment simply changes the value of this existing property.

Otherwise, the assignment creates a new property named x on the object o. If o previously inherited the property x, that inherited property is now hidden by the newly created own property with the same name. Property assignment examines the prototype chain to determine whether the assignment is allowed.

If o inherits a read-only property named x, for example, then the assignment is not allowed. If the assignment is allowed, however, it always creates or sets a property in the original object and never modifies the prototype chain.

This section explains the things that can go wrong when you query or set a property. It is not an error to query a property that does not exist. If the property x is not found as an own property or an inherited property of o, the property access expression o.

The null and undefined values have no properties, and it is an error to query properties of these values. Attempting to set a property on null or undefined also causes a TypeError, of course.

Attempts to set properties on other values do not always succeed, either: some properties are read-only and cannot be set, and some objects do not allow the addition of new properties. In strict mode, any failed attempt to set a property throws a TypeError exception. The rules that specify when a property assignment succeeds and when it fails are intuitive but difficult to express concisely.

See the defineProperty method, however, for an exception that allows configurable read-only properties to be set. If p does not already exist on o, and if there is no setter method to call, then p must be added to o. But if o is not extensible, then no new properties can be defined on it. Its single operand should be a property access expression. Surprisingly, delete does not operate on the value of the property but on the property itself: delete book. The delete operator only deletes own properties, not inherited ones.

To delete an inherited property, you must delete it from the prototype object in which it is defined. Doing this affects every object that inherits from that prototype. A delete expression evaluates to true if the delete succeeded or if the delete had no effect such as deleting a nonexistent property. Though it will remove configurable properties of nonextensible objects. Certain properties of built-in objects are nonconfigurable, as are properties of the global object created by variable declaration and function declaration.

In strict mode, attempting to delete a nonconfigurable property causes a TypeError. You can do this with the in operator, with the hasOwnProperty and propertyIsEnumerable methods, or simply by querying the property. The in operator expects a property name as a string on its left side and an object on its right. It returns true only if the named property is an own property and its enumerable attribute is true.

Certain built-in properties are not enumerable. It runs the body of the loop once for each enumerable property own or inherited of the specified object, assigning the name of the property to the loop variable.

Built-in methods that objects inherit are not enumerable, but the properties that your code adds to objects are enumerable unless you use one of the functions described later to make them nonenumerable. The extend function, in particular, is one that is commonly included in JavaScript utility libraries. The implementation of extend shown here is correct but does not compensate for a well-known bug in Internet Explorer.

The first is Object. It works just like the keys utility function shown in Example It works like Object. The return value of this method becomes the value of the property access expression. When a program sets the value of an accessor property, JavaScript invokes the setter method, passing the value of the right-hand side of the assignment. The return value of the setter method is ignored.

Accessor properties do not have a writable attribute as data properties do. If it has only a getter method, it is a read-only property. And if it has only a setter method, it is a write-only property something that is not possible with data properties and attempts to read it always evaluate to undefined.

Note that no colon is used to separate the name of the property from the functions that access that property, but that a comma is still required after the function body to separate the method from the next method or data property. As an example, consider the following object that represents a 2D Cartesian point. Properties defined by getters and setters are sometimes known as accessor properties to distinguish them from data properties that have a simple value.

JavaScript invokes these functions as methods of the object on which they are defined, which means that within the body of the function this refers to the point object.

So the getter method for the r property can refer to the x and y properties as this. Accessor properties are inherited, just as data properties are, so you can use the object p defined above as a prototype for other points. Variables in JavaScript are untyped. Although it has a syntax that is similar to Java, the two languages are otherwise unrelated.

David Flanagan is a computer programmer who spends the majority of his time writing about JavaScript and the Java programming language. David graduated from the Massachusetts Institute of Technology with a degree in computer science and engineering.

Note : If you likes to read the soft copy of this book, and you wants to buy hard copy of this book officially from the Publisher. Buy links to this book are given.



0コメント

  • 1000 / 1000