ES6入门之对象扩展
ES5對象(超類)原有:
屬性:construct構造函數
方法:
object.hasOwnProperty( propertyName ) //檢測是否有一個本地的屬性而不是繼承的,返回boolen prototypeObject.isPrototypeOf( object ) //檢測指示對象是否存在于另一個對象的原型鏈中,返回boolenpropertyIsEnumerable() //指定屬性是否為對象的一部分以及該屬性是否是可枚舉的,返回boolen var arr = [1, 2];arr.propertyIsEnumerable( 1 ) ; // true // 數組的length為內置屬性arr.propertyIsEnumerable( "length" ) // false object.valueOf( ) //返回指定對象的原生值 // Date:當前時間距1970年1月1日午夜的毫秒數 var date = new Date(2013, 7, 18, 23, 11, 59, 230);date.valueOf() ; // 1376838719230// Number:返回數字值 var num = 15.26540;num.valueOf() ; // 15.2654
靜態方法:
?
property特征:
1. value:值,默認是undefined
2. writable:是否可更改,默認是false
3. enumerable:是否可以被枚舉(for in),默認false
4. configurable:是否可以被刪除,默認false
get/set不能和value、writable同時使用
5.get:返回property的值得方法,默認是undefined
6.set:為property設置值的方法,默認是undefined
?
Object.create(prototype[,descriptors]) //通過指定原型及屬性創建一個新對象
var d = Object.create({a:1}); console.log(d); //Object {a:1}var e = Object.create(Array);
console.log(e); //Function {}
Object.defineProperties() //創建或配置多個屬性
Object.defineProperty() //創建或配置某個屬性
var o = {}; //這里的o對象必須提前定義 //多個屬性 Object.defineProperties(o, {'age': {value: 24,writable: true,enumerable: true,configurable: true},'sex': {value: 'male',writable: false,enumerable: false,configurable: false}}); console.log(o); //Object {age: 24, sex: "male"}//單個屬性 Object.defineProperty(o,'age', {value: 24,writable: true,enumerable: true,configurable: true});?Object.getOwnPropertyDescriptor(obj,property) //獲取指定對象的指定屬性
var props = Object.getOwnPropertyDescriptor(o, 'age');console.log(props); //Object {value: 24, writable: true, enumerable: true, configurable: true}Object.getOwnPropertyNames() //返回指定對象的所有非繼承包括不可枚舉屬性名的數組
console.log(Object.getOwnPropertyNames(o)); //["age", "sex"]Object.getPrototypeOf() //返回指定對象的原型
Object.keys() //返回指定對象所有非繼承可枚舉屬性名的數組(注意與getOwnPropertyNames的區別)
Object.preventExtensions(obj)//阻止向指定對象添加新的屬性,但是仍可以更改舊的屬性
Object.isExtensible(obj) //檢測對象是否可以添加新的屬性
Object.seal(obj) //阻止向指定對象添加新屬性或者是刪除現有屬性
Object.isSealed(obj) //檢測是否密封
Object.freeze(obj) //完全凍結,屬性的一切操作均無效
Object.isFrozen(obj) //是否已凍結
?
ES6對象的擴展
?屬性的簡化:
ES6允許直接寫入變量和函數,作為對象的屬性和方法
var birth = '2000/01/01';var Person = {name: '張三',//等同于birth: birth birth,// 等同于hello: function ()...hello() { console.log('我的名字是', this.name); }};注意,屬性名表達式與簡潔表示法,不能同時使用,會報錯。
// 報錯 var foo = 'bar'; var bar = 'abc'; var baz = { [foo] };// 正確 var foo = 'bar'; var baz = { [foo]: 'abc'};函數的name屬性,返回函數名。對象方法也是函數,因此也有name屬性。如果使用了取值函數,則會在方法名前加上get。如果是存值函數,方法名的前面會加上set。
var person = {sayName() {console.log(this.name);},get firstName() {return "Nicholas"} }person.sayName.name // "sayName" person.firstName.name // "get firstName"有兩種特殊情況:bind方法創造的函數,name屬性返回“bound”加上原函數的名字;Function構造函數創造的函數,name屬性返回“anonymous”。
(new Function()).name // "anonymous"var doSomething = function() {// ... }; doSomething.bind().name // "bound doSomething"如果對象的方法是一個Symbol值,那么name屬性返回的是這個Symbol值的描述
Object.is()
+0 === -0 //true NaN === NaN // false Object.is(+0, -0) // false Object.is(NaN, NaN) // trueObject.assign()第一個參數是目標對象,后面的參數都是源對象。用于對象合并,將源對象(source)的所有可枚舉屬性,復制到目標對象(target)。
注意,如果目標對象與源對象有同名屬性,或多個源對象有同名屬性,則后面的屬性會覆蓋前面的屬性。
var target = { a: 1, b: 1 };var source1 = { b: 2, c: 2 }; var source2 = { c: 3 };Object.assign(target, source1, source2); target // {a:1, b:2, c:3}只有一個參數則返回參數,如果這個參數不為對象,則直接轉為對象,但是undefined和null不可以。
?
轉載于:https://www.cnblogs.com/sker/p/5462694.html
總結
以上是生活随笔為你收集整理的ES6入门之对象扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 5274 Dylans love
- 下一篇: ASP.NET Core 1.1 Pre