4.1 Primary Expressions
1.23 // A number literal
"hello" // A string literal
/pattern/ // A regular expression literal
true // Evalutes to the boolean true value
false // Evaluates to the boolean false value
null // Evaluates to the null value
this // Evaluates to the "current" object
i // Evaluates to the value of the variable i.
sum // Evaluates to the value of the variable sum.
undefined // undefined is a global variable, not a keyword like null.
4.2 Object and Array Initializers
[] // An empty array: no expressions inside brackets means no elements
[1+2,3+4] // A 2-element array. First element is 3, second is 7
var matrix = [[1,2,3], [4,5,6], [7,8,9]];
var sparseArray = [1,,,,5];
Object initializer expressions
var p = { x:2.3, y:-1.2 }; // An object with 2 properties
var q = {}; // An empty object with no properties
q.x = 2.3; q.y = -1.2; // Now q has the same properties as p
object字面量可以被嵌套:
var rectangle = { upperLeft: { x: 2, y: 2 },
lowerRight: { x: 4, y: 5 } };
var side = 1;
var square = { "upperLeft": { x: p.x, y: p.y },
'lowerRight': { x: p.x + side, y: p.y + side}};
4.4 Property Access Expressions
expression . identifier
expression [ expression ]
var o = {x:1,y:{z:3}}; // An example object
var a = [o,4,[5,6]]; // An example array that contains the object
o.x // => 1: property x of expression o
o.y.z // => 3: property z of expression o.y
o["x"] // => 1: property x of object o
a[1] // => 4: element at index 1 of expression a
a[2]["1"] // => 6: element at index 1 of expression a[2]
a[0].x // => 1: property x of expression a[0]
4.5 Invocation Expressions
f(0) // f is the function expression; 0 is the argument expression.
Math.max(x,y,z) // Math.max is the function; x, y and z are the arguments.
a.sort() // a.sort is the function; there are no arguments.
4.6 Object Creation Expressions
new Object()
new Point(2,3)
如果没有参数的话:
new Object
new Date
4.8.1 The + Operator
如果其中有一个是object,那么就将它变成原始变量,Date object调用toString,其他的object调用valueOf函数,多数不含有有效的valueOf函数,所以调用toString。
如果有任何一个变成了string,那么另一个也变成String来进行运算。
否则都变成number,来进行运算。
1 + 2 // => 3: addition
"1" + "2" // => "12": concatenation
"1" + 2 // => "12": concatenation after number-to-string
1 + {} // => "1[object Object]": concatenation after object-to-string
true + true // => 2: addition after boolean-to-number
2 + null // => 2: addition after null converts to 0
2 + undefined // => NaN: addition after undefined converts to NaN
1 + 2 + " blind mice"; // => "3 blind mice"
1 + (2 + " blind mice"); // => "12 blind mice"
1 + 2 // Addition. Result is 3.
"1" + "2" // Concatenation. Result is "12".
"1" + 2 // Concatenation. 2 is converted to "2". Result is "12".
11 < 3 // Numeric comparison. Result is false.
"11" < "3" // String comparison. Result is true.
"11" < 3 // Numeric comparison. "11" converted to 11. Result is false.
"one" < 3 // Numeric comparison. "one" converted to NaN. Result is false.
4.9.3 The in Operator
var point = { x:1, y:1 }; // Define an object
"x" in point // => true: object has property named "x"
"z" in point // => false: object has no "z" property.
"toString" in point // => true: object inherits toString method
var data = [7,8,9]; // An array with elements 0, 1, and 2
"0" in data // => true: array has an element "0"
1 in data // => true: numbers are converted to strings
3 in data // => false: no element 3
4.9.4 The instanceof Operator
var d = new Date(); // Create a new object with the Date() constructor
d instanceof Date; // Evaluates to true; d was created with Date()
d instanceof Object; // Evaluates to true; all objects are instances of Object
d instanceof Number; // Evaluates to false; d is not a Number object
var a = [1, 2, 3]; // Create an array with array literal syntax
a instanceof Array; // Evaluates to true; a is an array
a instanceof Object; // Evaluates to true; all arrays are objects
a instanceof RegExp; // Evaluates to false; arrays are not regular expressions
4.12 Evaluation Expressions
javascript可以解释String到javascript代码,评价出它们的值,它使用全局范围的eval函数,如
eval("3+2") // => 5
eval里面如果使用x=1的语句,那么x这个变量的值将会被改变
4.13.3 The delete Operator
var o = { x: 1, y: 2}; // Start with an object
delete o.x; // Delete one of its properties
"x" in o // => false: the property does not exist anymore
var a = [1,2,3]; // Start with an array
delete a[2]; // Delete the last element of the array
a.length // => 2: array only has two elements now
var o = {x:1, y:2}; // Define a variable; initialize it to an object
delete o.x; // Delete one of the object properties; returns true
typeof o.x; // Property does not exist; returns "undefined"
delete o.x; // Delete a nonexistent property; returns true
delete o; // Can't delete a declared variable; returns false.
// Would raise an exception in strict mode.
delete 1; // Argument is not an lvalue: returns true
this.x = 1; // Define a property of the a global object without var
delete x; // Try to delete it: returns true in non-strict mode
// Exception in strict mode. Use 'delete this.x' instead
x; // Runtime error: x is not defined
分享到:
相关推荐
Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects Chapter 7 Arrays Chapter 8 Functions Chapter 9 Classes and Modules Chapter 10 Pattern Matching with Regular Expressions ...
Chapter 4 Expressions and Operators Chapter 5 Statements Chapter 6 Objects Chapter 7 Arrays Chapter 8 Functions Chapter 9 Classes and Modules Chapter 10 Pattern Matching with Regular Expressions ...
Expressions and Operators Chapter 6. Statements Chapter 7. Functions Chapter 8. Arrays Chapter 9. Pointers Chapter 10. Structures and Unions and Bit-Fields Chapter 11. Declarations Chapter 12. ...
Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and ...
Chapter 9 Strings And Regular Expressions Chapter 10 Collections Chapter 11 Special Collections Chapter 12 Language Integrated Query Chapter 13 Functional Programming With C# Chapter 14 Errors And ...
Chapter 4. A Tour of C++: Containers and Algorithms Chapter 5. A Tour of C++: Concurrency and Utilities Part II: Basic Facilities Chapter 6. Types and Declarations Chapter 7. Pointers, Arrays, and ...
Chapter 12 Complex Mathematical Expressions Chapter 13 Exercises with a Quotient and a Remainder Chapter 14 Manipulating Strings Section 4 Decision Control Structures Chapter 15 Introduction to ...
Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and ...
Chapter 4: Getting Started with Expressions Chapter 5: Control Structures Chapter 6: Functions and Libraries Chapter 7: Using Classes Chapter 8: More Selection Control Structures Chapter 9: More ...
Chapter 4. No Duplicates, No Nulls Chapter 5. Base Relvars, Base Tables Chapter 6. SQL and Relational Alegebra I: The Original Operators Chapter 7. SQL and Relational Algebra II: Additional Operators ...
Chapter 4. Introduction to Object-Oriented Programming Chapter 5. Constants, Variables, and Data Types Chapter 6. Operators Chapter 7. Flow Control Chapter 8. Functions and Closures Chapter 9. Classes...
Chapter 4. How JavaScript Was Created Chapter 5. Standardization: ECMAScript Chapter 6. Historical JavaScript Milestones Part III: JavaScript in Depth Chapter 7. JavaScript’s Syntax Chapter 8. ...
Chapter 4. Functions Chapter 5. First Class Functions Chapter 6. Common Collections Chapter 7. More Collections Part II. Object-Oriented Scala Chapter 8. Classes Chapter 9. Objects, Case Classes and ...