The code copy is as follows:
function forEach(o){
var html ="";
for(var i in o){
html += i+"="+o[i]+" ";
}
console.log(html);
console.log(o);
}
//1
//Object.create(proto)
//Object.create(proto,descriptors)
//Create an object using the specified prototype and attribute
//parameter:
//proto: The prototype of the newly created object can be null
//descriptors: an optional object that maps attribute names to attribute descriptors
//Return a newly created object, inherited from proto, and has the properties of descriptors at the same time.
The code copy is as follows:
var obj = Object.create({x:1,y:2},{
z:{value:3,writable:true,enumerable:true,configurable:true}
});
forEach(obj)
obj.z=5
console.log(obj)
console.log("=====================================================")
//2
//Object.defineProperties(o,descriptors)
//Create or configure multiple properties of an object
//parameter:
//o: The object on which the attributes are to be created or configured
//descriptors: The object that maps the attribute name to the attribute descriptor
//Return object o
The code copy is as follows:
Object.defineProperties(obj,{
a:{value:"a",writable:false,enumerable:true,configurable:true},
b:{value:"b",writable:false,enumerable:true,configurable:true}
})
forEach(obj);
console.log("=====================================================")
//3
//Object.defineProperty(o,name,desc)
//Create or configure an object's property
//parameter:
//o: Object on which the attribute will be created or configured
//name: The attribute name that will be created or configured
//desc: An attribute descriptor object that describes the new attribute to be created or modifications to existing attributes
//Return object o
The code copy is as follows:
Object.defineProperty(obj,"c",{value:"c",writable:false,enumerable:false,configurable:true})
forEach(obj);
console.log("=====================================================")
//4
//Object.freeze(o)
//Set an object to be unchangeable and will not affect inherited attributes
//parameter:
//o: The object to be frozen
//Return true|false
The code copy is as follows:
var p = {x:1,y:2}
Object.freeze(p);
px =2;
console.log(p);
console.log(Object.isFrozen(p)) //true, it cannot be thawed once frozen
console.log("=====================================================")
//5
//Object.getOwnPropertyDescriptor(o,name)
//parameter:
//o: an object
//name: The attribute name to be queried
//Query the properties of a property
//Returns an attribute descriptor object of the specified attribute of the object. If the specified attribute does not exist, it returns undefined.
/*
The attribute descriptor is an ordinary javascript object that describes the characteristics of a certain object. There are two types of javascript attributes. Data attributes have one value and three properties: enumerable,
writable, and configurable. The accessor property has a getter and/or setter method, and enumeration.
Descriptors for data properties:
{
value: any javascript value,
writable: true|false,
enumerable: true|false,
configurable:true|false
}
Descriptor for accessor properties:
{
get: function or undefined : replace attribute value
set: function or undefined : replace writability
enumerable:true|false,
configurable:true|false
}
*/
The code copy is as follows:
var o5 = Object.getOwnPropertyDescriptor(obj,"c");
console.log(o5);
forEach(o5);
console.log("=====================================================")
//6
//Object.getOwnPropertyNames(o)
//Return the name of the non-inherited attribute
//parameter:
//o: an object
//Returns the names of all non-inherited attributes of o, including which properties are not enumerable. {enumerable:false}
The code copy is as follows:
var o6 = Object.getOwnPropertyNames(obj);
console.log(o6);
console.log("=====================================================")
//7
//Object.getPrototypeOf(o)
//parameter:
//o: an object
//Return the prototype of an object
The code copy is as follows:
var o7 =Object.getPrototypeOf(obj);
console.log(o7);
console.log("=====================================================")
//8
//Object.hasOwnProperty(propname);
//parameter:
//propname contains the property name of the object
//Check whether an attribute is inherited
//Return true|false
The code copy is as follows:
console.log(obj.hasOwnProperty("x")); //=>false
console.log(obj.hasOwnProperty("z")); //=>true
console.log("=====================================================")
//9
//Object.isExtensible(o);
//Judge whether new attributes can be added to an object
//parameter:
//o: Object to be checked for extensibility
//Return can be added as true | cannot be false
//Description: All objects are extensible when created until they are passed into Object.preventExtensions(o) Object.seal(o) or Object.freeze(o);
The code copy is as follows:
console.log(Object.isExtensible(obj)); // =>true
//Object.preventExtensions(obj)//Set it to not extensible
//console.log(Object.isExtensible(obj)); //=>false
console.log("=====================================================")
//10
//Object.isFrozen(o)
//Judge whether the object is unchangeable
//parameter:
//o: Object to be checked
// true if o has been frozen and does not change; otherwise it is false;
The code copy is as follows:
console.log("=====================================================")
//11
//object.isPrototypeOf(o)
//Display whether the current object is another object's prototype
//parameter:
//o: All objects
//If object is a prototype of o, it is true, if o is not an object or object is not an prototype of o, it is false.
The code copy is as follows:
var o = new Object();
Object.prototype.isPrototypeOf(o) // true
Array.prototype.isPrototypeOf([1,2]) //true;
Object.prototype.isPrototypeOf(Function.prototype) //true
console.log("=====================================================")
//12
//Object.isSealed(o)
//Judge whether an object's properties can be added or deleted
//parameter:
//o: Object to be checked
// true if o is enclosed, otherwise false.
// If you cannot add a new (non-inherited) property to an object, and the existing (non-inherited) property is not deleted, it is enclosed.
//The common methods of enclosing an object are Object.seal(o) or Object.freeze(o)
console.log("=====================================================")
//13
//object.keys(o)
//Return free enumerable attribute name
//parameter:
//o: an object
The code copy is as follows:
console.log(Object.keys({x:1,y:2}) ) //=>[x,y]
console.log("=====================================================")
//14
//Object.preventExtensions(o)
//Not adding new attributes on an object
//parameter:
// o: Extensible object to be set
//Once it is set to be non-scalable, it can no longer be changed to be extensible
console.log("=====================================================")
//15
//object.propertyIsEnumerable(propname)
//Detection whether a certain attribute is visible in loop in for/in
//parameter
//propname: A string containing the specified attribute name of the object
//Return true if the object has a non-inherited property named propname and the property is enumerable.
The code copy is as follows:
var o15 = new Object();
o15.x = 15;
o15.propertyIsEnumerable("x"); //true;
o15.propertyIsEnumerable("y"); //false;
o15.propertyIsEnumerable("toString"); //false;
console.log("=====================================================")
//16
//Object.seal(o)
//Block the addition or removal of objects' properties
//parameter
//o: Object to be enclosed
//Return the parameter object in the closed state o
//17
//Object.toLocaleString()
//Return the localized string label of the object local
//The default toLocaleString() method provided by the Object class is just a simple call toString() method.
//But note that other classes (Array, Date, Number, etc.) each define their own version of this method. Used to perform localized string conversion. This method may also be required to be overridden when defining your own class.
//18
//object.toString()
//Define the string representation of an object
//In JavaScript programs, the toString() method is not often called. Generally, if this method is defined in an object, the system will automatically call it when needed to replace the object with a string.
//19
//Object.valueOf()
//The original value of the given object
//Return the original value associated with the specified object. If such a value exists, if there is no value associated with the modified object, the object itself will be returned.