Properties of the object (three attributes)

Source: Internet
Author: User

Object Basics

I. JavaScript data type: Object type (complex data type) string type (string type) number type (numeric type)
Boolean (Boolean type) Undefined (undefined type) null (NULL)

Two. Objects
1. Object concept: An object is a sequence of primitive data types or reference types (methods) that are stored as key-value pairs, each of which is called a property or method (behavior), and each property name must be unique.
Purpose of objects: Developers often use object data types, object data types to store data, or to create custom functions

2. Create a Simple Object
var person = {
Name: "Tom",
AGE:20,
30:10,
Sayname:function () {
Console.log ("I ' m" + this.name);
}
}
Value:
Console.log (Person.name); return Tom; Gets the value of the Name property
Console.log (person["30"]); Returns 10; Gets the value of this property 30

3. The property name can be a string or a number, if the property name is a number, then at the time of invocation (value), you need [] brackets, with "" double quotes to take the value
var person = {
10:15
30:35,
Sayname:function () {
Console.log ("I ' m" + this["10"]);
}
}
Value:
Console.log (person["10"]); Returns 15; Gets the value of the property "10"
Console.log (person["30"]); Returns 10; Gets the value of the property "30"

4. Raw data types and reference data types
1. The original data type is stored directly in a variable in the form of a value (self-understanding: Each variable is assigned a different memory area):
Example: var person = "Tom";
var other = person;
person = "Nicy";
Console.log (person); Back to "Nicy"
Console.log (other); Return to "Tom";
It is seen from the above: even if the value of person is modified, the value of other is the value stored previously.

2. The reference data type stores an address value (the address of the object's property) in a variable that points to the memory space of the object's properties:
Example: var person = {Name: "Tom"};
var other = person;
Person.name = "Nicy";
Console.log (Person.name); Back to "Nicy"
Console.log (Other.name); Back to "Nicy"
Explanation: Person/other Both variables store {name: "Tom"} The storage space for this object property, which is
Points to the same piece of memory area, which stores an anonymous object with the property of name.


Three. Attributes of an Object data type property

Object data type: Each data property has three other attributes (the initial values of these three attributes are true) in addition to the properties of the key-value pair.
To add an attribute to an object: Object.definproperty (person, "name", {
Configurable:true,//Set the value of the Name property of this object to be modifiable
Value: "Tom"
});
Note: When setting properties, the statements inside are separated by commas, and the last one is followed by a comma; the parentheses of the object are followed by a semicolon

1. Configurable (configurable): Indicates whether the property can be deleted; (set to True, can be deleted; set to False, cannot be deleted;),
To delete a property (such as the Name property): Delete Person.name

Example: Object.definproperty (person, "name", {
Configurable:true,//Set person The Name property of this object can be deleted
Value: "Tom"
});
Console (person.name);//Output "Tom"
Delete Person.name; Because configurable is set to True, the Name property can be deleted,
The delete Delete method is called, so the Name property is deleted.

Console (person.name);//Because the name attribute has been removed from the previous sentence, the person does not have the name attribute, so it is no longer printed.


2. Enumeration (enumerable): Indicates whether the property can be traversed in for/in; (set to True, the property can be traversed and returned;
Set to False, the property cannot be traversed, that is, is hidden), For/in's notation:
for (var x in person) {Console.log (x + ":" + person[x]);} ----This for/in is equivalent to the previously learned for Loop, X is the individual properties of the object to be traversed to, and to return the value of the property, the notation is person[x].

Example: Object.definproperty (person, "name", {
Enumerable:true,//Set person The Name property of this object can be returned in for/in
Value: "Tom"
});

Object.definproperty (person, ' age ', {
Enumerable:false,//Set person The age property of this object can no longer be returned in For/in (hiding the Age property)
Value:20
});

for (var X in person) {
Console.log (X + ":" + person[x]);
}
The output of the for/in is: Name: "Tom"
Because the enumerable value of the Age property is set to false, it means that the age property cannot be returned in for/in, which hides the age attribute.


3. Writable (writable): Indicates whether the property can be modified; (set to True, the value of the property can be modified; set to False, the value of the property cannot be modified)

Example: Object.definproperty (person, "name", {
Writable:true,//Setting the value of the Name property of this object can be modified
Value: "Tom"
});

Object.definproperty (person, ' age ', {
Writable:false,//Setting the value of the age property of this object cannot be modified
Value:20
});

Person.name = "Nicy";
Person.age = 30;
Console.log (Person.name); Returned "Nicy", the value of the Name property was modified successfully
Console.log (Person.age); Return, the value of the Age property is still the original value, has not been modified

Description: The writable of the Name property is set to True, so the value of the Name property can be modified;
The Age property's writable is set to false, so the value of the Age property is not sadly modified.



Properties of an object (three attributes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.