[基础类型巩固3]Object

时间:2021-12-28 13:57:19
/**Object对象***********************************************************/
console.log("**Object对象***********************************************************");
console.log(new Object(null));//{},与new Object(undefined)和new Object()结果一样
console.log((new Object(true)).constructor == Boolean);
//1,hasOwnProperty ( prop ) : Boolean 成员方法
/*Returns a boolean indicating whether an object contains the specified property as a direct property of that object
and not inherited through the prototype chain.
最后一句是亮点,从原型链得到的属性不算~!*/
function toStringByObj(obj) {
var r = "";
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
r += "[" + key + "]" + obj[key];
}
else {
console.log("error");
}
}
return r;
}
var base = {name: "hello"};
var child = {age: 28};
child.prototype = base;
console.log(toStringByObj(child));
//2,isPrototypeOf ( prototype, object ) : Boolean
/*函数用于指示对象是否存在于另一个对象的原型链中。
* 注意:a.isPrototypeOf(b)意思是,a是否存在b的原型链中,顺序可别整反了~!*/
function Fee() {
}
function Fi() {
}
var fee = new Fee();
Fi.prototype = fee;
function Fo() {
}
Fo.prototype = new Fi();

function Fum() {
}
Fum.prototype = new Fo();
var fObj = new Fum();
console.log(fee.isPrototypeOf(fObj));//true,fObj的原型链中有fee对象,可以用fObj instanceof Fee替代

function Site() {
}
var s = new Site();
console.log(Site.prototype.isPrototypeOf(s)); //true;s的原型链中有Site.prototype对象.

//3,propertyIsEnumerable ( prop ) : Boolean
/*Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
* Every object has a propertyIsEnumerable method.
* This method can determine whether the specified property in an object can be enumerated by a for...in loop,
* with the exception of properties inherited through the prototype chain.
* If the object does not have the specified property, this method returns false.
*
* 指定的属性是否能枚举,能在for..in循环中使用,原型链的除外
* PARAMETERS
* prop : String The name of the property to test.
* RETURNS :Boolean If the object does not have the specified property, this method returns false.*/
var a = [];
a[0] = 'is enumerable';
console.log(a.propertyIsEnumerable("0")); // returns true
console.log(a.propertyIsEnumerable("length"));//false
for (var index in a) {
console.log(a[index])
}
//4,toString String
/*Returns a string representation of the object.
* SomeClass.prototype.toString = function () {} 覆盖后,能输出自定义的对象*/
var o = new Object(null);
console.log(o.toString());//[object Object]
//5,valueOf Object
/*Returns the primitive value of the specified object.
* By default, the valueOf method is inherited by every object descended from Object.
* Every built- in core object overrides this method to return an appropriate value.
* If an object has no primitive value, valueOf returns the object itself, which is displayed as:
* [object Object]
*
* RETURNS :Object
Returns value of the object or the object itself.*/
var o2 = new Object("Hello o2");
o2.name = "world";
console.log(o2.valueOf());//Hello o2
//6,defineProperties ( obj, props )
/*定义属性(value,writable,get,set,configurable,enumerable)
* 1,属性名称加不加引号都行.
* 2,必须用enumerable: true 修饰属性,才能被枚举,才能被看到.不修饰的话,也能访问
* 3,在 descriptor 中不能同时设置访问器 (get 和 set) 和 wriable 或 value,否则会报以下错误:(重要啊~)
* Invalid property. A property cannot both have accessors and be writable or have a value
* 4,configurable为true的话,标志着这个属性能够被删除,默认false,不可以配置
* configurable如果为false,则任何尝试删除目标属性或修改属性以下特性(writable, configurable, enumerable)
* 的行为将被无效化,默认为 false。*/
var dObj = {};
Object.defineProperties(dObj, {
d1: {
value: true,
enumerable: true,
writable: true,
configurable: false
},
"property2": {
value: "Hello",
enumerable:false,
writable: false//只读属性了
},
property3: {//注意,不能设置writable和value了.
enumerable: true,//能枚举
set: function (x) {
this["property2"] = x;
},
get: function () {
return this["property2"];
}
}
});
delete dObj.d1;//configurable:false,删了也白删除~
dObj["property2"] = "world";//没用~
dObj["property3"] = "world";//依然没用~
console.log(dObj["property2"]);//Hello
console.log(dObj.propertyIsEnumerable("d1"));//true
for (var key in dObj) {
console.log("[" + key + "]" + dObj[key]);
//[d1]true,
//[property3]Hello
//看不到property2,但能访问
}
//7,defineProperty ( obj, prop, descriptor )
/*PARAMETERS
obj : Object
The object on which to define the property.
prop : String
The name of the property to be defined or modified.
descriptor : Object
The descriptor for the property being defined or modified.
*/
//8,getOwnPropertyDescriptor ( obj, prop ) : Mixed
/*PARAMETERS
obj : Object The object in which to look for the property.
prop : String The name of the property whose description is to be retrieved.
A property descriptor is a record with some of the following attributes:

value The value associated with the property (data descriptors only).
writable True if and only if the value associated with the property may be changed (data descriptors only).
get A function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only).
set A function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only).
configurable true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
enumerable true if and only if this property shows up during enumeration of the properties on the corresponding object.
RETURNS :Mixed
Value of the property descriptor.*/
console.log(Object.getOwnPropertyDescriptor(dObj, "property3"));
/*{ get: [Function: get],
set: [Function: set],
enumerable: true,
configurable: false }
*/
//9,freeze ( obj ) 冻结对象
/*Nothing can be added to or removed from the properties set of a frozen object.
Any attempt to do so will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode).
Values cannot be changed for data properties.
Accessor properties (getters and setters) work the same (and still give the illusion that you are changing the value).
Note that values that are objects can still be modified, unless they are also frozen.

最后的意思是,对象中的对象属性能修改,只是冻结了对象的引用~*/
Object.freeze(dObj);
dObj["d1"] = false;
console.log(dObj["d1"]);//true
//10,getOwnPropertyNames ( obj ) : String[]
/*注意:
* 1,property2虽然这个属性的enumerable为false,但依然能打印出来
* PARAMETERS
obj : Object The object whose enumerable and non-enumerable own properties are to be returned.
RETURNS :String[]
Array of property names.*/
console.log(Object.getOwnPropertyNames(dObj));//[ 'd1', 'property2', 'property3' ]
//11,getPrototypeOf ( object ) : Object
/*找到了,通过对象找到原型对象的方法~
* 与isPrototypeOf的区别
* 1,isPrototypeOf是成员方法,getPrototypeOf是类方法
* 2,isPrototypeOf为了判断2对象中,一个对象是否在另一对象的原型链中;getPrototypeOf完全是为了得到原型对象
*
* PARAMETERS
object : Object The object whose prototype is to be returned. Throws a TypeError exception if this parameter isn't an Object.
RETURNS :Object
the prototype*/
console.log(Object.getPrototypeOf(fObj));//Fee {}
//12,preventExtensions ( obj ) 阻止对象扩展
// seal ( obj )密封对象
// freeze ( obj ) 冻结对象
//13,isExtensible ( obj ) : Boolean 是否能扩展
// isFrozen ( obj ) : Boolean 是否被冻结
// isSealed ( obj ) : Boolean 是否密封的
/*Determines if an object is extensible (whether it can have new properties added to it).
Objects are extensible by default: they can have new properties added to them, and can be modified.
An object can be marked as non-extensible using Object#preventExtensions, Object#seal, or Object#freeze.*/
console.log(Object.isExtensible(dObj));//不能扩展了...

//14,keys ( obj ) : String[]
/*注意,和getOwnPropertyNames不同的是,keys只能显示能够枚举的~*/
console.log(Object.keys(dObj));//[ 'd1', 'property3' ]