[06] JavaScript 類型

下面對知識點總結:css

1.類型分類

a.原始類型:number, string, boolean, null, undefined編程

b.對象類型:除了原始類型都是(例如:object,array, function, new xxx[構造函數 class對象 Date, RegExp, Error])編程語言

備註:NAN,非數字,與任何值都不等,包括NAN函數

2.浮點數的計算差值

任何二進制浮點數的編程語言中,都會出現如下問題.所以咱們在金融的計算業務中,使用整數計算(例如使用1分代替0.01元)this

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 對象的屬性

對象上有兩種屬性:通常的屬性和存取器屬性。prototype

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.prototypeblog

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.可拓展性ip

表示對象是否能夠添加新的屬性。 

Object.isExtensible(): 判斷是否可拓展
Object.preventExtensions(obj); // 設置不可拓展

// 與上同理,可是能夠將屬性設置爲不可配
Object.isSealed()
Object.seal(obj)

//凍結, 更嚴格鎖定對象,不只能夠設置不可拓展和將屬性設置爲不可配,能夠將全部數據設置爲只讀(存取器屬性不受影響)
Object.freeze()
Object.isFrozen()
相關文章
相關標籤/搜索