æ¬ç« è¨è« JavaScript çåºæ¬èªæ³èåºç¤è³æé¡åãå æ¬è®æ¸ã常æ¸ãåå 常æ¸
åºç¤ç¥èJavaScript 許å¤åºæ¬èªæ³åéèª Javaï¼C ææ¯ C++ï¼ä½äº¦å AwkãPerl å Python çå½±é¿ã
JavaScript æ¯ Case-sensitiveï¼åå大å°å¯«ï¼ä¸¦ä½¿ç¨ Unicode 編碼ãèä¾ä¾èªªï¼Früh (å¾·æç"early") å¯ä»¥ç¶ä½è®æ¸çå稱ã
var Früh = "foobar";
ä½è®æ¸ früh 並ä¸çæ¼ Frühï¼å çºå¤§å°å¯«å° JavaScript æ¯æåå¥çã
å¨ JavaScript ä¸ï¼æ¯è¡æä»¤è¢«ç¨±çº Statementsï¼ä¸¦ç¨åèï¼;ï¼åéãç©ºæ ¼ãTab èæè¡ç¬¦èç被è¦çºç©ºç½ãJavaScript çæä»¶æå¾å·¦å°å³é²è¡ææï¼ä¸¦è½ææä¸ç³»åçå ç´ ï¼åæ¯ä»¤çï¼Tokenï¼ãæ§å¶å符ï¼Control charactersï¼ãæè¡å¨ï¼line terminatorsï¼ã註解ï¼Commentsï¼ææ¯ç©ºç½ï¼Withespaceï¼ï¼ECMAScript ä¹å®ç¾©äºç¹å®çä¿çåååé¢å¼ï¼ä¸¦å¨æ¯åæ²æå åèç Statement èªåå ä¸åèãç¶èï¼æ¨è¦ç使³éæ¯å¨æ¯å Statement ççµå°¾èªè¡å ä¸åèï¼ä»¥é²æ¢ä¸äºæ½å¨çå¯ä½ç¨ï¼å¦æéè¦æ´å¤è³è¨ï¼å¯ä»¥åèéç¯ã
è¨»è§£èªæ³è· C++ åå ¶ä»èªè¨ç¸åï¼
// a one line comment
/* this is a longer,
multi-line comment
*/
/* You can't, however, /* nest comments */ SyntaxError */
宣åï¼Declarationsï¼
JavaScript æä¸ç¨®å®£åæ¹å¼
var
宣åä¸åå¯é¨ææ´æ¹å ¶å §å®¹çè®æ¸
let
宣åä¸åå¯é¨ææ´æ¹å ¶å §å®¹çåå¡ååè®æ¸
const
宣åä¸ååªå¯è®åçä¸å¯è®å¸¸æ¸
è®æ¸ï¼variableï¼æ¯å°å¼ï¼valueï¼çå¼ç¨ï¼è®æ¸çåç¨±è¢«ç¨±çº identifiers éè¦éµå¾ä¸å®çè¦åã
å¨ JavaScript ä¸ï¼è®æ¸å¿ é 使ç¨åæ¯ï¼letterï¼ãä¸åºç·ï¼_ï¼ãé¢èï¼$ï¼ä½çºéé ï¼å¾é¢çåå¡çµæå¯ä»¥å 嫿¸åï¼0-9ï¼ãJavaScript æ¯åå大å°å¯«ï¼case sensitiveï¼çï¼å¤§å¯«åæ¯ï¼'A' ~ 'Z'ï¼åå°å¯«åæ¯ï¼'a' ~ 'z'ï¼çå¯ä½¿ç¨ä¸ä¸ç¸çã
You can use most of ISO 8859-1 or Unicode letters such as å and ü in identifiers (for more details see this blog post). You can also use the Unicode escape sequences as characters in identifiers.
Some examples of legal names are Number_hits
, temp99
, $credit
, and _name
.
ä½ å¯ä»¥ééä¸ç¨®æ¹å¼ä¾å®ç¾©è®æ¸ï¼
var
ä¾å®ç¾©è®æ¸ï¼èä¾ä¾èªªï¼ var x = 42
ï¼é種æ¹å¼å¯ä»¥ç¨ä¾å®ç¾©åå以åå
¨åè®æ¸ãx = 42
ï¼é種æ¹å¼åªè½å®ç¾©å
¨åè®æ¸ï¼å¦æå¨æ¹æ³å¤é¢ä½¿ç¨è©²æ¹æ³å®ç¾©è®æ¸ï¼å´æ ¼æ¨¡å¼è£¡æç¢çè¦åï¼è©²å®ç¾©æ¹å¼æè©²ç¡å¯è½é¿å
ãlet
ï¼èä¾ä¾èªªï¼let y = 13
ï¼let
å¯ä»¥ç¨ä¾å®ç¾©åå¡è£¡çååè®æ¸ãæ³çè§£æ´å¤ï¼å¯ä»¥åèè®æ¸ååçç« ç¯ãè®æ¸å¯ä»¥éé var
ææ¯ let
ä¾å®ç¾©ï¼å¦æå°æªæå®æ¸å¼çµ¦è©²è®æ¸ï¼é£éº¼è©²è®æ¸çå¼ææ¯ undefined
ã妿å試å»ååæªå®ç¾©çè®æ¸ï¼æè·³åº ReferenceError
çä¾å¤ã
var a;
console.log("The value of a is " + a); // The value of a is undefined
console.log("The value of b is " + b); // The value of b is undefined
var b;
console.log("The value of c is " + c); // Uncaught ReferenceError: c is not defined
let x;
console.log("The value of x is " + x); // The value of x is undefined
console.log("The value of y is " + y); // Uncaught ReferenceError: y is not defined
let y;
ä½ å¯ä»¥å©ç¨ undefined
ä¾å¤æ·è©²è®æ¸æ¯å¦æå¼ï¼å¨ä¸é¢çç¨å¼ç¢¼çä¾åä¸ï¼input
éåè®æ¸æ²æè³¦å¼ï¼if
夿·å¼æå¾å° true
ççµæã
var input;
if (input === undefined) {
doThis();
} else {
doThat();
}
è¢«è³¦äº undefined
çè®æ¸ï¼å¨è¢«ç¶å叿å¼çæ
å¢ä¸é½æè¢«è¦çº false
ï¼ä»¥ä¸é¢çä¾åä¾èªªï¼ç¨å¼ç¢¼æå·è¡ myFunction
ï¼å çº myArray
æ¯ undefined
ï¼
var myArray = [];
if (!myArray[0]) myFunction();
è¢«è³¦äº undefined
çè®æ¸ï¼å¨åæ¸å¼é²è¡éç®ä¹å¾ï¼æè¢«è½æéæ¸å¼ï¼NaN
ï¼ï¼
var a;
a + 2; // Evaluates to NaN
ç¶ä½ å° null
é²è¡éç®ï¼null
æèªåè½æææ¸å¼ 0ï¼å¦æç¶å叿å¼éç®ï¼æè¢«ç¶æ false
ï¼èä¾ä¾èªªï¼
var n = null;
console.log(n * 32); // Will log 0 to the console
è®æ¸ç¯å
ç¶æåå¨å½å¼å¤å®£åä¸åè®æ¸æï¼éåè®æ¸ææ¯ä¸åå ¨åè®æ¸ (global variable), å çºå¨é份ç¨å¼æä»¶è£¡é¢çææç¨å¼ç¢¼é½å¯ä»¥ä½¿ç¨å°éåè®æ¸ãä½ç¶æååªå¨å½å¼å §å®£åè®æ¸æï¼éè®æ¸æ¯ååè®æ¸ (local variable)ï¼å çºè®æ¸åªæå¨å½å¼å §è¢«ä½¿ç¨å°ã
è«æ³¨æ!! å¨ ECMAScript 2015 以åç JavaScript çæ¬è£¡ï¼ä¸¦æ²æå®ç¾©åå¡æè¿° (block statement) çè®æ¸ææç¯åãæ´ç²¾ç¢ºç說ï¼ä¹åçæ¬æå®ç¾©çè®æ¸ï¼å ¶ç¹æ§ç¸ç¶æ¼å ¨åè®æ¸ï¼ä¸åªå¨å®£åçå塿彿¸è£¡é¢ææ ï¼å ¶è®æ¸å¼éæè¶ åºå®£ååå¡èå½±é¿å°å ¨é¨çæè¿°ç¢¼ã
å¾ä¸é¢ä¾åä¾çï¼å ¶è¼¸åºçµæææ¯ 5ãéç¶ x æ¯å¨ if { } åå¡è£¡é¢è¢«å®£åçï¼ä½å»å çºæå ¨åè®æ¸çç¹æ§ï¼å æ¤æº¢åºå¤§æ¬èèæçºå¾çºæè¿°ç¢¼çè®æ¸å¼ã
if (true) {
var x = 5;
}
console.log(x); // x is 5
æ¥èèä¸å ECMAScript 2015 ä¹å¾ç宣åç¯ä¾ãç¶ä½¿ç¨äº let
éåååè®æ¸å®£åæ¹å¼ï¼è®æ¸ y çææç¯ååªæå¨ if { } çç¯åå
§ï¼å æ¤è¼¸åºçµææ¯ ReferenceErrorã
if (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined (yæ²æè¢«å®ç¾©)
è®æ¸æå
å¨ JavaScript ä¸å¦ä¸ä»¶æéè®æ¸ä¸å¸¸è¦çäº, æ¯ä½ å¯å¼ç¨ä¸åè¼æå®£åçè®æ¸ä¸¦ä¸ä¸ææç°å¸¸ãéåæ¦å¿µè¢«ç¨±çºãæåï¼hoistingï¼ãï¼å¾æç¾©ä¸ä¾èªªæï¼è®æ¸å¨ JavaScript 䏿¯ã被æåï¼hoistedï¼ãæã被æ¬è³ï¼liftedï¼ãå°å½å¼ï¼functionï¼æé³è¿°å¼ï¼statementï¼çé é¨ã ç¶èï¼è¢«æåï¼hoistedï¼çè®æ¸å°è¿åä¸åæªå®ç¾©çå¼ï¼undefinedï¼ãæä»¥å³ä½¿ä½ å¨ä½¿ç¨æè å¼ç¨éåè®æ¸ä¹å¾æå®£åååå§åå®ï¼å®ä»ç¶æè¿å宿¯ä¸åæªå®ç¾©çå¼ï¼undefinedï¼ã
/**
* Example 1
*/
console.log(x === undefined); // true
var x = 3;
/**
* Example 2
*/
// will return a value of undefined
var myvar = "my value";
(function () {
console.log(myvar); // undefined
var myvar = "local value";
})();
ä¸é¢çä¾åå¯ä»¥è½è¯æå¦ä¸ç¸åçç¨å¼:
/**
* Example 1
*/
var x;
console.log(x === undefined); // true
x = 3;
/**
* Example 2
*/
var myvar = "my value";
(function () {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})();
ç±æ¼æå(hoisting)ï¼å ¨é¨å¨å½æ¸(function) ä¸ç var é³è¿°å¼æè©²ç¡å¯è½å°ç½®æ¾å¨æ¥è¿å½æ¸(function)çé é¨ãéåæä½³å¯¦è¸å¢å äºç¨å¼ç¢¼çæ¸ æ°åº¦ã
å¨ ECMAScript 2015 ä¸ï¼letï¼constï¼ä¸æå°è®æ¸æåå°åå¡ï¼blockï¼çé é¨ã使¯ï¼å¨è®æ¸å®£åä¹åå°±å¼ç¨å¡ä¸çè®æ¸ï¼æå°è´ ReferenceError
ãè®æ¸å¾åå¡ï¼blockï¼çéå§å°å®£å被èçä¹åï¼å°±èæ¼ãæ«æç¡æï¼temporal dead zoneï¼ãã
console.log(x); // ReferenceError
let x = 3;
å½å¼æå
éå°å½å¼ä¾èªªï¼åªæå½å¼å®£åå¼(function declaration)ææå°é é¨ï¼ä½å½å¼è¡¨ç¤ºå¼(function exprssion) ä¸è¢«ææè³é é¨ã
/* Function declaration */
foo(); // "bar"
function foo() {
console.log("bar");
}
/* Function expression */
baz(); // TypeError: baz is not a function
var baz = function () {
console.log("bar2");
};
å
¨åè®æ¸ (Global variables)
å
¨åè®æ¸äºå¯¦ä¸æ¯å
¨åç©ä»¶ç屬æ§å¼ãå¨ç¶²é ä¸çå
¨åç©ä»¶æ¯ window
ï¼å æ¤ä½ å¯ä½¿ç¨ window.variable
çèªæ³ä¾è¨å®åååå
¨åè®æ¸ã
Consequently, ä½ å¯ä»¥æå® window æ frame ç©ä»¶çå稱ä¾ååå¨å¦ä¸åå¨ window ç©ä»¶æ frame ç©ä»¶æå®£åçå
¨åè®æ¸ãä¾å¦ï¼å¦æå¨ä¸åææªä¸å·²å®£åä¸åç¨±çº phoneNumber
çè®æ¸ï¼ä½ å¯ä»¥å¨ iframe ä¸ä½¿ç¨ parent.phoneNumber
ä¾ååè©²è®æ¸
ä½ å¯ç¨ const
ééµåä¾å»ºç«ä¸åå¯è®ãæå稱ç常æ¸ã 常æ¸èå¥åçå½åèªæ³èè®æ¸èå¥åçå½åèªæ³æ¯ä¸æ¨£ç: å¿
é ç±ä¸åè±æåæ¯ï¼åºç·æé¢ç¬¦è($)éå§ï¼ä¹å¾å¯å
å«è±æåæ¯ï¼æ¸åååºç·åå
ã
ç¶ç¨å¼å·è¡æï¼ç¡æ³åééè³¦å¼æéæ°å®£å便¹è®å¸¸æ¸å·²è¨å®çå¼ã常æ¸å¿ é 被åå§åã
The scope rules for constants are the same as those for let
block-scope variables. If the const
keyword is omitted, the identifier is assumed to represent a variable.
ä½ ä¸è½å¨åä¸åå±é¨ç¯åå §ä½¿ç¨èå ¶å®å½å¼æè®æ¸ç¸åçå稱ä¾å®£åè®æ¸ãä¾å¦:
// THIS WILL CAUSE AN ERROR
function f() {}
const f = 5;
// THIS WILL CAUSE AN ERROR ALSO
function f() {
const g = 5;
var g;
//statements
}
使¯å¸¸æ¸ç©ä»¶å §çç©ä»¶å±¬æ§ä¸¦ä¸åå°ä¿è·ï¼å æ¤ä»¥ä¸é³è¿°å¼å¯ä»¥æ£å¸¸å·è¡ã
const MY_OBJECT = { key: "value" };
MY_OBJECT.key = "otherValue";
è³æçµæ§ååå¥ è³æåå¥ (Data types)
ææ° ECMAScript æ¨æºå®ç¾©ä»¥ä¸ä¸ç¨®è³æåå¥:
å ç¨®åºæ¬(primitives)è³æåå¥ :
true
and false
.null
is not the same as Null
, NULL
, or any other variant.42
or 3.14159
.and Object
å管éäºè®æ¸éè¯æ§å¾å°ï¼ä»åå¯ä»¥è®ä½ å¨ä½ çæç¨ç¨å¼ä¸ï¼ç¢çåºææç¾©ç彿¸ã
ç©ä»¶è 彿¸ å¨èªè¨ä¸æ¯å ¶å®çåºæ¬å ç´ . ä½ å¯ä»¥æç©ä»¶æ³ææ¯ä¸å被å½åéä¸ç¨ä¾è£æ¸å¼ç容å¨,以å彿¸åçºä½ çæç¨ç¨å¼æå·è¡çæ¥é©.
è³æåå¥è½æJavaScript æ¯ä¸ååæ åå¥çèªè¨ï¼éæå³èä½ ä¸éè¦å¨å®£åè®æ¸æå®ç¾©å®çè³æåå¥ï¼ç¨å¼å·è¡ææèªåè½æï¼ä½ å¯ä»¥ç¨ä¸é¢æ¹å¼å®£åè®æ¸ï¼
ä½ å¯ä»¥ææ´¾å串å¨ååè®æ¸ä¸ï¼ä¾å¦ï¼
answer = "Thanks for all the fish...";
ç±æ¼ Javascript æ¯ä¸ååæ åå¥çèªè¨ï¼å æ¤é樣ç宣忹å¼ä¸æå°è´é¯èª¤ã
å¨è©²é³è¿°å¼ä¸ï¼å®èª¿ç¨äºå䏲忏åï¼ä¸¦ä½¿ç¨ + é²è¡éç®ï¼JavaScript æèªåææ¸åè½ææå串ï¼ä¾å¦ï¼
x = "The answer is " + 42; // "The answer is 42"
y = 42 + " is the answer"; // "42 is the answer"
å¨è©²é³è¿°å¼ä¸ï¼å®èª¿ç¨äºå ¶å®éç®åï¼JavaScript 就䏿尿¸åè½ææå串ï¼ä¾å¦ï¼
"37" - 7; // 30
"37" + 7; // "377"
åä¸²è½æ¸å¼
ç¶ä»£è¡¨æ¸åçå¼ä»¥å串形å¼åå¨è¨æ¶é«ä¸ï¼æäºæ¹æ³å¯ç¨ä¾å°é種åä¸²è½æææ´æ¸ææµ®é»æ¸ã
parseInt
åªæè¿åæ´æ¸ï¼å æ¤æ¸å°äºå°å°æ¸ç使ç¨ãæ¤å¤ï¼parseInt çæä½³å¯¦åæ¯å§çµå
å«åºæ¸åæ¸ãåºæ¸åæ¸ç¨æ¼æå®ä½¿ç¨çæ¸å¼ç³»çµ±ã
å¦ä¸åå°åä¸²è½ææ¸åæ¯ä½¿ç¨å®å
+
(unary plus) éç®å:
'1.1' + '1.1' = '1.11.1'
(+'1.1') + (+'1.1') = 2.2
// 注æ: æ¬èæ¯çºäºææ¼é±è®ï¼ä¸¦ä¸æ¯å¿
é ç.
åé¢å¼ï¼Literalsï¼
ä½ è½ä½¿ç¨åé¢å¼ä¾è¡¨ç¤º JavaScript ä¸çå¼ãéäºæ¯ä½ å¨è ³æ¬ä¸å¯¦éæä¾çåºå®å¼ï¼è䏿¯è®éãæ¬ç¯æè¿°ä»¥ä¸é¡åçåé¢å¼ï¼
é£ååé¢å¼æ¯é¶æå¤å表éå¼çåè¡¨ï¼æ¯å表éå¼ä»£è¡¨ä¸åæ¸çµå ç´ ï¼ä¸¦ç¨æ¹æ¬èï¼[]ï¼æ¬èµ·ä¾ã使ç¨é£ååé¢å¼åµå»ºé£åæï¼å°ä½¿ç¨æå®çå¼ä½çºå ¶å ç´ å°å ¶é²è¡åå§åï¼ä¸¦å°å ¶é·åº¦è¨ç½®çºæå®ç忏å¼ã
以ä¸ç¯ä¾åµå»ºäºé£å coffees
ï¼é·åº¦çº 3 並å
å«ä¸åå
ç´ ï¼
var coffees = ["French Roast", "Colombian", "Kona"];
åè¨»ï¼ An array literal is a type of object initializer. See Using Object Initializers.
If an array is created using a literal in a top-level script, JavaScript interprets the array each time it evaluates the expression containing the array literal. In addition, a literal used in a function is created each time the function is called.
Array literals are also Array
objects. See Array
and Indexed collections for details on Array
objects.
You do not have to specify all elements in an array literal. If you put two commas in a row, the array is created with undefined
for the unspecified elements. The following example creates the fish
array:
var fish = ["Lion", , "Angel"];
This array has two elements with values and one empty element (fish[0]
is "Lion", fish[1]
is undefined
, and fish[2]
is "Angel").
If you include a trailing comma at the end of the list of elements, the comma is ignored. In the following example, the length of the array is three. There is no myList[3]
. All other commas in the list indicate a new element.
åè¨»ï¼ Trailing commas can create errors in older browser versions and it is a best practice to remove them.
var myList = ["home", , "school"];
In the following example, the length of the array is four, and myList[0]
and myList[2]
are missing.
var myList = [, "home", , "school"];
In the following example, the length of the array is four, and myList[1]
and myList[3]
are missing. Only the last comma is ignored.
var myList = ["home", , "school", ,];
Understanding the behavior of extra commas is important to understanding JavaScript as a language, however when writing your own code: explicitly declaring the missing elements as undefined
will increase your code's clarity and maintainability.
叿å奿å
©ç¨®åé¢å¼ï¼ true
è· false
.
Do not confuse the primitive Boolean values true
and false
with the true and false values of the Boolean object. The Boolean object is a wrapper around the primitive Boolean data type. See Boolean
for more information.
æ´æ¸è½è¡¨ç¤ºçºãåé²å¶ãããåå é²å¶ãããå «é²å¶ãããäºé²å¶ã
æ´æ¸åé¢å¼ç¯ä¾å¦ä¸ï¼
0, 117 and -345 (decimal, base 10) 015, 0001 and -0o77 (octal, base 8) 0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16) 0b11, 0b0011 and -0b11 (binary, base 2)
æ´å¤è³è¨è«åé± Numeric literals in the Lexical grammar reference.
æµ®é»æ¸åé¢å¼ (Floating-point literals)æµ®é»æ¸åé¢å¼è½å å«ä»¥ä¸é¨åï¼
ææ¸é¨åç±ã"e" æ "E" å¾é¢è·æ´æ¸ãæçµæï¼å¯å¸¶ç¬¦è "+" æ "-" æ¼æ´æ¸åãæµ®é»æ¸åé¢å¼è³å°ç±ãä¸ä½æ¸åãèãä¸åå°æ¸é» "e" (æ "E")ãçµæã
ç°¡è¨ä¹ï¼æ¼æ³å¦ä¸ï¼
[(+|-)][digits][.digits][(E|e)[(+|-)]digits]
èåä¾åï¼
3.1415926 -.123456789 -3.1E+12 .1e-23ç©ä»¶åé¢å¼ (Object literals)
ç©ä»¶åé¢å¼æ¯ç¨å¤§æ¬èï¼{}ï¼æ¬èµ·ä¾çé¶æå¤å°éµå¼å°çå表ãå çº "{" å°è¢«è§£è¯çºåå¡(block)çéé ï¼å æ¤ä½ 䏿å¨é³è¿°å¥éé 使ç¨ç©ä»¶åé¢å¼ï¼éå°å°è´é¯èª¤æä¸é æçè¡çºã
以䏿¯ç©ä»¶åé¢å¼çç¯ä¾ãcar
ç©ä»¶å
å«ä¸å屬æ§ï¼
myCar
賦å¼çºå串 'Saturn
'getCar
賦å¼çºã調ç¨å½æ¸carTypes('Honda')
ãççµæspecial
使ç¨ç¾æè®é sales
賦å¼var sales = "Toyota";
function carTypes(name) {
if (name === "Honda") {
return name;
} else {
return "Sorry, we don't sell " + name + ".";
}
}
var car = { myCar: "Saturn", getCar: carTypes("Honda"), special: sales };
console.log(car.myCar); // Saturn
console.log(car.getCar); // Honda
console.log(car.special); // Toyota
æ¤å¤ï¼ä½ å¯ä»¥ä½¿ç¨æ¸åæå串åé¢å¼ä½çºå±¬æ§åï¼ä¹å¯å°ç©ä»¶åµå¥å¨å¦ä¸åç©ä»¶ä¸ãå¦ä¸ç¯ä¾ï¼
var car = { manyCars: { a: "Saab", b: "Jeep" }, 7: "Mazda" };
console.log(car.manyCars.b); // Jeep
console.log(car[7]); // Mazda
ç©ä»¶å±¬æ§åå¯ä»¥æ¯ä»»ä½å串ï¼å
æ¬ç©ºå串ã妿屬æ§å䏿¯ææç JavaScript èå¥å ææ¸åï¼åå¿
é å°å
¶ç¨å¼èå¼èµ·ä¾ãç¡æç屬æ§å稱ä¹ä¸è½ä½çºé» (.
) 屬æ§è¨ªåï¼ä½æ¯å¯ä»¥ä½¿ç¨é¡ä¼¼æ¸çµç符èï¼"[]
"ï¼é²è¡è¨ªååè¨ç½®ã
var unusualPropertyNames = {
'': 'An empty string',
'!': 'Bang!'
}
console.log(unusualPropertyNames.''); // SyntaxError: Unexpected string
console.log(unusualPropertyNames['']); // An empty string
console.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !
console.log(unusualPropertyNames['!']); // Bang!
Enhanced Object literals
In ES2015, object literals are extended to support setting the prototype at construction, shorthand for foo: foo
assignments, defining methods, making super calls, and computing property names with expressions. Together, these also bring object literals and class declarations closer together, and let object-based design benefit from some of the same conveniences.
var obj = {
// __proto__
__proto__: theProtoObj,
// Shorthand for âhandler: handlerâ
handler,
// Methods
toString() {
// Super calls
return "d " + super.toString();
},
// Computed (dynamic) property names
["prop_" + (() => 42)()]: 42,
};
Please note:
var foo = { a: "alpha", 2: "two" };
console.log(foo.a); // alpha
console.log(foo[2]); // two
//console.log(foo.2); // Error: missing ) after argument list
//console.log(foo[a]); // Error: a is not defined
console.log(foo["a"]); // alpha
console.log(foo["2"]); // two
æ£è¦è¡¨éå¼åé¢å¼ (RegExp literals)
æ£å表éå¼åé¢å¼æ¯å å«å¨ææ éçæ¨£å¼ã以䏿¯æ£å表é弿åçç¯ä¾ã
å串åé¢å¼ (String literals)å串åé¢å¼æ¯ç¨éå¼èï¼"ï¼æå®å¼èï¼'ï¼å ä½çé¶æå¤ååå ãåä¸²å¿ é ç¨åé¡çå¼èå®çï¼ä¹å°±æ¯ãå ©åå®å¼èãæãå ©åéå¼èãã以䏿¯å串åé¢å¼çç¯ä¾ï¼
"foo";
"bar";
"1234";
"one line \n another line";
"John's cat";
ä½ å¯ä»¥å¨å串åé¢å¼ä¸èª¿ç¨ String ç©ä»¶ç任使¹æ³ - JavaScript å°èªåè½æå串åé¢å¼çºè¨æ String ç©ä»¶ä¸¦èª¿ç¨è©²æ¹æ³ï¼ç¶å¾ä¸æ£è©²è¨æ String ç©ä»¶ãä½ éå¯ä»¥å° String.length 屬æ§èå串åé¢å¼ä¸èµ·ä½¿ç¨ï¼
console.log("John's cat".length);
// Will print the number of symbols in the string including whitespace.
// In this case, 10.
In ES2015, template literals are also available. Template literals are enclosed by the back-tick (` `) (grave accent) character instead of double or single quotes. Template strings provide syntactic sugar for constructing strings. This is similar to string interpolation features in Perl, Python and more. Optionally, a tag can be added to allow the string construction to be customized, avoiding injection attacks or constructing higher level data structures from string contents.
// Basic literal string creation
`In JavaScript '\n' is a line-feed.` // Multiline strings
`In JavaScript template strings can run
over multiple lines, but double and single
quoted strings cannot.`;
// String interpolation
var name = "Bob",
time = "today";
`Hello ${name}, how are you ${time}?`;
// Construct an HTTP request prefix is used to interpret the replacements and construction
POST`http://foo.org/bar?a=${a}&b=${b}
Content-Type: application/json
X-Credentials: ${credentials}
{ "foo": ${foo},
"bar": ${bar}}`(myOnReadyStateChangeHandler);
You should use string literals unless you specifically need to use a String object. See String
for details on String
objects.
é¤äºæ®éåå ï¼å串ä¹è½å å«ç¹æ®åå ï¼ç¯ä¾å¦ä¸ï¼
"one line \n another line";
ä¸è¡¨ååºäºå¯ä»¥å¨ JavaScript å串ä¸ä½¿ç¨çç¹æ®åå ã
åå ææ¶µ\0
Null Byte \b
éæ ¼ (Backspace) \f
Form feed \n
æè¡ (New line) \r
åè» (Carriage return) \t
è·³æ ¼ (Tab) \v
Vertical tab \'
Apostrophe or single quote \"
Double quote \\
Backslash character \XXX
The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol. \xXX
The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol. \uXXXX
The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. See Unicode escape sequences. \u{XXXXX}
Unicode code point escapes. For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04. Escaping characters
For characters not listed in the table, a preceding backslash is ignored, but this usage is deprecated and should be avoided.
You can insert a quotation mark inside a string by preceding it with a backslash. This is known as escaping the quotation mark. For example:
var quote = 'He read "The Cremation of Sam McGee" by R.W. Service.';
console.log(quote);
The result of this would be:
He read "The Cremation of Sam McGee" by R.W. Service.
To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path c:\temp
to a string, use the following:
You can also escape line breaks by preceding them with backslash. The backslash and line break are both removed from the value of the string.
var str =
"this string \
is broken \
across multiple \
lines.";
console.log(str); // this string is broken across multiplelines.
Although JavaScript does not have "heredoc" syntax, you can get close by adding a line break escape and an escaped line break at the end of each line:
var poem =
"Roses are red,\n\
Violets are blue.\n\
Sugar is sweet,\n\
and so is foo.";
ECMAScript 2015 introduces a new type of literal, namely template literals. This allows for many new features including multiline strings!
var poem = `Roses are red,
Violets are blue.
Sugar is sweet,
and so is foo.`;
More information
This chapter focuses on basic syntax for declarations and types. To learn more about JavaScript's language constructs, see also the following chapters in this guide:
In the next chapter, we will have a look at control flow constructs and error handling.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4