Mention the operation, many people will think of our learning process most often do the operation, is to increase the database, delete, change, check, since the mention of this, then the object of the property operation is no exception, basically can be said also these several operations.
Attribute tags for objects in JS
Writable (writable), enumerable (enumerable), configurable (configurable), value (value), Get/set (Get, set method). These tags will be gradually enriched in the back, here is a brief introduction.
Read and Write properties
The reading and writing of the property has almost already been said about the object, here to add and summarize. This is done through the ". Method", or through a string of key values as an index read.
var object = {
X:1,
Y:2
};
document.write (Object.x); 1
document.write (object["y"]); 2
object["x"] = 3;
Object.y = 4;
document.write (object["x"]); 3
document.write (OBJECT.Y); 4
Traversing Object Properties
In general, the object's properties are traversed by for-in, and the code is spoken against the above object.
var p;
For (P-in object) {
if (Object.hasownproperty (p)) {
document.write (P + ":" Object[p] + "<br/>");
}
}
This code is designed to iterate through all the attributes above the object, adding a judgment because the for in is traversing through all the properties, including the properties on the prototype chain, so remember not to forget to add judgment when traversing the properties of the object.
Undefined properties Read and write
Undefined is not a property, so remember not to set properties and read properties for undefined.
Deletion of properties
Delete keyword, delete is used to delete the object's properties and variables. Below is a demonstration of its usage, by the way, a simple description of what the state of the property variables can be deleted, when not.
var object = {
X:1,
Y:2
};
Delete object.x; True
Delete object["Y"]; True
document.write (Object.x); Undefined
document.write (OBJECT.Y); Undefined
Above is the successful deletion of the object by the Delete keyword property, but delete this method is not always very useful, here is the next new concept Dontdelete, which can be called a tag of an object, if an object is created by the time it holds this tag, Then it is not allowed to be deleted.
Attribute tag condition Holding Dontdelete
Explicitly declare a variable (global, local), function, object, all holding Dontdelete
var variable = 1; An explicitly declared global variable
Delete variable; False
function func () {}; Explicitly declared functions
Delete func; False
local = 5; Implicitly set a global property
Delete local; True
The built-in object is automatically held Dontdelete tags, such as the function of the arguments array object, as a parameter array of built-in objects, is not allowed to be deleted.
Variables declared through eval () can be deleted because the variables declared in eval do not hold the dontdelete tag when they are created.
Eval (' var variable = 3; ');
Variable 3
Delete variable; True
To sum up, I have a little personal understanding, after the wrong with my understanding will come back to revise, after all, writing is not to let people overturn the discussion, I think the delete feature is in the non-eval case can only delete a property of an existing object, and can not delete a piece of open memory space, An explicit declaration of a variable is actually a memory for a variable, functions, objects are so, so these can not be deleted, and the object's properties, implicit global variables (in fact, the context of this property), these are an object properties, so they can be deleted. Eval is the dynamic characteristics of JS, similar to the OC runtime seems to have not studied too deep, so it is not discussed in depth, knowledge is limited.
Detection of attributes
To know whether an object has a property, there are several ways to detect it, and the following is broken down.
Inch
If you want to simply determine whether an object and its prototype chain have a property, you only need to use the in operator, whose property is to return true whether it is held by itself or by the prototype chain.
var person = {
Name: "Jianweiwang",
Age: "23",
};
' name ' in person; True, self-holding
' Hobby ' in person; False, which does not hold itself
' toString ' in person; True, the prototype chain holds
The left side of the in operator is usually a string, and the right side must be an object.
hasOwnProperty () method
This is only to determine if the current object has attributes, and it does not detect properties on the prototype chain. So if you need to accurately determine what properties the object is self-sustaining, choose to use the hasOwnProperty () method.
var person = {
Name: "Jianweiwang",
Age: "23",
};
Person.hasownproperty (' name '); True, self-holding
Person.hasownproperty (' toString '); False, the prototype chain holds
Understanding the properties of JavaScript objects operations