Parameters:
(1) obj
Necessary. The name of the variable to which the Object object is assigned.
(2) value
Optional. Any of the JavaScript primitive data types (numbers, Boolean values, or strings). 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.
Method of Object objects
When object is used as a constructor, you can accept a parameter. If the argument is an object, the object is returned directly, or the wrapper object for that value is returned if it is a value of the original type. With 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 above the object objects.
Deployed in object objects themselves
Deploying in Object.prototype objects
The Object.keys method is similar to the Object.getownpropertynames method and is generally used to traverse the properties of an object. All of their arguments are an object that returns an array whose members are all property names (not inherited) of the object itself. The difference is that the former only returns enumerable properties, which also return an enumerable property name. Because an array has an enumerable attribute length, it is generally used to traverse an array using Object.keys.
JavaScript does not provide a way to compute the number of object attributes, which can be obtained by 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 (): Generates a new object, and the object's prototype.
Object.getprototypeof (): Gets the prototype object of the object.
method of object instance objects
Object.prototype.valueOf (): The purpose of the ValueOf method is to return the value of an object, which by default returns the object itself. The main purpose of this method is to invoke this method by default when JavaScript automatically type conversions.
Object.prototype.toString (): The purpose 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 invoked automatically.
Using the call method, you can determine the type of the value by calling the Object.prototype.toString method on any value. The ToString method return values for different data types are as follows:
Value: Returning [object number]
String: Returning [object string]
Boolean value: Return [Object Boolean]
Undefined: Back [object undefined]
Null: Return [object Null]
Object: Returns the name of "[object" + constructor + "]"
Object.prototype.toString.call (2)//"[Object number]"
Object.prototype.toString.call (')//"[Object String]"
Using this feature, you can write a more accurate type-judging function 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 the 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 ({}); True
Type.isnumber (NaN);//False
Type.iselement (document.createelement (' div '));//True
Object's Property model
Inside JavaScript, each property has a corresponding attributes object that holds some of the meta information for that property. Using the Object.getownpropertydescriptor method, you can read the Attributes object for the P property of the O object. The Attributes object contains the following meta information:
Value: Represents the values of this property, which defaults to undefined (can be changed as long as writable and configurable have one that is true).
Writable: Indicates whether the value of this property (value) can be changed, and the default is true.
Enumerable: Indicates whether the property is enumerable and the default is true, that is, the property appears in operations such as for...in and Object.keys (). In general, the system's native properties (that is, non-user-defined properties) are not enumerable.
Represents "configurable" and defaults to true. If set to false, it means that the property cannot be deleted, and the attributes object cannot be changed (except for the Value property, which can still change value if writable is true). That is, the configurable property controls the writable nature of the attributes object.
Represents the value function (getter) of this property, which defaults to undefined.
Represents the stored-value function (setter) for this property, and 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 the attributes 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 of the property, the second is the property name (it should be a string), and the third is the description object of the property. By using this method to define a property, the default values for the writable, configurable, enumerable three 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 you define or modify multiple properties at once, you can use the Object.defineproperties method. Note that once you have defined the value function get (or the stored value function set), you cannot set writable to True, or you can define the Value property at the same time, or you will be able to make an error.
var o = object.defineproperties ({}, {
p1: {value:123, enumerable:true},
P2: {value: "abc", enumerable:true},< C18/>P3: {
get:function () {return
THIS.P1 + this.p2
},
enumerable:true,
configurable:true
}
});
Enumerable can be used to set the secret property, and if the enumerable of a property is false, for ... The in loop, the Object.keys method, and the Json.stringify method do not get the property, but you can get its value directly through o.xx.
The difference between the for...in loop and the Object.keys method is that the former includes attributes that the object inherits from the prototype object, and the latter includes only the properties of the object itself. If you need to get all the properties of the object itself, regardless of the value of the enumerable, you can use the Object.getownpropertynames method.
The configurable nature determines whether a variable can be deleted (delete). When declaring a variable with the var command, the variable's configurable is false, and the variable's configurable value is true when the variable is declared without using the var command (or when the variable is declared using a property assignment). This indicates that delete can only delete the object's properties.
var a1 = 1; Configurable:false
a2 = 1;//configurable:true (equivalent to THIS.A2 = 1)
In addition to the direct definition, attributes can also be defined with an access function (accessor). Where the stored-value function is called the setter, the set command is used, the value function is called the getter, and the Get command is used. Using the Access function, the bidirectional binding between the data object and the DOM object can be realized.
Object.defineproperty (user, ' name ', {
get:function () {return
document.getElementById ("foo"). Value
},
set:function (newvalue) {
document.getElementById ("foo"). Value = NewValue;
},
configurable:true
});
Controlling Object state
JavaScript provides three ways 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 no longer add new properties, but you can delete its existing properties with the Delete command. The Object.isextensible method can be used to check whether a property can be added to an object.
The Object.seal method makes it impossible for an object to add a new property or delete an old property. Object.seal also sets the configurable property of the attributes object of an existing property 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 makes it impossible for an object to add new attributes, delete old properties, or change the value of a property, making the object actually a constant. The Object.isfrozen method is used to check whether an object uses the Object.freeze () method.
Use these methods to lock the object's writable properties, but you can still add attributes to it by changing the object's prototype object.
var o = new Object ();
Object.preventextensions (o);
var proto = Object.getprototypeof (o);
proto.t = "Hello";
O.T
//Hello
One solution is to freeze the prototypes too.
var o = object.seal (
object.create (Object.freeze ({x:1}),
{y: {value:2, writable:true}})
);
Object.getprototypeof (o). T = "Hello";
O.T//undefined
The
PS:
Object object is contained in all other JavaScript objects, and all its methods and properties are available to all other objects. The method can be redefined in a user-defined object and invoked by JavaScript at the appropriate time. The tostring method is an example of an Object method that is frequently redefined.
In this language reference, the description of each object method includes the default and object-specific implementation information for the internal JavaScript object.
IE compatibility, the Microsoft MSDN documentation is "has been introduced in Internet Explorer before Internet Explorer 6", so don't worry ~