learning-javascript

advertisement
1,Lexical Structure
1.1,Character Set
(1)ECMAScriptv1 and v2 standard allows Unicode characters only in
commons and quoted string literals; all elements are restricted to the
ASCII character set.
(2)ECMAScriptv3 standard allows Unicode character anywhere in a
JavaScript program.
(3)Versions of JavaScript that ECMAScript standardization typically do
not support Unicode at all.
1.2,Case Sensitivity
(1)HTML is not case-sensitive.
(2)JavaScript is case-sensitive.
(3)XHTML is case-sensitive.
1.3,Literals
12
1.2
"hello world"
'Hi'
true
false
/javascript/gi
matching)
null
//
//
//
//
//
//
//
The number twelve
The number one point two
A string of text
Another string
A Boolean value
The other Boolean value
A "regular expression" literal (for pattern
// Absence of an object
In ECMAScript v3, expressions that serve as array and object literals
are also supported. For example:
{ x:1, y:2 }
[1,2,3,4,5]
// An object initializer
// An array initialize
2,Datatypes(JavaScript supports)
2.1,Three primitive datatypes



numbers
string of text(known as strings)
Boolean truth values(known as booleans)
2.1.1, numbers
All numbers in JavaScript are represented as floating-point values.(the same implementation of
Java’s double type.)
Note that any numeric literal can be preceded by a minus sign (-) to make the number negative.
Technically, however, - is the unary negation operator and is not part of the numeric literal
syntax.
2.1.1.1, Integer
2.1.1.2, Hexadecimal and Octal Literals
A hexadecimal literal begins with "0x" or "0X", followed by a string of hexadecimal digits. A
hexadecimal digit is one of the digits 0 through 9 or the letters a (or A) through f (or F), which
represent values 10 through 15.
Although the ECMAScript standard does not support them, some implementations of JavaScript
allow you to specify integer literals in octal (base-8) format. An octal literal begins with the digit
0 and is followed by a sequence of digits, each between 0 and 7. For example:
0377 // 3*64 + 7*8 + 7 = 255 (base 10)
Since some implementations support octal literals and some do not, you should never write an
integer literal with a leading zero; you cannot know in this case whether an implementation will
interpret it as an octal or decimal value.
2.1.1.3, Floating-Point Literals
look lilke :
[digits][.digits][(E|e)[(+|-)]digits]
These are all valid float number:
3.14
2345.789
.333333333333333333
6.02e23
// 6.02 x 1023
1.4738223E-32 // 1.4738223 x 10-32
2.2,two trivial datatypes

null

undefined
each of which only defines only a single value.
2.3,Composite datatypes
2.3.1, Object
An object represents a collection of values (either primitive values,
such as numbers and strings, or composite values, such as other
objects). Objects in JavaScript have a dual nature: an object can
represent an unordered collection of named values or an ordered
collection of numbered values. In the latter case, the object is called
an array. Although objects and arrays are fundamentally the same
datatype in JavaScript, they behave quite differently and will usually
be considered distinct types.
2.3.1.1 object[‘property’].
See example below:
if we have an object
var Person = {
id:1,
name:’John’
}
then Person[‘name’] is equivalent to ‘john’
2.3.2, array
an ordered collection of numbered values.
2.3.2.1 object[index]
this is the typical usage of an array. We could get any element within
the array.
2.3.3, function
JavaScript defines another special kind of object, known as a function.
A function is an object that has executable code associated with it. A
function may be invoked to perform some kind of operation. Like arrays,
functions behave differently from other kinds of objects, and
JavaScript defines a special language syntax for working with them.
Thus, we'll treat the function datatype independently of the object and
array types.
2.3.4, Date
Represents dates
2.3.5, RegExp
Represents regular expression
2.3.6, Error
Represents syntax the runtime errors that can occur in a JavaScript
program.
DOM
Document
Method:
getElementsByName
(1)how to get an element by name, using the following script:
var nodes = null;
var ele = null;
// Now it is time to look by name
nodes = document.getElementsByName(inputName);
if (nodes.length >= 1) ele = nodes.item(0);
Element
Method:
setAttribute(“attribute”,”value of this attribute”);
property:
childNodes
nodeType
--
type of Array.
Array
Method:
item(i) -- to get the item at index i.
sort(options) -- options is optional.
(1)If options is messing, the JavaScript compare every two object in
this array.
(2)If options is available. It should be a function which has two
parameters, and this function should return value should be -1 or 0 or
1. See example below.
people.sort(function(p1,p2) { return p1.name.localeCompare(p2.name); });
property:
length
--
size of array.
Download