ç¨å¼èªè¨é½å ·æè³æçµæ§ï¼ä½å¨ä¸åçç¨å¼èªè¨ä¹éææäºå·®ç°ãé裡å°ååºå¯ä»¥å¨ JavaScript ä¸ä½¿ç¨çè³æçµæ§ï¼ä»¥åå®åçç¹æ§ï¼å®åå¯ä»¥ç¨ä¾æ§æå ¶ä»çè³æçµæ§ã妿å¯ä»¥çè©±ï¼ææ¨ç¤ºèå ¶ä»èªè¨ä¸åçå°æ¹ã
åæ åå¥JavaScript æ¯å¼±åå¥ï¼ä¹è½èªªæ¯åæ çç¨å¼èªè¨ãéä»£è¡¨ä½ ä¸å¿ ç¹å¥å®£åè®æ¸çåå¥ãç¨å¼å¨é使ï¼å奿èªåè½æãéä¹ä»£è¡¨ä½ å¯ä»¥ä»¥ä¸åçåå¥ä½¿ç¨åä¸åè®æ¸ã
var foo = 42; // foo ç®åæ¯æ¸å
var foo = "bar"; // foo ç®åæ¯å串
var foo = true; // foo ç®åæ¯å¸æå¼
è³æåå¥
ææ°ç ECMAScript æ¨æºå®ç¾©äºä¸ç¨®è³æåå¥ï¼
æå ç¨®è³æå奿¯åå§åå¥ï¼
å¦å¤éæ Object
é¤äºç©ä»¶ä»¥å¤çææå¼ï¼é½æ¯åå§å®ç¾©çå¼ï¼å¼æå³èä¸è½è¢«æ¹è®ï¼ãä¾å¦è C ä¸åçå°æ¹ï¼å°±æ¯å串æ¯ä¸è®çãæåå¼ç¨éäºé¡åçå¼çº primitive valuesï¼åå§å¼ï¼ã
叿åå¥å¸æï¼Booleanï¼ä»£è¡¨äºæå
©åå¼çé輯實é«ï¼true
è false
ã
Null åå¥åªæä¸åå¼ï¼null
ãè«åè¦ null
è Nullã
ä¸åæ²æè¢«å®ç¾©çè®æ¸æ undefined
å¼ãè«åè¦ undefined
è Undefinedã
æ ¹æ ECMAScript æ¨æºï¼æ¸ååå¥åªæä¸ç¨®ï¼é精度 64 ä½å
äºé²å¶æ ¼å¼ IEEE 754 å¼ï¼å¨ -(2^53 -1) and 2^53 -1 ä¹éçæ¸åï¼ãèæ´æ¸ä¸¦æ²ææå®çåå¥ãæ¸åé¤äºè½ä»£è¡¨æµ®é»æ¸ä»¥å¤ï¼éæä¸å符èå¼ï¼+Infinity
ã-Infinity
ãNaN
ï¼not-a-numberï¼éæ¸åï¼ã
è¦æª¢æ¥å¨ +/-Infinity
å
§å¯ç¨çæå¤§å¼ææå°å¼ï¼å¯ä»¥ç¨ Number.MAX_VALUE
æ Number.MIN_VALUE
常æ¸ãå¾ ECMAScript 6 éå§ï¼ä¹å¯ä»¥éé Number.isSafeInteger()
ãNumber.MAX_SAFE_INTEGER
ãNumber.MIN_SAFE_INTEGER
æª¢æ¥ææ¸åæ¯å¦çºé精度浮é»å¼ä¹éãBeyond this range, integers in JavaScript are not safe anymore and will be a double-precision floating point approximation of the value.
The number type has only one integer that has two representations: 0 is represented as -0 and +0. ("0" is an alias for +0). In the praxis, this has almost no impact. For example +0 === -0
is true
. However, you are able to notice this when you divide by zero:
> 42 / +0
Infinity
> 42 / -0
-Infinity
Although a number often represents only its value, JavaScript provides some binary operators. These can be used to represent several Boolean values within a single number using bit masking. However, this is usually considered a bad practice, since JavaScript offers other means to represent a set of Booleans (like an array of Booleans or an object with Boolean values assigned to named properties). Bit masking also tends to make code more difficult to read, understand, and maintain. It may be necessary to use such techniques in very constrained environments, like when trying to cope with the storage limitation of local storage or in extreme cases when each bit over the network counts. This technique should only be considered when it is the last measure that can be taken to optimize size.
å串åå¥JavaScript ç å串
åå¥ç¨ä¾ä»£è¡¨æåè³æã宿¯ä¸çµ 16 ä½çæªå®£åãå
ç´ ãå¼ãæ¯åå串çå
ç´ ï¼å¨å串å
§ç使ä¸ä½ã第ä¸åå
ç´ ä½æ¼ç´¢å¼ç第 0 ä½ï¼ä¸ä¸åå
ç´ ä½æ¼ç¬¬ 1 ä½ï¼ä¸¦ä¾æ¤é¡æ¨ãå串çé·åº¦ï¼æ¯æè©²å串æå¤å°å
ç´ ã
è C éé¡çèªè¨ä¸åï¼JavaScript å串æ¯ä¸è®çï¼æææ¯èªªç¶å串被åµé åºä¾ä»¥å¾ï¼ä½ ä¸å¯è½ä¿®æ¹å®ãä¸éï¼å¯ä»¥åºæ¼æä½åä¾çå串ï¼ä¾ç¢çæ°çå串ãä¾å¦ï¼
String.substr()
çåå§åå串ã+
ï¼æ String.concat()
飿¥å
©åå符串ãIt can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:
XMLHttpRequest
responses when using responseText
, etc.) and it can be tempting to only work with strings.With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.
Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.
Symbol typeSymbols are new to JavaScript in ECMAScript Edition 6. A Symbol is a unique and immutable primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called atoms. You can also compare them to named enumerations (enum) in C. For more details see Symbol and the Symbol
object wrapper in JavaScript.
以è³è¨ç§å¸èè¨ï¼ç©ä»¶æ¯åè½ééidentifieråç §çææ¸å¼è¨æ¶é«ã
PropertiesIn JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.
There are two types of object properties which have certain attributes: The data property and the accessor property.
Data propertyAssociates a key with a value and has the following attributes:
Attribute Type Description Default value [[Value]] Any JavaScript type The value retrieved by a get access of the property. undefined [[Writable]] Boolean Iffalse
, the property's [[Value]] can't be changed. false [[Enumerable]] Boolean If true
, the property will be enumerated in for...in loops. See also Enumerability and ownership of properties false [[Configurable]] Boolean If false
, the property can't be deleted and attributes other than [[Value]] and [[Writable]] can't be changed. false Attribute Type Description Read-only Boolean Reversed state of the ES5 [[Writable]] attribute. DontEnum Boolean Reversed state of the ES5 [[Enumerable]] attribute. DontDelete Boolean Reversed state of the ES5 [[Configurable]] attribute. Accessor property
Associates a key with one or two accessor functions (get and set) to retrieve or store a value and has the following attributes:
Attribute Type Description Default value [[Get]] Function object or undefined The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed. See alsoget
. undefined [[Set]] Function object or undefined The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed. See also set
. undefined [[Enumerable]] Boolean If true
, the property will be enumerated in for...in loops. false [[Configurable]] Boolean If false
, the property can't be deleted and can't be changed to a data property. false
åè¨»ï¼ Attribute is usually used by JavaScript engine, so you can't directly access it(see more about Object.defineProperty()).That's why the attribute is put in double square brackets instead of single.
"Normal" objects, and functionsA JavaScript object is a mapping between keys and values. Keys are strings (or Symbol
s) and values can be anything. This makes objects a natural fit for hashmaps.
Functions are regular objects with the additional capability of being callable.
DatesWhen representing dates, the best choice is to use the built-in Date
utility in JavaScript.
Arrays are regular objects for which there is a particular relationship between integer-keyed properties and the length
property.
Additionally, arrays inherit from Array.prototype
, which provides a handful of convenient methods to manipulate arrays. For example, indexOf()
searches a value in the array and push()
appends an element to the array. This makes Arrays a perfect candidate to represent ordered lists.
Typed Arrays present an array-like view of an underlying binary data buffer, and offer many methods that have similar semantics to the array counterparts. "Typed array" is an umbrella term for a range of data structures, including Int8Array
, Float32Array
, etc. Check the typed array page for more information. Typed arrays are often used in conjunction with ArrayBuffer
and DataView
.
These data structures take object references as keys and are introduced in ECMAScript Edition 6. Set
and WeakSet
represent a set of objects, while Map
and WeakMap
associate a value to an object. The difference between Maps and WeakMaps is that in the former, object keys can be enumerated over. This allows garbage collection optimizations in the latter case.
One could implement Maps and Sets in pure ECMAScript 5. However, since objects cannot be compared (in the sense of "less than" for instance), look-up performance would necessarily be linear. Native implementations of them (including WeakMaps) can have look-up performance that is approximately logarithmic to constant time.
Usually, to bind data to a DOM node, one could set properties directly on the object or use data-*
attributes. This has the downside that the data is available to any script running in the same context. Maps and WeakMaps make it easy to privately bind data to an object.
JSON (JavaScript Object Notation) is a lightweight data-interchange format, derived from JavaScript but used by many programming languages. JSON builds universal data structures. See JSON and JSON
for more details.
JavaScript has a standard library of built-in objects. Please have a look at the reference to find out about more objects.
使ç¨typeof
éç®åä¾å¤æ·åå¥
typeof
éç®åå¯ä»¥å¹«å©ä½ æ¾å°ä½ çè®æ¸åå¥ï¼è«é±è®ãreference page ãä¾å徿´å¤ç´°ç¯åéç·£æ¡ä¾ã
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