Learning a programming language is nothing but two aspects: one is grammar, the other is data type. The syntax of Class C language is nothing more than if, while, for, function, arithmetic, and so on, and object-oriented language plus object.
Grammar is just a set of rules that language designers do in advance, different language grammar is not the same, but there are some common points, for those familiar with the one or two programming languages, when learning other programming languages, grammar is often not a problem (of course, if you have been learning C language, the first contact with Lisp will certainly take some time), The focus of learning is often on data types and related operations, not an old saying: "Data structure + algorithm = program"! Second, some language syntax itself has a design problem (JavaScript more), we do not need to delve into these points, of course, if you boast geek, you can play.
This article gives a detailed description of the data types in JavaScript.
Weak type vs Strong type
Given JavaScript's design philosophy, JavaScript is designed as a weak-type language.
Speaking of which, it is inevitable to say that the difference between a weak type and a strong type.
Some people mistakenly assume that the difference between the two is that "a strongly typed language needs to indicate its type when declaring a variable, while a weak type does not." In fact, this view is wrong. For example, the following Java code fragment:
Copy Code code as follows:
String s = "Hello";
int L = s.getbytes (). length;
How does the compiler know that. length is a valid expression? This is because the compiler knows that the data type of S is string, and when the GetBytes method of string is invoked, the data type of the return value is byte[], so. length is a valid expression.
The real difference between the two is:
In strongly typed languages, the types of each expression can be determined at compile time, and only those operations that are applicable to that type are allowed;
A weakly typed language allows any action to be exerted on any type, except that the operation may be wrong in the run-time times.
Data type
According to the ECMAScript 5.1 specification, JavaScript has six types of data, namely: Undefined, Null, Boolean, number, String, Object. The first five types belong to the basic type, and the last one belongs to the object type.
Basic data types
The undefined type has only one value, which is undefined, meaning "null value (no value)", which applies to all data types.
A null type has only one value and is null, meaning "null object (No object)" and applies only to object types.
Boolean type has two values, true and False
The number type value is a collection of 64-bit floating-point numbers that follow the IEEE 754 standard, similar to the Java double. There is no integral data structure. In addition, three special values are included: NaN, Infinity,-infinity
A string type value is a collection of poor Unicode characters. must be enclosed in ' or '.
null and undefined
Null and undefined both represent the concept of "no value (non-value)" If Strictly differentiated:
-NULL indicates NULL
-undefined indicates that it does not exist. Variables that are not initialized, missing arguments in a function, and functions that do not have an explicit return value for this value
In other languages, a null value is generally used to represent nulls, and why is there a undefined in javascript? This is due to historical reasons:
JavaScript takes Java syntax, divides types into basic types and object types, Java uses NULL to represent empty objects, and JavaScript inherits it for granted; in C, NULL is 0 when converted to a number, JavaScript also takes the same approach:
Copy Code code as follows:
> number (NULL)
0
> 5 + NULL
5
In javascript1.0, there is no exception handling (exception handling), for some exceptions (uninitialized variables, missing parameters when calling functions, etc.) need to be marked as a special value, NULL would have been a good choice, but Brendan Eich also want to avoid the following two things:
-This particular value should not have a reference attribute, because it is the object of special
-This special value should not be converted to 0 because it is not easy to find bugs in the program
Based on these two reasons, Brendan Eich chose undefined, which can be strongly converted to Nan.
Copy Code code as follows:
> Number (undefined)
NaN
> 5 + undefined
NaN
When the JSON object is dealt with, the results are very different:
Copy Code code as follows:
> json.parse (NULL)
Null
> json.parse (undefined)
Firfox SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data
Chrome syntaxerror:unexpected token u
> json.stringify (NULL)
"NULL"
> json.stringify (undefined)
Undefined
Object type
JavaScript, as a scripting language, has a very concise function, and many functions (file read/write, network, etc.) are provided by the hosting environment. The host environment is a bridge to the JavaScript language, and the hosting environment provides a variety of features by providing a series of objects that conform to JavaScript syntax.
In this article on JavaScript object-oriented programming (if you don't know what prototype is, strongly recommend looking at this article), I've repeatedly emphasized that objects in JavaScript are a series of key-value pairs, just like HashMap in Java, but The properties of objects in JavaScript can have descriptors (property descriptor), which are not available in HashMap.
Property descriptor
Property descriptors are grouped into two categories:
Data descriptor (descriptor) that contains a series of Boolean values that indicate whether the property allows modification or deletion.
Access descriptor (accessor descriptor) that contains the get and set functions.
Both of these descriptors are objects, and they all have the following two Boolean properties:
Configurable is used to specify whether the descriptor allows modification and deletion. The default is False.
Enumerable is used to specify whether to access the property when traversing an object (using the for...in loop or the Object.keys method). The default is False.
In addition to the two common properties above, the data descriptor has the following two properties:
-value is used to specify the value of the property, which defaults to undefined
-writable Specifies whether the value of this property is allowed to change the value of the property, which defaults to False
The access descriptor also has the following two properties:
-Get is used to specify the accessor (getter, essentially a function) to access the property, and the return value of the accessor is the value of the property. Default is undefined
-set Specifies the assignment (setter, which is essentially a function) to access the property, which accepts an argument. Default is undefined
We can use Object.defineproperty to set the object's property descriptor. For example:
Copy Code code as follows:
Using __proto__
Object.defineproperty (obj, ' key ', {
__PROTO__: null,//No inherited properties
Value: ' Static '//Not enumerable
Not configurable
Not writable
As defaults
});
As we can see from the above example, the descriptor has inherited characteristics, and we explicitly set the Descriptor object's __proto__ to null to avoid inheriting the corresponding property from Object.prototype. Of course, we can also explicitly set all the properties of the descriptor:
Copy Code code as follows:
Being explicit
Object.defineproperty (obj, ' key ', {
Enumerable:false,
Configurable:false,
Writable:false,
Value: ' Static '
});
This effect is the same as the first piece of code.
Here's an example of an access descriptor:
Copy Code code as follows:
Example of an object property added and DefineProperty with a accessor property descriptor
var bvalue = 38;
Object.defineproperty (obj, ' key ', {
Get:function () {return bvalue;},
Set:function (newvalue) {bvalue = newvalue;},
Enumerable:true,
Configurable:true
});
It should be noted that the access descriptor and the data descriptor cannot be confused. The following is the wrong way to write:
Copy Code code as follows:
Cannot try to mix both:
Object.defineproperty (obj, ' conflict ', {
value:0x9f91102,
Get:function () {return 0xdeadbeef;}
});
Throws a typeerror:property descriptors must not specify a value
Or is writable when a getter or setter has been specified
typeof
If you want to learn the type of a variable at run time, you can use the TypeOf operator. The return value of the TypeOf is the following table:
One of the things to note is that typeof null = "Object", according to the ECMAScript 5.1 standard, the null type should be a basic type, why return object here? The reason is this:
In JavaScript 1.0, the value in JavaScript is represented by a struct of type tag and an actual value, and the object's type flag is 0,null in C to represent a null pointer (0x00), so the type flag for null is 0.
The above is the entire content of this article, there is a need for small partners under the reference bar.