javaScript六大数据类型

JavaScript数据类型

    基本数据类型:Undefined,Null,Boolean,Number,String。

    复杂数据类型:Object。

 

    1.Undefined

    这个类型只有一个值是undefined,使用var声明了 但是没有赋值的变量

 

var message;
alert(message == undefined) // true
typeof message // "undefined"

 

 

    2.Null 

    这个类型只有一个值是null,标示一个空的对象指针。

var message = null;
typeof message; // "null"

 

    3.Boolean

    这个类型的值有两个 true 和 false 是区分大小写的

 

// 转换为true的值
var value = true;
var value2 = "hello world"; // 任何非空字符串
var value3 = 1; // 任何非零数字
var value4 = {}; // 任何对象 包括function,Object

if (value & value2 & value3) {
    alert("value is true");
}

// 转换为 false 的值
var value = false;
var value2 = ""; // 空字符串
var value3 = 0; // 0或NaN
var value4 = null; // null
var value5 = undefined; // undefined

if (value & value2 & value3 & value4) {
    // 不会进来
} else {
    alert("value is false");
}

typeof  true; // "boolean"

   

 

    4.Number

 

var octalNum1 = 070;  // 八进制56
var octalNum2 = 079; // 无效的八进制——79
var octalNum3 = 08; // 无效的八进制——8
// 在严格模式下以上赋值是无效的 回导致浏览器抛出异常

var hexNum1 = 0xA; // 十六进制的10
var hexNum2 = 0x1f; // 十六进制的31

    4.1 浮点数值

 

    小数点后边必须包含一位数字

 

var floatNum1 = 1.1 或  var floatNum2 = 0.1

    用e表示法表示数值

var floatNum = 3.125e7; // 31250000

    浮点数值最高精度是17位小数

// 浮点预算会失去精度
var a = 0.2;
var b = 0.3;
a + b; // 0.50000000000000004

    4.2 数值范围

 

    最小数值保存在 Number.MIN_VALUE中,值为 5e-324,最大数值保存在 Number.MAX_VALUE 中,值为1.7976931348623157e+308。

     如果计算数值超出了数值范围,这个值会被自动转成Infinity值。可以用isFinite()函数检测是否超出数值范围

var num = Number.MAX_VALUE + Number.MAX_VALUE; // Infinity
isFinite(num);  // false

    4.3 NaN

 

    NaN,表示非数值(Not a Numer)是一个特殊数值,计算导致错误时会返回NaN。

    全局函数isNaN(),接受任何参数来判断这个参数“不是数值”。

    两个特性:任何涉及NaN的操作都会返回NaN,NaN与任何值都不相等。

NaN/10;    // NaN
NaN == NaN; //  false

isNaN(NaN);  // true
isNaN(10);    // false (是一个数值)
isNaN("10"); // false (可以转换成数值)
isNaN("name"); // true (不能转换成数值)
isNaN(true);  // false  (可以转换成数值1)

    4.4 数值转换

 

    有3个函数把非数字转换成数字,Number(),parseInt(),parseFloat()。

 

// Number()
var num1 = Number("hello world"); // NaN
var num2 = Number(""); // 0
var num3 = Number("00011"); // 11
var num4 = Number(true); // 1
var num5 = Number(false); // 0

// parseInt()
var num1 = parseInt("1234hello world"); // 1234
var num2 = parseInt(""); // NaN
var num3 = parseInt("0xA"); // 10(十六进制数)
var num4 = parseInt(22.5); // 22
var num5 = parseInt("70"); // 70
var num6 = parseInt(true); // NaN

var num1 = parseInt("10", 2); // 2 (按二进制解析)
var num2 = parseInt("10", 8); // 8(按八进制解析)
var num3 = parseInt("10", 10); // 10(按十进制解析)
var num4 = parseInt("10", 16); // 16(按十六进制解析)

// parseFloat()
var num1 = parseFloat("1234hello world");  // 1234
var num2 = parseFloat("0xA"); // 0 
var num3 = parseFloat("22.5"); // 22.5 
var num4 = parseFloat(22.34.5); // 22.34
var num5 = parseFloat("0908.5"); // 908.5  
var num6 = parseFloat("3.125e7"); // 31250000

    5. String

 

    String类型用于表示有零或多个16位Unicode编码组成的字符序列,即字符串。

   

    5.1 字符字面量

  •     \n    // 换行
  •     \t     // 制表
  •     \b    //退格
  •     \r    //回车
  •     \\    //进纸
  •     \'    //单引号(‘)
  •     \"    //双引号(“)
  •     \xnn    //以十六进制代码表示的一个字符
  •     \unnn  //以十六进制代码表示的一个Unicode字符

    5.2 字符串的特点

 

var lang = "java";

lang = lang + "Script"; // "javaScript"
lang = lang + 1.8; // "java1.8"
lang = lang + null; // "javanull"
lang = lang + undefined; // "javaundefined"
lang = lang + true; // "javatrue"

    5.3 转换为字符串

 

    

var str  = "hello world";
var num  = 10;
var bool = true;
var func = function(){};
var obj1 = {};
var obj2 = new Object();
var arr1 = [];
var arr2 = ["a","b"];

str.toString();     // "hello world"
num.toString();     // "10"
bool.toString();    // "true"
func.toString();    // "function(){}"
obj1.toString();    // "[object Object]"
obj2.toString();    // "[object Object]"
arr1.toString();    // ""
arr2.toString();    // "a,c"

var value1 = 10;
var value2 = true;
var value3 = null;
var value4 = ;

String(value1); // "10"
String(value2); // "true"
String(value3); // "null" 
String(value4); // "undefined"

    6.Object

 

    创建Object类型的实例

// 字面量方法
var o1 = {};
// new 操作符创建
var o2 = new Object();

    每个Object实例对象都有下列属性和方法:

  •     constructor: 保存着用于创建对象的函数 —— Object()。
  •     hasOwnProperty(propertyName):检车给定的属性是否存在当前对象的实例中(不是原型中)
    var O = {
        "name": "zhang san",
        "age": 25
    };
    O.hasOwnProperty("name");  // true 
  •     isPrototypeOf(object):用于检查一个对象是否存在于另一个对象的原型链上。
    // 定义函数对象
    function Foo() {}
    function Baz() {}
    // 继承Foo的原型
    Baz.prototype = Object.create(Foo.prototype);
    // 创建一个新对象
    var baz = new Baz();
    // 检查baz是否继承自Foo
    Foo.prototype.isPrototypeOf(baz); // true 
  • propertyIsEnumerable(propertyName):用于检查给定属性是否能用for-in语句来枚举。
    var o = {};
    var a = [];
    o.prop = 'is enumerable';
    a[0] = 'is enumerable';
    
    o.propertyIsEnumerable('prop');   // true
    a.propertyIsEnumerable(0);    // true
    
    a = ['is enumerable'];
    a.propertyIsEnumerable(0);          // 返回 true
    
    // 内置对象的属性不能被枚举
    Math.propertyIsEnumerable('random');   // 返回 false
    
    /***********************自身属性和继承属性****************************/
    // 自身属性不能被枚举
    var a = ['is enumerable'];
    a.propertyIsEnumerable('constructor') // 返回 false
    a.propertyIsEnumerable('length');   // 返回 false
    
    
    function firstConstructor() {
      this.property = 'is not enumerable';
    }
    
    firstConstructor.prototype.firstMethod = function() {};
    
    function secondConstructor() {
      this.method = function method() { return 'is enumerable'; };
    }
    
    secondConstructor.prototype = new firstConstructor;
    secondConstructor.prototype.constructor = secondConstructor;
    
    var o = new secondConstructor();
    o.arbitraryProperty = 'is enumerable';
    
    o.propertyIsEnumerable('arbitraryProperty');   // 返回 true
    o.propertyIsEnumerable('method');              // 返回 true
    o.propertyIsEnumerable('property');            // 返回 false
    
    o.property = 'is enumerable';
    
    o.propertyIsEnumerable('property');            // 返回 true
    
    // 这些返回fasle,是因为,在原型链上propertyIsEnumerable不被考虑
    // (尽管最后两个在for-in循环中可以被循环出来)。
    o.propertyIsEnumerable('prototype');   // 返回 false (根据 JS 1.8.1/FF3.6)
    o.propertyIsEnumerable('constructor'); // 返回 false
    o.propertyIsEnumerable('firstMethod'); // 返回 false 
  • toLocaleString(): 返回调用 toString() 的结果。
  • toString(): 返回对象的字符串表示
  • valueOf():返回对象的字符串,数值或布尔值表示,通常与toString()返回值相同。
      

—— 完

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值