`

JavaScript ---- Type Conversions

 
阅读更多
JavaScript is very flexible about the types of values it requires.
When JavaScript expects a boolean value, you may supply a value of any type, and JavaScript will convert it as needed.
If JavaScript wants a string, it will convert whatever value you give it to a string.
If JavaScript wants a number, it will try to convert the value you give it to a number(or to NaN if it cannot perform a meaningful conversion).
例如:
     10 + " objects"         // => "10 objects". Number 10 converts to a string
     "7" * "4"               // => 28: both strings convert to numbers
     var n = 1 - "x";        // => NaN: string "x" can't convert to a number
     n + " objects"          // => "NaN objects": NaN converts to string "NaN"

显式转换:
    显式转换最简单的方式是使用:Boolean(), Number(), String(), or Object() functions.
    例如:
         Number("3")                 // => 3
         String(false)               // => "false" or use false.toString()
         Boolean([])                 // => true
         Object(3)                   // => new Number(3)

The toString() method defined by the Number class accepts an optional argument that specifies a radix, or base, for the conversion. If you do not specify the argument, the  conversion is done in base 10. However, you can also convert numbers in other bases (between 2 and 36).
例如:
     var n = 17;
     binary_string = n.toString(2);        // Evaluates to "10001"
     octal_string = "0" + n.toString(8);   // Evaluates to "021"
     hex_string = "0x" + n.toString(16);   // Evaluates to "0x11"

数值处理:
    Number(): If you pass a string to the Number() conversion function, it attempts to parse that string as an integer or floating-point literal. That function only works for base-10 integers, and does not allow trailing characters that are not part of the literal.
    parseInt():
    parseFloat(): The parseInt() and parseFloat() functions (these are global functions, not methods of any class) are more flexible.
    parseInt() parses only integers
    parseFloat() prases both integers and floating-point numbers
    If a string begins with "0x" or "0X", parseInt() interprets it as a hexadecimal number. Both parseInt() and parseFloat() skip leading whitespace, parse as many numeric characters as they can, and ignore anything that follows.If the first nonspace character is not part of a valid numeric literal, they return NaN:
    例如:
       
        parseInt("3 blind mice")       // => 3
        parseFloat(" 3.14 meters")     // => 3.14
        parseInt("-12.34")             // => -12
        parseInt("0xFF")               // => 255
        parseFloat(".1")               // => 0.1
        parseInt("0.1")                // => 0
        parseInt(".1")                 // => NaN: integers can't start with "."
        parseFloat("$72.47");          // => NaN: numbers can't start with "$"
        

parseInt() accepts an optional second argument specifying the radix (base) of the number to be parsed.Legal values are between 2 and 36. For example:
       
        parseInt("11", 2);             // => 3 (1*2 + 1)
        parseInt("ff", 16);            // => 255 (15*16 + 15)
        parseInt("zz", 36);            // => 1295 (35 * 36 + 35)
        parseInt("077", 8);            // => 63 (7*8 + 7)
        parseInt("077", 10);           // => 77 (7*10 + 7)
        


将一个对象转换为一个string:
     1. 首先查找对象是否有一个toString()方法,如果有,则调用他,如果没有或者没有返回一个原始值,则 =>
     2. 查找对象是否有一个valueOf()方法,如果有,则调用,如果没有或者没有返回一个原始类型,则=>
     3. 抛出TypeError

将一个对象转换为number:
     是同样的3个步骤,但是首先查找valueOf()方法,其次才是toString()

Arrays:
     Arrays inherit the default valueOf() method that returns an object rather than a  primitive value, so array-to-number conversion relies on the toString() method.
分享到:
评论

相关推荐

    javascript权威指南(第六版)

    3.8 Type Conversions 45 3.9 Variable Declaration 52 3.10 Variable Scope 53 4. Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57...

    bootcamp-js

    现代JavaScript教程 PART01-JAVASCRIPT基础知识01 - HELLO WORLD02 - CODE STRUCTURE03 - THE MODERN MODE, 'USE STRICT'04 - VARIABLES05 - DATA TYPES06 - INTERACTION07 - TYPE CONVERSIONS08 - BASIC OPERATORS ...

    程序语言设计原理习题解答

    7.4 Type Conversions 323 History Note 324 7.5 Relational and Boolean Expressions 326 History Note 326 7.6 Short-Circuit Evaluation 329 7.7 Assignment Statements 330 History Note 333 7.8 ...

    python3.6.5参考手册 chm

    The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...

    PHP and MYSQL Bilbe [英文原版]

    Chapter 25: Types and Type Conversions 479 Chapter 26: Advanced Use of Functions489 Chapter 27: Mathematics 501 Chapter 28: PEAR 517Chapter 29: Security 531 Chapter 30: Configuration 555 Chapter 31: ...

    Beginning Microsoft Visual CSharp 2008 Wiley Publishing(english)

    JavaScript 738 Summary 744 Exercises 745 Chapter 21: Web Services 746 Before Web Services 747 Where to Use Web Services 748 Web Services Architecture 751 Web Services and the ...

Global site tag (gtag.js) - Google Analytics