JavaScript内建对象-Object



 

一、Object是所有类的父类,Object类存在以下一些静态方法:

1)Object.preventExtensions(object),阻止向对象添加新属性。

var Extensible = {

};

Object.preventExtensions(Extensible);

Extensible.a = 20;

console.log(Extensible.a);//undefined

 

2)Object.getPrototypeOf(object),返回对象的原型。

console.log(Object.getPrototypeOf(Extensible));//获取原型

console.log(Object.getPrototypeOf(Extensible)===Extensible.constructor.prototype);//true

console.log(Object.prototype);//Object对象的原型为null

 

3) Object.getOwnPropertyDescriptor(object, propertyname)

var b = {i:10};

console.log(Object.getOwnPropertyDescriptor(b,"i"));//返回属性描述,包含:value、writable、get、set、configurable(change或者delete)、enumerable(是否枚举)

 

4) Object.defineProperty(object, propertyname, descriptor),将属性添加到对象或修改现有属性的特性

var object = {};

 

// Add a data property to the object.

Object.defineProperty(object, "newDataProperty", {

    value: 101,

    writable: true,

    enumerable: true,

    configurable: true

});

obj.newDataProperty = 102;

console.log(obj.newDataProperty);

 

5)Object.getOwnPropertyNames(object),列出对象的属性

var names = Object.getOwnPropertyNames(obj);

for (var i = 0; i < names.length; i++) {

    var prop = names[i];

    console.log(prop + ': ' + obj[prop]);

}

 

6)Object.defineProperties(object, descriptors),将一个或多个属性添加到对象,并/或修改现有属性的特性

var object = {};

Object.defineProperties(object, {

    newDataProperty: {

        value: 101,

        writable: true,

        enumerable: true,

        configurable: true

    },

    newAccessorProperty: {

        set: function (x) {

            document.write("in property set accessor" + newLine);

            this.newaccpropvalue = x;

        },

        get: function () {

            document.write("in property get accessor" + newLine);

            return this.newaccpropvalue;

        },

        enumerable: true,

        configurable: true

    }});

 

// Set the accessor property value.

object.newAccessorProperty = 10;

 

7) Object.isSealed(object),如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回 true.

var obj = { pasta: "spaghetti", length: 10 };

Object.seal(obj);//阻止修改现有属性的特性,并阻止添加新属性。

console.log(Object.isSealed(obj));//true

obj.newProp = 50;

console.log(obj.newProp);//undefined

delete obj.length;

console.log(obj.length);

obj.length = 20;

console.log(obj.length);//20,对象值可以被修改

 

8)Object.freeze(object),阻止修改现有属性的特性和值,并阻止添加新属性。

var obj = { pasta: "spaghetti", length: 10 };

Object.freeze(obj);

obj.newProp = 50;

console.log(obj.newProp);

 

delete obj.length;

console.log(obj.length);

 

obj.pasta = "linguini";

console.log(obj.pasta);//spaghetti

 

console.log(Object.isFrozen(obj));//true,如果无法在对象中修改现有属性的特性和值,且无法向对象添加新属性,则返回 true。

 

9)Object.create(prototype),建一个具有与 Shape 对象相同的内部原型的对象。

var Shape = { twoDimensional: true, color: undefined, hasLineSegments: undefined };

var Square = Object.create(Object.getPrototypeOf(Shape)); 

 

 

10)Object.keys(object),返回对象的可枚举属性和方法的名称。

function Pasta(grain, width, shape) {

    this.grain = grain;

    this.width = width;

    this.shape = shape;

 

    this.toString = function () {

        return (this.grain + ", " + this.width + ", " + this.shape);

    }

}

 

var spaghetti = new Pasta("wheat", 0.2, "circle");

 

var arr = Object.keys(spaghetti);

 

console.log(arr);

 

// Create a constructor function.

function Pasta(grain, width, shape) {

    this.grain = grain;

    this.width = width;

    this.shape = shape;

}

 

var polenta = new Pasta("corn", 1, "mush");

 

var keys = Object.keys(polenta).filter(CheckKey);//过滤器

document.write(keys);

 

// Check whether the first character of a string is "g".

function CheckKey(value) {

    var firstChar = value.substr(0, 1);

    if (firstChar.toLowerCase() == "g")

        return true;

    else

        return false;

 

}

二、Object类存在以下一些原型方法:

1)object.hasOwnProperty(proName),确定某个对象是否具有带指定名称的属性。

var s = new String("Sample");

console.log(s.hasOwnProperty("split"));//false

 

console.log(String.prototype.hasOwnProperty("split"));//true

 

2)prototype.isPrototypeOf(object),确定一个对象是否存在于另一个对象的原型链中。

function Rectangle() {

}

var rec = new Rectangle();

 

console.log(Rectangle.prototype.isPrototypeOf(rec));

 

3)object.propertyIsEnumerable(proName),确定指定的属性是否可枚举。通常,预定义的属性是不可枚举的,而用户定义的属性始终是可枚举的。

var arr = new Array("apple", "banana", "cactus");

console.log(arr.propertyIsEnumerable(1));//true

 

var object = {id:1,name:"name"};

 

console.log(object.propertyIsEnumerable("name"));//true

 

4)dateObj.toLocaleString(),返回使用当前区域设置转换为字符串的日期。

5)objectname.toString([radix]),返回对象的字符串表示形式。

 

6)object.valueOf(),返回指定对象的基元值。

var attr = [1,2,3,4];

var date  = new Date();

var func = function(){

    console.log("test...");

};

var object = {id:1,name:"name"};

console.log(attr.valueOf());//数组,[1,2,3,4]

console.log(date.valueOf());//日期,从 UTC 1970 年 1 月 1 日午夜开始的存储的时间值(以毫秒为单位)

console.log(func.valueOf());//函数本身

 

 

注意:Math 和 Error 对象都没有 valueOf 方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值