js对象类型文档格式.docx
- 文档编号:6775536
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:29
- 大小:22.50KB
js对象类型文档格式.docx
《js对象类型文档格式.docx》由会员分享,可在线阅读,更多相关《js对象类型文档格式.docx(29页珍藏版)》请在冰点文库上搜索。
42,writable:
true}
o={};
Object.defineProperty(o,"
baz"
{value:
8675309,writable:
false,enumerable:
false});
for(varpind){
alert(p+"
->
"
+d[p]);
}
//dis{value:
false,configurable:
false}
/script>
/body>
/html>
2getOwnpropertyNames
//vararr=["
a"
"
b"
c"
];
//alert(Object.getOwnPropertyNames(arr).sort());
//输出"
0,1,2,length"
//
////类数组对象
//varobj={0:
"
1:
2:
};
//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
varproto={};
varobj=Object.create(proto);
alert(Object.getPrototypeOf(obj)===proto);
alert(Object.getPrototypeOf(obj));
4hasOwnProperty
//使用hasOwnProperty方法判断某对象是否含有特定的自身属性
varo=newObject();
o.prop='
exits'
;
functionchangeO(){
o.newprop=o.prop;
deleteo.prop;
}
//alert(o.hasOwnProperty('
prop'
));
//changeO();
//自身属性和继承属性的区别
//o=newObject();
//o.prop='
exists'
//alert(o.hasOwnProperty('
//返回true
toString'
//返回false
hasOwnProperty'
//遍历一个对象的所有自身属性
//何在遍历一个对象的所有属性时忽略掉继承属性,注意这里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,'
//true
alert(Object.prototype.hasOwnProperty.call(foo,'
5isExtenstion
//新对象默认是可扩展的.
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
//一个对象默认是可扩展的,所以它也是非冻结的.
alert(Object.isFrozen({})===false);
//一个不可扩展的空对象同时也是一个冻结对象.
varvacuouslyFrozen=Object.preventExtensions({});
alert(Object.isFrozen(vacuouslyFrozen)===true);
//一个非空对象默认也是非冻结的.
varoneProp={p:
alert(Object.isFrozen(oneProp)===false);
//让这个对象变的不可扩展,并不意味着这个对象变成了冻结对象,
//因为p属性仍然是可以配置的(而且可写的).
Object.preventExtensions(oneProp);
//...如果删除了这个属性,则它会成为一个冻结对象.
deleteoneProp.p;
alert(Object.isFrozen(oneProp)===true);
//一个不可扩展的对象,拥有一个不可写但可配置的属性,则它仍然是非冻结的.
varnonWritable={e:
plep"
};
Object.preventExtensions(nonWritable);
Object.defineProperty(nonWritable,"
e"
{writable:
//变得不可写
alert(Object.isFrozen(nonWritable)===false);
//把这个属性改为不可配置,会让这个对象成为冻结对象.
{configurable:
//变得不可配置
alert(Object.isFrozen(nonWritable)===true);
//一个不可扩展的对象,拥有一个不可配置但可写的属性,则它仍然是非冻结的.
varnonConfigurable={release:
thekraken!
Object.preventExtensions(nonConfigurable);
Object.defineProperty(nonConfigurable,"
release"
alert(Object.isFrozen(nonConfigurable)===false);
//把这个属性改为不可写,会让这个对象成为冻结对象.
alert(Object.isFrozen(nonConfigurable)===true);
//一个不可扩展的对象,值拥有一个访问器属性,则它仍然是非冻结的.
varaccessor={getfood(){return"
yum"
Object.preventExtensions(accessor);
alert(Object.isFrozen(accessor)===false);
//...但把这个属性改为不可配置,会让这个对象成为冻结对象.
Object.defineProperty(accessor,"
food"
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);
//当然,更是一个不可扩展的对象.
7isProtype
functionFee(){
//...
functionFi(){
Fi.prototype=newFee();
functionFo(){
Fo.prototype=newFi();
functionFum(){
Fum.prototype=newFo();
varfum=newFum();
if(Fi.prototype.isPrototypeOf(fum)){
alert('
thisisatest'
8isSealed
//新建的对象默认不是密封的.
alert(Object.isSealed(empty)===false);
//如果你把一个空对象变的不可扩展,则它同时也会变成个密封对象.
alert(Object.isSealed(empty)===true);
//但如果这个对象不是空对象,则它不会变成密封对象,因为密封对象的所有自身属性必须是不可配置的.
varhasProp={fee:
fiefoefum"
Object.preventExtensions(hasProp);
alert(Object.isSealed(hasProp)===false);
//如果把这个属性变的不可配置,则这个对象也就成了密封对象.
Object.defineProperty(hasProp,"
fee"
alert(Object.isSealed(hasProp)===true);
//最简单的方法来生成一个密封对象,当然是使用Object.seal.
varsealed={};
Object.seal(sealed);
alert(Object.isSealed(sealed)===true);
//一个密封对象同时也是不可扩展的.
//一个密封对象也可以是一个冻结对象,但不是必须的.
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
vararr=["
alert(Object.keys(arr));
//弹出"
//类数组对象
varobj={
0:
1:
2:
};
alert(Object.keys(obj));
//getFoo是个不可枚举的属性
varmy_obj=Object.create({},{
getFoo:
{
value:
returnthis.foo
}
}
});
my_obj.foo=1;
alert(Object.keys(my_obj));
//只弹出foo
10Number
varn=newNumber(123.456);
document.write(n.toFixed
(2));
document.write("
br/>
document.write(n.valueOf());
//alert(Number.isInteger(123));
//alert(Number.isFinite(123));
//alert(Number.isInteger(3.1));
document.write(n.toExponential
(2));
document.write(n.toPrecision(4));
//alert(Number.NaN);
document.write(Number.MAX_VALUE);
document.write(Number.MIN_VALUE);
document.write('
'
document.write(Number.POSITIVE_INFINITY);
document.write(Number.NEGATIVE_INFINITY);
document.write(Number.prototype);
document.write(n.toString());
11创建新对象
//通过newObject()创建对象
varobj=newObject();
//obj.name='
king'
//varusername=obj.name;
////alert(username);
////alert(typeof(obj));
////通过对象字面量创建对象
//var
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- js 对象 类型