码迷,mamicode.com
首页 > 其他好文 > 详细

ES5 object方法整理

时间:2015-08-12 21:08:34      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

Object.getPrototypeOf(object):调用对象父类原型上的方法;
function Person(){
    this.method1 = function(){alert(1)}
}
Person.prototype.method2 = function(){alert(2);}
 
function Man(){
    this.m1 = function(){
        Object.getPrototypeOf(this).method1();
    }
}
Man.prototype = new Person();
 
Man.prototype.m2 = function(){
    Object.getPrototypeOf(this).method2();
}
 
 
var man = new Man();
man.m1();
man.m2();

Object.getOwnPropertyDescriptor(object, propertyname):获取对象中属性的ECMAScript对象;
var obj = {};

obj.a = "abc";

var descriptor = Object.getOwnPropertyDescriptor(obj, "a");

for(var prop in descriptor){
    document.write(prop + ‘: ‘ + descriptor[prop]);
    document.write("<br />");
}

/*
configurable: true
enumerable: true
value: abc
writable: true
*/
Object.defineProperty(object, propertyname, descriptor):将ECMAScript对象设置为对象中的属性.
var obj = {};

obj.a = "abc";

var descriptor = Object.getOwnPropertyDescriptor(obj, "a");
descriptor.writable = false;
Object.defineProperty(obj, "a", descriptor);
for(var prop in descriptor){
    document.write(prop + ‘: ‘ + descriptor[prop]);
    document.write("<br />");
}
/*
configurable: true
enumerable: true
value: abc
writable: false
*/
object.defineProperties(object, descriptors):用 ECMAScript对象 设置为object中多个属性的值.
var obj = {};

obj.a = "abc";

Object.defineProperties(obj,{
    a:{
        configurable: true,
        enumerable: true,
        value: ‘aaa‘,
        writable: false
    }
});
var descriptor = Object.getOwnPropertyDescriptor(obj, "a");
for(var prop in descriptor){
    document.write(prop + ‘: ‘ + descriptor[prop]);
    document.write("<br />");
}
/*
configurable: true
enumerable: true
value: aaa
writable: false
*/
Object.getOwnPropertyNames(object):返回一个由对象属性名组成的数组(包含不可枚举的)
function a(){
    this.a=‘1‘;
}
a.prototype.b=‘2‘;
var c=new a();
c.c=‘3‘;
alert(Object.getOwnPropertyNames(c));//a,c
Object.create(prototype, descriptors):建立一个原型为[prototype](必需,可为NULL),[descriptors](可选)为ECMAScript对象的对象.
var a = Object.create({a:1,b:2}, {
            c: {
                value: "large",
                enumerable: true
            },
            d: {
                value: "round",
                enumerable: true
            }
        });
Object.seal(object):锁定对象,无法修改对象的属性,无法加入新的属性.并把ECMAScript对象的configurable设置为false;
var obj = {};

obj.a = "abc";
Object.seal(obj);
var descriptor = Object.getOwnPropertyDescriptor(obj, "a");


for(var prop in descriptor){
    document.write(prop + ‘: ‘ + descriptor[prop]);
    document.write("<br />");
}

/*
configurable: false
enumerable: true
value: abc
writable: true
*/
Object.freeze(object):冻结对象,无法修改对象的属性,无法加入新的属性.
(与seal的区别为,freeze会把对象的数据属性的Writable设置为false)
var obj = {};

obj.a = "abc";
Object.freeze(obj);
var descriptor = Object.getOwnPropertyDescriptor(obj, "a");


for(var prop in descriptor){
    document.write(prop + ‘: ‘ + descriptor[prop]);
    document.write("<br />");
}

/*
configurable: false
enumerable: true
value: abc
writable: false
*/
Object.preventExtensions(object):避免加新属性加入对象(Extensible设置为false);
var obj = { a: "1"};

Object.preventExtensions(obj);
document.write(Object.isExtensible(obj));//false

obj.newProp = 50;
document.write(obj.newProp);//undefined
Object.isSealed(object);
Object.isFrozen(object);
Object.isExtensible(object);

判断对象是否为锁定,冻结,不可扩展的.(如果一个对象是冻结的,那其肯定是密封的);

 

var obj = { a: "1"};
//Object.seal(obj);
Object.freeze(obj);
//Object.preventExtensions(obj);

alert(Object.isSealed(obj));
//alert(Object.isFrozen(obj));
//alert(Object.isExtensible(obj));

Object.keys(object):返回一个由对象可枚举的属性组成的数组.

function a(){
    this.a=‘1‘;
}
var b=new a();
alert(Object.keys(b));//a

 

 

ES5 object方法整理

标签:

原文地址:http://www.cnblogs.com/makan/p/4725239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!