ES5
-
Object.create(prototype, descriptors)
//创建对象 var o1 = {foo:'bar'}; var o2 = new Object(); //Object.create(proto,[descriptors]); var o3 = Object.create(null); function Person(name) { this.name = name; } var p1 = new Person('sindy'); var p2 = Object.create(Person.prototype, { age: { value: 18, writable: false, enumerable: false, configurable: true, //value , writable 和 getter setter函数不能同时设置 /*get: function () { if (typeof age === 'undefined') { return 'not set age value yet'; } else { return 'your age:' + age; } }, set: function (a) { age = a; if (this.cTime == 0) { console.log('set age OK'); this.cTime++; } else { console.log('change age OK'); } } },*/ cTime: { value: 0, writable: true } });
-
Object.defineProperty(object, propertyName, descriptor)
var p1 = new Person('alice'); Object.defineProperty(p1, 'name', {value:'alice2', writable:true, enumerable: false, configurable: false}); console.log(p1.name); //=> alice2 delete p1.name; //=> false configurable==flase,不能defineProperty重定义属性特性 Object.defineProperty(p1, 'name', {value:'kk', enumerable: true}); //=>error
-
Object.defineProperties(object, descriptors)
var p1 = new Person('alice');
Object.defineProperties(p1,{
skill:{
value: 'singing',
writable: true,
enumerable: true,
configurable: false
},
age: {
value: 18,
writable: true,
enumerable: true,
configurable: false
}
});
p1.age //18
p1.skill //singing -
Object.getOwnPropertyDescriptor(obj, propertyName)
获取自有属性的descriptorfunction Person (n){ this.name = n; } Person.prototype = { constructor: Person, hi: function(name){ console.log('hi,'+name);} }; var p1 = new Person('alice'); var nameDescriptor = Object.getOwnPropertyDescriptor(p1, 'name');//=>{value:'alice', enumerable: true, writable: true, configurable: true} var des = Object.getOwnPropertyDescriptor(p1, 'hi');//=>undefined
-
Object.getOwnPropertyNames(obj)
获取对象所有的自有属性名//接上 p1.age = 11; var props = Object.getOwnPropertyNames(p1); //=> ['name', 'age']
-
Object.preventExtensions(obj)
禁止扩展对象,即不能添加属性到对象上。//接上 Object.preventExtensions(p1); p1.home = 'HK'; console.log(p1.home); //=> undefined
-
Object.isExtensible(obj)
判断对象是否可扩展//接上 Object.isExtensible(p1);//=> false
-
Object.seal(obj) , Object.isSealed(obj)
若对象为isSeal状态则不能添加或删除属性, 但可以修改现有属性的值//接上 Object.seal(p1); Object.isSealed(p1); //true delete p1.name;//=>false p1.like = 'shopping'; console.log(p1.like); //=> undefined
-
Object.freeze(obj) , Object.isFrozen(obj)
冻结对象(不能添加或删除属性,不能修改现有属性) freeze -> seal -> configurable:false 限制的严格程度递减Object.freeze(p1); Object.isFrozen(p1); //=> true p1.name = 'alice2'; console.log(p1.name); //=> alice p1.school = 'hkzw'; console.log(p1.school); //=> undefined
-
Object.getPrototypeOf(obj);
获取对象的原型对象var proto = Object.getPrototypeOf(p1);//=>{constructor: Person, hi: function(){..}}
-
Array.isArray(arg)
-
[].indexOf(val,[pos])
-
[].lastIndexOf(val, [pos])
-
[].every(fn(v, i, arr),[context])
-
[].some(fn(v,i, arr), [context])
-
[].forEach(fn(v, i, arr), [context])
-
[].filter(fn(v, i, arr), [context])
-
[].map(fn(v, i, arr), [context]);
-
[].reduce(fn(prev, curr, i, arr), [primitive]);
-
[].reduceRight(fn(prev, curr, i, arr), [primitive]);
-
new Date().toJSON()
序列化日期对象 输出:"2016-03-14T07:36:09.602Z"
-
String.prototype.trim
删除字符串两端空格(" touch ").trim(); => touch