Reading notes - "JavaScript Advanced Programming" - Chapter 3 Data Types

3.3 Variable

ECMAScript variables are loose type is the so-called bulk type can be used to store any type of data.

3.4 Data Types

Since the variable is loosely typed ECMAScript can be used to detect a data type typeof given variable. typeof is an operator rather than a function.

  • undefined
  • Boolean
  • string
  • number
  • object (null is returned this)
  • function

A null value indicates a null pointer, undefined value is derived from a null value

console.log(null == undefined ) \\true

console.log(null === undefined ) \\false

It can be used Boolean()to convert a Boolean value corresponding to

3.4.5 Number Type

  • Octal: The first must be 0 and the other is 0-7;
  • Hex: two digits are 0X (0x), or behind 0-9 AF (available lowercase)

In octal, if the literal value exceeds the range, in front of 0 is ignored, as a decimal value behind the resolution.

var Num = 019 \\ 19

Save floating-point values ​​required memory space is saved twice the integer value

Infinity represents infinity, + Infinity plus infinity, -Infinity negative infinity

If a particular calculation returns Infinity, then the value can not continue to participate in the next calculation. Use isFinite()based on a numerical value is not infinitely

0 divided by 0 is equal to NaN

console.log(0/0) // NaN
console.log(1/0) // Infinity
console.log(-1/0) // -Infinity
复制代码

NaN == NaN //false

isNaN()May determine whether the 'value is not', it will try to convert the argument passed to numerical values, may be converted, returns false, can not be converted, returns true

console.log(isNaN(NaN))  // true
console.log(isNaN(10))  // false
console.log(isNaN("10"))  // false
console.log(isNaN("blue")) // true
console.log(isNaN(true)) // false,可以被转换成数值1
复制代码

Number () can be converted to any data type value

Conversion rules:

  • If Boolean, true and false returns 1 and 0, respectively;
  • If it is digital, the digital return
  • If it is null, it returns 0,
  • If it is undefined, NaN is returned
  • If a string
    • If the string contains only numeric values, converted into decimal
    • If the string contains a valid floating-point, floating-point values ​​into corresponding
    • If the string contains a valid hexadecimal format, converted to a decimal value of the same size
    • String is empty, returns 0
    • If included, among other formats is the outer returns NaN
  • If is an object, call the object's valueOf()methods, and in accordance with the foregoing conversion rules. If the result of the conversion is NaN, the object is invoked the toString()method, then in accordance with the foregoing conversion rules

Since Number () function converts the string while complex and not reasonable, and therefore in the process of the integer time more commonlyparseInt()

parseInt()

  • We ignore leading spaces, until you find the first non-space character
  • If the first character is not a number or negative number, returns NaN
  • If the first character is a number, we will continue to resolve the next character until resolved or complete non-numeric characters encountered
  • Recognize decimal, octal, hexadecimal
console.log(parseInt())  // NaN
console.log(parseInt("1234blue"))   //1234
console.log(parseInt("0xA"))  //10,十六进制
console.log(parseInt("22.5")) //22,小数点不是有效的数字字符
console.log(parseInt("070"))  //56,八进制
复制代码

Because ECMAScript3 JavaScript engine, "070" is treated as an octal literal, and in ECMAScript5 JavaScript engine, parseInt no longer have the ability to parse octal, "070" is converted into 70. Thus, the parseInt cardinality second parameter, use

console.log(parseInt("AF"))  //NaN
console.log(parseInt("AF",16)) //175
console.log(parseInt("070",8))  //56
console.log(parseInt("070"))  //70
复制代码

parseFloat()

  • Only the decimal resolved, there is no second argument
  • Hexadecimal string will return 0
  • We ignore leading zeros
console.log(parseFloat("1234blue"))  //1234
console.log(parseFloat("0xA"))  // 0
console.log(parseFloat("22.5")) // 22.5
console.log(parseFloat("22.34.5")) //22.34
console.log(parseFloat("0980.5"))  //980.5
console.log(parseFloat("3.125e7"))  //31250000
复制代码

3.4.6 string type

toString()The method can convert a value into a string, null, and this method is not undefined, which parameters can be passed, the output value of the radix (binary few)

Without knowing the value to be converted is not null and undefined, the transition function may be used String(), any type able to convert the value into a string

  • If the values ​​are toSting () method, this method is called (without parameters) to return the result
  • If so null, returns "null", if so undefined, returns "undefined"

3.4.7 Object Types

Object Each instance has the following attributes and methods

  • constructor
  • hasOwnProperty(propertyName)
  • isPrototypeOf (object): Object is used to check whether incoming incoming object prototype
function object1() {}
const object3 = new object1();
console.log(object1.prototype.isPrototypeOf(object3));
// expected output: true
复制代码
  • propertyIsEnumerable (propertyName): for checking whether a given attribute may be used for-in statement based enumeration
  • toLocalString (): used to date objects with returning to the region or specify parameters according to the local date format
  • toString()
  • valueOf () Returns the object's original value var timestamp = (new Date()).valueOf() //返回时间戳

Reproduced in: https: //juejin.im/post/5cf2256df265da1bbd4b5ca4

Guess you like

Origin blog.csdn.net/weixin_34247032/article/details/91469855