JS学习笔记——JavaScript数据类型

JS学习笔记——JavaScript数据类型

(值类型)基本类型:undefined、null、string、number、boolean、symbol
引用数据类型:array、function、object

  1. undefined:变量不含有值
  2. null:清空变量的值
  3. string:字符串,es6中字符串格式化可以使用${expression},如:
x = '字符串';
console.log(`我是${x}`);		//输出:我是字符串
  1. number:这种类型用来表示整数和浮点数值,还有一种特殊的数值,即NaN(非数值 Not a Number)。这个数值用于表示一个本来要返回数值的操作数未返回数值的情况(这样就不会抛出错误了)。例如,在其他编程语言中,任何数值除以0都会导致错误,从而停止代码执行。但在JavaScript中,任何数值除以0会返回NaN,因此不会影响其他代码的执行。在js中,console.log(1+‘2’)返回NaN,而且,当算式中含有一个NaN值时,结果将会返回NaN。NaN后接字符串将会打印NaN+字符串,如:
console.log(1+'2')	//输出:NaN
console.log(1+'2'+3)	//输出:NaN
console.log(1+'2'+'2')	//输出:NaN2
  1. boolean:布尔值true和false。可以对任何数据类型的值调用Boolean()函数,而且总会返回一个Boolean值。至于返回的这个值是true还是false,取决于要转换值的数据类型及其实际值。下表给出了各种数据类型及其对象的转换规则。
    boolean()

  2. symbol:ES6引入的一种表示独一无二的值的基本数据类型。暂时还没有用上,后续补充。

  3. array:数组类型,js中提供了很多关于数组的操作:DNM中关于js数组的方法介绍
    当检测Array实例时, Array.isArray() 优于 instanceof,因为Array.isArray能检测iframes.检测数组的方法为

arr = []	//创建新数组
Object.prototype.toString.call(arr) === '[object Array]';	//true;这是Array.isArray()方法的实现
//在iframes中,isArray()和instanceof不一致,应该采用isArray()来判断
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]

// Correctly checking for Array
Array.isArray(arr);  // true
// Considered harmful, because doesn't work though iframes
arr instanceof Array; // false

8.function:ES6新增箭头函数:(*argment) => {};箭头函数中的this来自函数的作用域链,它的取值遵循普通普通变量一样的规则,在函数作用域链中一层一层往上找。
有了箭头函数,只要遵守下面的规则,this的问题就可以基本上不用管了:

  • 对于需要使用object.method()方式调用的函数,使用普通函数定义,不要使用箭头函数。对象方法中所使用的this值有确定的含义,指的就是object本身。
  • 其他情况下,全部使用箭头函数

const person1 = {
    height: 180,
    get_height() {
        let get_true_height = function() {
            console.log(this.height-2);
            console.log(this === person1);
        }
        get_true_height();
    }
};
const person2 = {
    height: 180,
    get_height() {
        let get_true_height = () => {
            console.log(this.height-2);
            console.log(this === person2);
        }
        get_true_height();
    }
};
person1.get_height();		//输出:NaN,false
person2.get_height();		//输出:178,true

在普通函数中可以用一个临时变量来存储this的指向值,也可以用bind()函数来避开this指向问题

const person1 = {
    height: 180,
    get_height() {
    	self = this
        let get_true_height = function() {
            console.log(self.height-2);
            console.log(self === person1);
        };
        get_true_height();
    }
};
person1.get_height();	//输出:178,true
 const person1 = {
    height: 180,
    get_height() {
        let get_true_height = function() {
            console.log(this.height-2);
            console.log(this === person1);
        }.bind(this);
        get_true_height();
    }
};
person1.gete_height();	//输出:178,true
  1. object:对象数据,在js中绝大部分数据都是对象,对象以键值对的形势定义。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值