zoukankan      html  css  js  c++  java
  • Chapter 2 JavaScript Basic

    Has 5 primitive types: Undefined, Null, Boolean, String, Number.

    typeof  operator

    Undefined return undefined

    Null return object

    Boolean return boolean

    String return string

    Number return number

    Null returns object, this is an error in the original JS. Today, null is considered a placeholder for an object,  even though, it is a primitive types.

    The Undefined Type

    Use typeof with Object undefined, it also give "undefined", like the variables defined but not initialized.

    The Null Type

    alert(null == undefined);  //outputs "true"

    Even though this is equal, they have different meanings.

    The Boolean Type

    true false

    The Number Type

    Number can present 32-bit integer and 64-bit floating point value.

    Define floating-point value, you should use like: var fNum = 5.0; This will stored as string, and converted to number until calculation.

    e-notion:  var fNum = 3.125e7, fNum2=3.125e-7;

    Number.MAX_VALUE  Number.MIN_VALUE

    >Number.MAX_VALUE  ->   Number.POSITIVE_INFINITY

    <Number.MIN_VALUE   ->   Number.NEGATIVE_INFINITY

    Function isFinite(value) returns Boolean

    NaN: When convert from other type and failed, the number value is NaN, can't calculate.

    alert(NaN == NaN);  //outputs "false"

    Function isNaN(value) returns Boolean

    The String Type

    ''

    ""

    Conversions

    Converting to a string

    Booleans, numbers, strings are pseudo-objects.

    All objects, include   pseudo-objects, all have toString().

    Boolean:   true  false

    Number: 

    var num = 10;

    alert(num.toString());   //10

    alert(num.toString(2)); // 1010

    alert(num.toString(16)); // A

    Converting to a number

    parseInt():  validate the character in position 0 and determines if this is a valid number, if it isn't , return NaN, else continue untile a character isn't a valid number, and parse the part of valid.

    var iNum1 = parseInt("123blue"); //returns 123

    var iNum1 = parseInt("22.5"); //returns 22, decimal point is not valid

    var iNum2 = parseInt("10", 2) ;//returns 2

    var iNum3 = parseInt("0XA");//returns 10

    var iNum4 = parseInt("010");//returns 8

    var iNum5 = parseInt("010", 10);//returns 10

    parseFloat(): if have more than one decimal point, all are invalid except the first one. No radix mode.

    var iNum1 = parseFloat("12.34.56"); //returns 12.34

    var iNum2 = parseInt("123blue"); //returns 123.0

    var iNum3 = parseInt("010");//returns 10

    var iNum4 = parseInt("0XA");//returns NaN

    Type Casting

    Boolean(value)

    String with at least one character, a number other than 0, or an object: return true;

    Empty string, the number 0, undefined or null, return false.

    Number(value)

    Works similar to parseInt() & parseFloat(), except that it converts the entire value.

    String(value)

    Like toString(), but can produce a string for a null or undefined value without error.

    var s1 = String(null);   //"null"

    var oNull = null;

    var s2 = oNull.toString();  //won't work

    Reference Types

    ECMAScript doesn't have classes in the traditional sense. ECMAScript defines "object definitions" that are logically equivalent to classes in other programming languages.

    The Object class

    Like the java.lang.Object in java.

    Properties

    constructor

    prototype

    Methods

    hasOwnProperty(property)

    isPrototypeof(object)

    toString()

    valueOf()

    The Boolean Class

    var oFalseObject = new Boolean(false);

    Var bResult = oFalseObject  && true;    //outputs true

    Because all objects converted to true.

    The Number class

    toFixed(), toFixed(n): returns a string representation of a number with a specified number of decimal points.      0 <= n <= 20

    toExponential(n): returns a string with the number formatted in e-notation.

    toPrecision():

    var oNumberObject = new Number(99);

    alert(oNumberObject .toPrecision(1));  //outputs "1e+2"

    alert(oNumberObject .toPrecision(2));  //outputs "99"

    alert(oNumberObject .toPrecision(3));  //outputs "99.0"

    The String class

    The String class has length property.

    charAt(): returns a string containing the character in that position.

    charCodeAt():

  • 相关阅读:
    平方和公式
    $bootpuss$切不掉的「水题」
    回滚莫队初步
    [***]HZOJ 柱状图
    HZOJ 走格子
    HZOJ 旋转子段
    [***]HZOJ 优美序列
    [***]HZOJ 跳房子
    HZOJ 矩阵游戏
    模板—K-D-tree(P2479 [SDOI2010]捉迷藏)
  • 原文地址:https://www.cnblogs.com/fxb248/p/3574399.html
Copyright © 2011-2022 走看看