javaScript面向对象的程序设计(一)——数据类型

数据类型

  • 字符串(string) ::string
  • 数字(number) ::整型 浮点型 NaN
  • 布尔(boolean) ::true false
  • 数组(array)
  • 对象(object) ::
  • Null ::null
  • Undefined ::undefined

引用类型(栈&&堆)

值引用

undefined, number, string, boolean属于值类型,不是对象。

//Number
var num1;   //这个时候不进行内存分配
var num3=9; //分配内存
var num4=num3; //会分配内存

//String
var str1; //这个时候不进行内存分配
var str2 = 'blog'; //分配内存
var str3=str2; //会分配内存

//boolean
//undefined
类型引用

函数、数组、对象、null、new Number(10)都是对象,他们都是引用类型。
空间 = 指针+数据

//Array
var arr1=['Marvin','blog']; //分配内存
var arr2=arr1; //不分配内存,其实arr2变量中存的是地址,指向arr1内存

//Function
function sum(num1,num2){
        return num1 + num2;
}
var sum1 = sum2;

//值引用和类型引用的区别
var fun = function(){}
    name = '';
    fun.prototype = {
        info : {
            name1 : 'peter',
            age : 25
        }
    }

var a = new fun();
var b = new fun();

a.name = 'jack';
b.name = 'tom';

console.log(a.name) //jack
console.log(b.name) //tom

a.info.name1 = 'jack'; 
b.info.name1 = 'tom';

console.log(a.info.name) //tom
console.log(b.info.name) //tom

//Object
var obj = {}
var obj2 = obj1;
var a = {"x": 1};
var b = a;
a.x = 2;
console.log(b.x);   // 2
a = {"x":3}; //重新分配空间,
console.log(b.x);  // 2


var a = {"x": 1};
var b = a;
a.x = 2;
console.log(b.x); // 2

a = {"x":3};  
console.log(b.x); // 2

a.x = 4;
console.log(b.x);  // 2

数据结构的检测

isFinite(): 检测数据是否是无穷大

//数据类型判断 - typeof
console.log(typeof undefined)//'undefined'
console.log(typeof null) // well-known bug
console.log(typeof true) //'boolean'
//数据类型判断 - toString.call
//通用但很繁琐的方法: prototype
console.log(toString.call(123))          //[object Number]
console.log(toString.call('123'))        //[object String]
console.log(toString.call(undefined))    //[object Undefined]
console.log(toString.call(true))         //[object Boolean]
console.log(Object.prototype.toString.call(str) === '[object String]')   //-------> true;
    console.log(Object.prototype.toString.call(num) === '[object Number]')   //-------> true;
    console.log(Object.prototype.toString.call(arr) === '[object Array]')    //-------> true;
//数据类型判断 - instanceof
console.log(arr instanceof Array)     //---------------> true
    console.log(date instanceof Date)     //---------------> true
    console.log(fn instanceof Function)   //------------> true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值