The basic concept of re-learn js JavaScript (on) => Data Types

Note: This article is the first part of the third series chapter [Data type] "re-learn the JavaScript js advanced programming."
About "re-learn the JavaScript js advanced programming" is to revisit js-based learning.

Premise: the core of any language will describe the basic principle of the language, content is often described involves the basic concepts of the language syntax, operators, data types, such as built-in functions for building complex solutions.

1. Grammar

1、js 中的变量、函数名和操作符都区分大小写,
2、另外一些像typeof这些关键字也不能使用
复制代码

2. Identifier

The so-called identifier refers to the name of variables, functions, properties, or the parameter of the function.

标识符的写法标准如下:

1、第一个字符必须是一个字母、下划线或者一个美元符号
2、其他字符可以是字母、下划线、美元符号或数字
3、标识符中的字母采用驼峰大小写格式,例如:firstSecond、myCar
复制代码

3. Comment

js annotations include single line comment block comment, and two single-line comments beginning backslashes block comment is based on (/ ) starting with ( /) End

单行注释

// 注释

块级注释

/*
    块级注释
*/
复制代码

4. Statement

Js statement is nothing in the end but not required, but recommended or write the semicolon. This unnecessary because the problem does not occur when the compression code.

5. Keywords and reserved words

There are specific keywords js, these keywords may be used to indicate the start or end control statements, or to perform a specific operation or the like, the language keyword is specially reserved, it can not be used as identifiers. In addition there are some other js reserved words, it can not be used as identifiers.

6. variables

Because loose js variable type, the type that can be used to store loose any type of data. Each variable is just a placeholder used to hold the value of it, the definition of variables need to use var, and adds let the const keyword after the ES6 to define variables.

var a = 10 // 定义一个变量a 给它赋值 10
复制代码

Note: Although not define global var operator can be variable, but it is not recommended because of the local variables defined in the global scope that will be difficult to maintain the code.

7. Data Type

There are five basic types of data in the js: Undefined, Null, Boolean, Number and String, complex type Object. Object is a group of unordered name-value pairs. Create custom any type of mechanism is not supported in the ES.

8. typeof operator

For detecting the data type of a given variable, the return value is detected as follows:

undefined -- 值未定义
Boolean -- 布尔值
string -- 字符串
number -- 数值
object -- 对象或者null
function -- 函数
复制代码

Operands typeof operator may be variable, may be a literal value, typeof is an operator rather than a function.

9. undefined

It is only a special type of value that is undefined, if the variable is not assigned after var, then the value of this variable is undefined.

var message;
message // undefined
复制代码

Note: uninitialized variables and variables are unassigned after typeof undefined, so in order to better determine the source of undefined in the end is unassigned or not initialized, the statement recommended that all values ​​are in use.

10. null type

only null data type is a second value, is null, the face of it is an empty or null object pointer, and this reason is really detected or null value typeof "object" of.

If you want to define a variable to hold the object, then the best approach is to detect such null null variable assignment will know whether the corresponding variable holds a reference to the object.

Note: null == undefined is true, although such use is not the same of both. Whether all without any need to explicitly set the value of a variable is undefined, and is null if the variable that holds the object did not really save the object , then it should be clear so that variable holds a null value. This further distinction null and undefined.

11. Boolean type

boolean word denomination types are: ture and false, and a further two values ​​are not the same digital value, ture is not necessarily equal to 1, false not necessarily equal to zero.

Note: true and false are case-sensitive, True and Flase just identifiers.

We can function ** Boolean () ** converts a Boolean type value. as follows:

var message = 'haha,nihao';
var zh = Boolean(message)
复制代码

Various types of conversion rules are as follows:

type of data Converted to true values Converted to a value of false
Boolean true false
String Any non-null string "" (An empty string)
Number Any nonzero numeric value (including infinite) 0 sum NaN
Object Any object null
Undefined Not applicable undefined

12. number Type

Data type widely used in the js, integers and floating point values. It can be represented by a decimal, octal, hexadecimal literal.

var bjz = 070   // 八进制 56
var sjz = 56    // 十进制 56
var sljz = 0xA  // 十六进制 10
复制代码

12.1 floating-point values

Is, the value must contain a decimal point, and at least one digit after the decimal point.

var f = 1.1
复制代码

Note : Due to save memory space is saved floating point double integer, so the following will be automatically converted to floating-point integer saved as follows:

var f = 1.  // 小数点后没有数字 保存为 1
var b = 10.0 // 保存为 10
复制代码

In addition to too large or too small value may be represented by e (scientific notation), as follows:

var f = 3.125e7   // 3.125 * 10^7 => 31250000
var f = 3e-17     // js 会将小数点后面超过6个0的浮点数值转换为科学计数法
复制代码

Note : 0.1 + 0.2! = 0.3

Numerical range 12.2

Js minimum due to limitations of memory and can not store all values, js can be represented as 5e-324, the maximum value is 1.7976931348623157e + 308. If the calculated value exceeds a particular maximum is converted to Infinity, minimum -Infinity. If the two values ​​appear, then the value will not be in operation after participation.

12.3 NaN

Js is non-numeric, is a special value. Mainly to prevent throw an error, it has been used to indicate a case where the return value of the operand value is not returned.

There are two specific NaN, 1, any involving a NaN operations return NaN, 2, NaN not equal to any value including itself. For these two characteristics, ES defines the isNaN () function, which accepts one parameter, which may be of any type, and the function will help us determine whether this parameter is "not a number", isNaN () attempts after accepting parameters convert this value to value, not the value of some value directly into the value. as follows:

isNaN(NaN)      // true
isNaN(10)       // false (10是一个数值)
isNaN("10")     // false (可以被转换成数值10)
isNaN('blue')   // true (不能转换成数值)
isNaN(true)     // flase (可以被转换成数值 1)
复制代码

Note: isNaN () can also be used for the object, based on the object when invoked, calls the first valueOf () method, the method then determines whether the return value can be converted to the value, if the return value is not based on calling toString ( ) method returns the value in the test.

12.4 Number Converter

There are three functions can be converted to a non-numeric values: Number (), parseInt (), parseFloat (), the first function may be used for any type of data. The other two functions are used to convert a string value.

Number()转换规则如下:
    Boolean值  => true or flase => 1 or 0
    number => 传入和返回
    null => 返回 0 
    undefined => 返回null
    字符串
        如果包含十进制数值 则 '123' => 123 ,'012' => 12
        如果是浮点字符串 则 '1.1' => 1.1 , '01.1' => 1.1
        如果包含十六进制 则 '0xf' => 相同的十进制
        如果为空 则 '' => 0
        如果包含除了上述格式 则 '' => NaN
        如果是对象,则调用对象的 valueOf()方法,在按照之前的规则转换返回值,
    如果转换对象是NaN,则调用对象的 toString() 方法,在按照之前的规则返回字符串
    值。

parseInt()转换规则如下:
    它会忽略字符串前面的空格,如果第一个字符不是数字或者负号则返回NaN。
    如果是其他进制,则转换为响应的十进制数值。
    
    var n = parseInt('1234blue')    => 1234
    var m = parseInt('')            => NaN
    
parseFloat()转换规则如下:
    从第一个字符开始解析每个字符,知道遇到第一个无效的浮点数字字符为止
    也就是说字符串中第一个小数点是有效的,第二个小数点就无效的了。因此
    它后面的字符串就被忽略了。
    
    var n = parseFloat('1234blue')    => 1234
    var m = parseFloat('0xA')         => 0
    var c = parseFloat('22.21.2')     => 22.21
    var b = parseFloat('22.4')        => 22.4
复制代码

13. string type

By a sequence of characters used to represent zero or a plurality of 16-bit Unicode characters, i.e., the string. By the double quotation marks ( "") or single quotation marks ( '') from the packages

var a = '124'
var b = "123"

// 注意:双引号开头必须双引号结尾!
复制代码

13.1 character literals

Some special literal characters that escape sequences. It represents a non-printing characters, or characters having other uses.

Literals meaning
\n Wrap
\t tabulation
\b Blank
\r Enter
\f Feed
\ Slash
' apostrophe
" Double quotes

These characters can appear anywhere in a string of red, and will be treated as a character to parse.

The string can be of any length by length to obtain attribute.

let b = '1kljk'
b.length // 5
复制代码

13.2 string features

In ES strings are immutable. Once the string is created, its value can not be changed. If you want to change the value of a string is required the destruction of the original value, the re-assignment

var lang = 'shanghai'
lang = lang + 'daxue'

// shanghaidaxue
复制代码

13.3 is converted to a string

If you want to convert a string value in two ways. I. toString (), as follows:

var age = 11;
var agestring = age.toString() // '11'
var b = true
var bstring = b.toString() // 'true'
复制代码

Most data type (numeric, boolean, string, and the object value) has ** toString ** method, but not null and undefined. In most cases toString is not need to pass parameters. The default value is returned in decimal format string representation. But the fact is you can pass octal, binary, hexadecimal parameters.

var num = 10;
num.toString()      // '10'
num.toString(2)     // '1010'
num.toString(8)     // '12'
num.toString(10)    // '10'
num.toString(16)    // 'a'
复制代码

14. object type

The object is a collection of data and functionality. Objects can be created by the implementation of the object type the name of the new operator followed to create. Examples of the type of object created, you can add its properties and methods.

var o = new Object()
复制代码

Object type in the ES is the basis for all of its instances, any of the properties and methods of type Object has also present in a more specific object.

Each Object instance has the following attributes and methods:

constructor => Save function is used to create the current object

hasOwnProperty (propertyName) => used to check whether a given attribute exists in the current object instance. propertyName must be specified as a string.

isPrototypeOf (Object) => used to check whether the object is passed to another object prototype.

propertyIsEnumerable (propertyName) => to check whether the attribute is used for-in statement can be used to enumerate, and as hasOwnProperty () method, as the property name must be specified in the parameter string.

toString () => Returns a string representation of the object

valueOf () => Returns a string object, or Boolean values represented. ·

I welcome the attention of the public tender small number [students]

Guess you like

Origin blog.csdn.net/weixin_34066347/article/details/91363114