js对象类型.docx

上传人:b****7 文档编号:10777116 上传时间:2023-02-22 格式:DOCX 页数:29 大小:22.50KB
下载 相关 举报
js对象类型.docx_第1页
第1页 / 共29页
js对象类型.docx_第2页
第2页 / 共29页
js对象类型.docx_第3页
第3页 / 共29页
js对象类型.docx_第4页
第4页 / 共29页
js对象类型.docx_第5页
第5页 / 共29页
点击查看更多>>
下载资源
资源描述

js对象类型.docx

《js对象类型.docx》由会员分享,可在线阅读,更多相关《js对象类型.docx(29页珍藏版)》请在冰豆网上搜索。

js对象类型.docx

js对象类型

1getOwnPropertyDescriptor

DOCTYPEhtml>

varo,d;

o={getfoo(){return17;}};

d=Object.getOwnPropertyDescriptor(o,"foo");

//dis{configurable:

true,enumerable:

true,get:

/*访问器函数*/,set:

undefined}

o={bar:

42};

d=Object.getOwnPropertyDescriptor(o,"bar");

//dis{configurable:

true,enumerable:

true,value:

42,writable:

true}

o={};

Object.defineProperty(o,"baz",{value:

8675309,writable:

false,enumerable:

false});

d=Object.getOwnPropertyDescriptor(o,"baz");

for(varpind){

alert(p+"->"+d[p]);

}

//dis{value:

8675309,writable:

false,enumerable:

false,configurable:

false}

2getOwnpropertyNames

DOCTYPEhtml>

//vararr=["a","b","c"];

//alert(Object.getOwnPropertyNames(arr).sort());//输出"0,1,2,length"

//

////类数组对象

//varobj={0:

"a",1:

"b",2:

"c"};

//alert(Object.getOwnPropertyNames(obj).sort());//输出"0,1,2"

//

////使用Array.forEach输出属性名和属性值

//Object.getOwnPropertyNames(obj).forEach(function(val,idx,array){

//alert(val+"->"+obj[val]);

//});

////输出

////0->a

////1->b

////2->c

//

////不可枚举属性

//varmy_obj=Object.create({},{getFoo:

{value:

function(){returnthis.foo;},enumerable:

false}});

//my_obj.foo=1;

//

//alert(Object.getOwnPropertyNames(my_obj).sort());//输出"foo,getFoo"

//下面的例子演示了该方法不会获取到原型链上的属性:

functionParentClass(){}

ParentClass.prototype.inheritedMethod=function(){};

functionChildClass(){

this.prop=5;

this.method=function(){};

}

ChildClass.prototype=newParentClass;

ChildClass.prototype.prototypeMethod=function(){};

alert(Object.getOwnPropertyNames(newChildClass()));//弹出["prop","method"]))

3getPrototypeOf

DOCTYPEhtml>

varproto={};

varobj=Object.create(proto);

alert(Object.getPrototypeOf(obj)===proto);

alert(Object.getPrototypeOf(obj));

4hasOwnProperty

DOCTYPEhtml>

//使用hasOwnProperty方法判断某对象是否含有特定的自身属性

varo=newObject();

o.prop='exits';

functionchangeO(){

o.newprop=o.prop;

deleteo.prop;

}

//alert(o.hasOwnProperty('prop'));

//changeO();

//alert(o.hasOwnProperty('prop'));

//自身属性和继承属性的区别

//o=newObject();

//o.prop='exists';

//alert(o.hasOwnProperty('prop'));//返回true

//alert(o.hasOwnProperty('toString'));//返回false

//alert(o.hasOwnProperty('hasOwnProperty'));//返回false

//遍历一个对象的所有自身属性

//何在遍历一个对象的所有属性时忽略掉继承属性,注意这里for..in循环只会遍历可枚举属性,这通常就是我们想要的,

//直接使用Object.getOwnPropertyNames()方法也可以实现类似的需求。

varbuz={

fog:

'stack'

};

for(varnameinbuz){

if(buz.hasOwnProperty(name)){

alert("thisisfog("+name+")forsure.Value:

"+buz[name]);

}

else{

alert(name);//toStringorsomethingelse

}

}

//hasOwnProperty方法有可能被遮蔽

//如果一个对象拥有自己的hasOwnProperty方法,则原型链上的同名方法会被遮蔽(shadowed)

varfoo={

hasOwnProperty:

function(){

returnfalse;

},

bar:

'Herebedragons'

};

foo.hasOwnProperty('bar');//始终返回false

//如果担心这种情况,可以直接使用原型链上真正的hasOwnProperty方法

alert(({}).hasOwnProperty.call(foo,'bar'));//true

alert(Object.prototype.hasOwnProperty.call(foo,'bar'));//true

 

5isExtenstion

DOCTYPEhtml>

//新对象默认是可扩展的.

varempty={};

alert(Object.isExtensible(empty)===true);

//...可以变的不可扩展.

Object.preventExtensions(empty);

alert(Object.isExtensible(empty)===false);

//密封对象是不可扩展的.

varsealed=Object.seal({});

alert(Object.isExtensible(sealed)===false);

//冻结对象也是不可扩展.

varfrozen=Object.freeze({});

alert(Object.isExtensible(frozen)===false);

6isFrozen

DOCTYPEhtml>

//一个对象默认是可扩展的,所以它也是非冻结的.

alert(Object.isFrozen({})===false);

//一个不可扩展的空对象同时也是一个冻结对象.

varvacuouslyFrozen=Object.preventExtensions({});

alert(Object.isFrozen(vacuouslyFrozen)===true);

//一个非空对象默认也是非冻结的.

varoneProp={p:

42};

alert(Object.isFrozen(oneProp)===false);

//让这个对象变的不可扩展,并不意味着这个对象变成了冻结对象,

//因为p属性仍然是可以配置的(而且可写的).

Object.preventExtensions(oneProp);

alert(Object.isFrozen(oneProp)===false);

//...如果删除了这个属性,则它会成为一个冻结对象.

deleteoneProp.p;

alert(Object.isFrozen(oneProp)===true);

//一个不可扩展的对象,拥有一个不可写但可配置的属性,则它仍然是非冻结的.

varnonWritable={e:

"plep"};

Object.preventExtensions(nonWritable);

Object.defineProperty(nonWritable,"e",{writable:

false});//变得不可写

alert(Object.isFrozen(nonWritable)===false);

//把这个属性改为不可配置,会让这个对象成为冻结对象.

Object.defineProperty(nonWritable,"e",{configurable:

false});//变得不可配置

alert(Object.isFrozen(nonWritable)===true);

//一个不可扩展的对象,拥有一个不可配置但可写的属性,则它仍然是非冻结的.

varnonConfigurable={release:

"thekraken!

"};

Object.preventExtensions(nonConfigurable);

Object.defineProperty(nonConfigurable,"release",{configurable:

false});

alert(Object.isFrozen(nonConfigurable)===false);

//把这个属性改为不可写,会让这个对象成为冻结对象.

Object.defineProperty(nonConfigurable,"release",{writable:

false});

alert(Object.isFrozen(nonConfigurable)===true);

//一个不可扩展的对象,值拥有一个访问器属性,则它仍然是非冻结的.

varaccessor={getfood(){return"yum";}};

Object.preventExtensions(accessor);

alert(Object.isFrozen(accessor)===false);

//...但把这个属性改为不可配置,会让这个对象成为冻结对象.

Object.defineProperty(accessor,"food",{configurable:

false});

alert(Object.isFrozen(accessor)===true);

//使用Object.freeze是冻结一个对象最方便的方法.

varfrozen={1:

81};

alert(Object.isFrozen(frozen)===false);

Object.freeze(frozen);

alert(Object.isFrozen(frozen)===true);

//一个冻结对象也是一个密封对象.

alert(Object.isSealed(frozen)===true);

//当然,更是一个不可扩展的对象.

alert(Object.isExtensible(frozen)===false);

7isProtype

DOCTYPEhtml>

functionFee(){

//...

}

functionFi(){

//...

}

Fi.prototype=newFee();

functionFo(){

//...

}

Fo.prototype=newFi();

functionFum(){

//...

}

Fum.prototype=newFo();

varfum=newFum();

if(Fi.prototype.isPrototypeOf(fum)){

alert('thisisatest');

}

8isSealed

DOCTYPEhtml>

//新建的对象默认不是密封的.

varempty={};

alert(Object.isSealed(empty)===false);

//如果你把一个空对象变的不可扩展,则它同时也会变成个密封对象.

Object.preventExtensions(empty);

alert(Object.isSealed(empty)===true);

//但如果这个对象不是空对象,则它不会变成密封对象,因为密封对象的所有自身属性必须是不可配置的.

varhasProp={fee:

"fiefoefum"};

Object.preventExtensions(hasProp);

alert(Object.isSealed(hasProp)===false);

//如果把这个属性变的不可配置,则这个对象也就成了密封对象.

Object.defineProperty(hasProp,"fee",{configurable:

false});

alert(Object.isSealed(hasProp)===true);

//最简单的方法来生成一个密封对象,当然是使用Object.seal.

varsealed={};

Object.seal(sealed);

alert(Object.isSealed(sealed)===true);

//一个密封对象同时也是不可扩展的.

alert(Object.isExtensible(sealed)===false);

//一个密封对象也可以是一个冻结对象,但不是必须的.

alert(Object.isFrozen(sealed)===true);//所有的属性都是不可写

vars2=Object.seal({p:

3});

alert(Object.isFrozen(s2)===false);//属性"p"可写

vars3=Object.seal({getp(){return0;}});

alert(Object.isFrozen(s3)===true);//访问器属性不考虑可写不可写,只考虑是否可配置

9keys

DOCTYPEhtml>

vararr=["a","b","c"];

alert(Object.keys(arr));//弹出"0,1,2"

//类数组对象

varobj={

0:

"a",

1:

"b",

2:

"c"

};

alert(Object.keys(obj));//弹出"0,1,2"

//getFoo是个不可枚举的属性

varmy_obj=Object.create({},{

getFoo:

{

value:

function(){

returnthis.foo

}

}

});

my_obj.foo=1;

alert(Object.keys(my_obj));//只弹出foo

10Number

DOCTYPEhtml>

varn=newNumber(123.456);

document.write(n.toFixed

(2));

document.write("
");

document.write(n.valueOf());

document.write("
");

//alert(Number.isInteger(123));

//alert(Number.isFinite(123));

//alert(Number.isInteger(3.1));

document.write(n.toExponential

(2));

document.write("
");

document.write(n.toPrecision(4));

//alert(Number.NaN);

document.write("
");

document.write(Number.MAX_VALUE);

document.write("
");

document.write(Number.MIN_VALUE);

document.write('
');

document.write(Number.POSITIVE_INFINITY);

document.write("
");

document.write(Number.NEGATIVE_INFINITY);

document.write('
');

document.write(Number.MAX_VALUE);

document.write("
");

document.write(Number.prototype);

document.write("
");

document.write(n.valueOf());

document.write("
");

document.write(n.toString());

11创建新对象

DOCTYPEhtml>

//通过newObject()创建对象

varobj=newObject();

//obj.name='king';

//varusername=obj.name;

////alert(username);

////alert(typeof(obj));

////通过对象字面量创建对象

//var

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 教育学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1