下面对知识点总结:
1.类型分类
a.原始类型:number, string, boolean, null, undefined
b.对象类型:除了原始类型都是(例如:object,array, function, new xxx[构造函数 class对象 Date, RegExp, Error])
备注:NAN,非数字,与任何值都不等,包括NAN
2.浮点数的计算差值
任何二进制浮点数的编程语言中,都会出现以下问题.因此我们在金融的计算业务中,使用整数计算(例如使用1分代替0.01元)
var a = 0.3 - 0.2;
var b = 0.2 - 0.1;
console.log( a == b); // false
3.对象
3.1 对象的遍历方式
for ....in : 遍历自己可枚举的属性+ 继承的可枚举属性
Object.isOwnProperty(Object, property): 判断是否是自己的属性
3.2 对象的属性
对象上有两种属性:一般的属性和存取器属性。
var object = {
x: 10, // 一般属性
get y() { return 3*this.x},
set y(num) { this.x = num + this.x;}
};
object.y // 30
object.y = 20; // x = 30;
object.y // 120
3.3 属性的特性: 值,可写,可枚举,可配置 || 存取器属性:读取(get), 写入(set), 可枚举,可配置
例如例子,复制属性的特性
Object.defineProperty(Object.prototype,
'extend',
{
writable: true,
enumerable: false,
configurable: true,
value: function(o) {
var names = Object.getOwnPropertyNames(o);
for( var i = 0; i < names.length; i ++) {
console.log(name[i]);
if(names[i] in this) {
continue;
}
else {
Object.defineProperty(this, names[i], Object.getOwnPropertyDescriptor(o, names[i]))
}
}
}
}
)
var a = {x:1};
var b ={y: 2};
a.extend(b); //{x:1,y: 2}
3.4 创建对象
1.字面量创建
var object = {a: 1}; // prototype: Object.prototype
Object.prototype.isPrototypeOf(object); // true
2. new 方式创建
function a() {}
var object = new a(); // prototype: a.prototype
object.__prop__ == a.prototype; // true a.prototype
a.prototype.isPrototypeOf(object); // true
3.Object.create()
var object = Object.create({y: 1}); // prototype: Object.prototype:
Object.prototype.isPrototypeOf(obejct); // true
// Object.prototype: 就是Object的构造函数
// a.prototype: 就是a的构造函数
3.5 对象的三个属性
a.prototype
b.class 类属性
一般是取对象的toString()方法的第8个到倒数第二个位置的字符串
function A() {}
console.log(A.toString().substring(8, -1)); // Function
如果toString方法被重写,就不可以通过这种方式完成,可以通过以下方式:
function classOf(o) {
if(o === null ) { return 'null'};
if(o === undefined) { return 'undefined'}
return Object.prototype.toString.call(o).slice(8,-1)
}
function A() {}
classOf(A) // Function
c.可拓展性
表示对象是否可以添加新的属性。
Object.isExtensible(): 判断是否可拓展
Object.preventExtensions(obj); // 设置不可拓展
// 与上同理,但是可以将属性设置为不可配
Object.isSealed()
Object.seal(obj)
//冻结, 更严格锁定对象,不仅可以设置不可拓展和将属性设置为不可配,可以将所有数据设置为只读(存取器属性不受影响)
Object.freeze()
Object.isFrozen()