parameter:
(1) obj
Required. The name of the variable to which the Object object is assigned.
(2) Value
Optional. Any JavaScript primitive data type (number, boolean, or string). If the value is an object, the returned object is unmodified. If the value is null, "undefined", or "not provided", an object without content is created.
Methods of Object Object
When Object is used as a constructor, it can accept a parameter. If the parameter is an object, it will directly return the object; if it is a value of the original type, it will return the wrapper object corresponding to the value. Using this, you can write a function that determines whether a variable is an object.
function isObject(value) { return value === Object(value);}There are two ways to deploy a method on an Object object.
Deploy in the Object object itself
Deploy in Object.prototype object
The Object.keys method is very similar to the Object.getOwnPropertyNames method and is generally used to traverse the properties of an object. Their parameters are all an object, and they all return an array, and the members of the array are all attribute names of the object itself (rather than inherited). The difference between the two is that the former returns only enumerable attributes, while the latter also returns the non-enumerable attribute names. Since the array has an inenumerable attribute length, the array is generally traversed with Object.keys.
JavaScript does not provide a method to calculate the number of object properties, which can be obtained through Object.keys(o).length and Object.getOwnPropertyNames(o).length.
The Object.observe method is used to observe changes in object properties.
Prototype chain related methods:
Object.create(): Generate a new object and the prototype of the object.
Object.getPrototypeOf(): Gets the Prototype object of the object.
Methods of Object instance object
Object.prototype.valueOf(): The function of the valueOf method is to return the value of an object, and by default, the object itself is returned. The main purpose of this method is that this method will be called by default when JavaScript automatically converts type.
Object.prototype.toString(): The function of the toString method is to return the string form of an object. When an object is used for string addition, the toString method is automatically called.
Using the call method, you can call the Object.prototype.toString method on any value to determine the type of this value. The return value of the toString method of different data types is as follows:
Value: Return [object Number]
String: Return [object String]
Boolean: Returns [object Boolean]
undefined: Return [object Undefined]
null: Return [object Null]
Object: Returns "[object" + the name of the constructor + "]"
Object.prototype.toString.call(2) // "[object Number]"Object.prototype.toString.call('') // "[object String]"Using this feature, you can write a type judgment function that is more accurate than the typeof operator.
var type = function (o){ var s = Object.prototype.toString.call(o); return s.match(//[object (.*?)/]/)[1].toLowerCase();};type({}); // "object"type([]); // "array"type(5); // "number"Based on the above type function, you can also add a method that specifically determines a certain type of data.
['Null', 'Undefined', 'Object', 'Array', 'String', 'Number','Boolean', 'Function', 'RegExp', 'Element', 'NaN', 'Infinite'].forEach(function (t) { type['is' + t] = function (o) { return type(o) === t.toLowerCase(); };});type.isObject({}); // truetype.isNumber(NaN); // falsetype.isElement(document.createElement('div')); // trueThe attribute model of the object
In JavaScript, each attribute has a corresponding attributes object, which saves some meta information of the attribute. Use the Object.getOwnPropertyDescriptor method to read the attributes object of the p attribute of the O object. The attributes object contains the following meta information:
value: represents the value of this property, which is undefined by default (as long as one of the writable and configurable is true, it can be changed).
writable: indicates whether the value of this property can be changed, default is true.
enumerable: Indicates whether the attribute is enumerable, default is true, that is, the attribute will appear in operations such as for…in and Object.keys(). Generally speaking, system native properties (i.e. non-user-defined properties) are not enumerable.
Indicates "configurability", defaults to true. If set to false, it means that the attribute cannot be deleted, and the attributes object must not be changed (except the value attribute, if writable is true, the value can still be changed), that is, the configurable attribute controls the writability of the attributes object.
The value function (getter) representing this property, defaults to undefined.
The value storage function (setter) representing this property, defaults to undefined.
var o = { p: 'a' };Object.getOwnPropertyDescriptor(o, 'p');// Object {// value: "a",// writable: true,// enumerable: true,// configurable: true// }The Object.defineProperty method allows you to define or modify an attribute by defining an attribute object, and then return the modified object. The format is as follows:
Object.defineProperty(object, propertyName, attributesObject)
The Object.defineProperty method accepts three parameters, the first is the object where the property is located, the second is the property name (it should be a string), and the third is the property description object. Through this method, the default values of the writable, configurable, and enumerable properties of the property object are false.
Object.defineProperty(o, "p", { value: "bar"});Object.getOwnPropertyDescriptor(o, 'p');// Object {// value: "bar",// writable: false,// enumerable: false,// configurable: false// }If multiple properties are defined or modified at once, you can use the Object.defineProperties method. It should be noted that once the value function get (or value-storage function set) is defined, the writable cannot be set to true, or the value attribute is defined at the same time, otherwise an error will be reported.
var o = Object.defineProperties({}, { p1: {value: 123, enumerable: true}, p2: { value: "abc", enumerable: true}, p3: { get: function () { return this.p1 + this.p2 }, enumerable: true, configurable: true }});Enumerable can be used to set the "secret" property. If the enumerable of a property is false, the for..in loop, the Object.keys method, and the JSON.stringify method will not get the property, but its value can be obtained directly through o.xx.
The difference between the for…in loop and the Object.keys method is that the former includes the properties of the object inherited from the prototype object, while the latter only includes the properties of the object itself. If you need to get all properties of the object itself, regardless of the value of the enumerable, you can use the Object.getOwnPropertyNames method.
Configurability determines whether a variable can be deleted. When a variable is declared using the var command, the variable's configurable is false, and when a variable is declared using the var command (or a variable is declared using the attribute assignment), the variable's configurability is true. This means that delete can only delete the object's properties.
var a1 = 1; // configurable: falsesea2 = 1; // configurable: true (equivalent to this.a2 = 1)
In addition to direct definition, properties can also be defined using accessor functions. Among them, the value storage function is called setter, which uses the set command; the value acquisition function is called getter, which uses the get command. Using the access function, two-way binding between data objects and DOM objects can be realized.
Object.defineProperty(user, 'name', { get: function () { return document.getElementById("foo").value }, set: function (newValue) { document.getElementById("foo").value = newValue; }, configurable: true});Control object status
JavaScript provides three methods to accurately control the read and write state of an object and prevent the object from being changed. The weakest layer of protection is preventExtensions, followed by seal, the strongest freeze.
The Object.preventExtensions method can make an object unable to add new properties, but it can use the delete command to delete its existing properties. The Object.isExtensible method can be used to check whether an object can be added.
The Object.seal method makes it impossible for an object to add new properties or delete old properties. Object.seal also sets the configurable attribute of the attributes object of the existing attributes to false, so that the attributes object can no longer be changed. The Object.isSealed method is used to check whether an object uses the Object.seal method.
The Object.freeze method can make an object unable to add new attributes, delete old attributes, and change the value of attributes, making this object actually a constant. The Object.isFrozen method is used to check whether an object uses the Object.freeze() method.
Use the above methods to lock the writability of the object, but you can still add properties to it by changing the prototype object of the object.
var o = new Object();Object.preventExtensions(o);var proto = Object.getPrototypeOf(o);proto.t = "hello";ot// hello
One solution is to freeze the prototype as well.
var o = Object.seal( Object.create(Object.freeze({x:1}), {y: {value: 2, writable: true}}));Object.getPrototypeOf(o).t = "hello";ot // undefinedPS:
An Object object is included in all other JavaScript objects; all its methods and properties are available for all other objects. Methods can be redefined in user-defined objects and called by JavaScript at the appropriate time. The toString method is an example of a frequently redefined Object method.
In this language reference, the description of each Object method includes default and object-specific implementation information for the internal JavaScript object.
In terms of IE compatibility, Microsoft's MSDN document is "Object Object has been introduced in Internet Explorer before Internet Explorer 6", so don't worry ~