A summary of the concept of JavaScript objects

Source: Internet
Author: User

Original

Https://www.jianshu.com/p/88213b499c4b

Outline

Objective
1. Related Concepts of objects
2. Creation of objects (simple creation)
3. Properties of an Object
3.1. Data properties
3.2. Accessor properties
4. Setting and Reading properties in an object
5. A variable stores a reference to an object
6. Basic data types can be automatically converted into object data types
7. The difference between the point method and the Bracket method to get the object properties

Objective

The following discussion is my understanding and understanding of the object's knowledge points, including what is the object? The basic way of creating objects and the related concepts of objects are explained, hoping to bring some help to the readers.

1. Related concepts of objects 1, ECMAScript is Object-based, the object is a collection of attributes (properties), and each property has 0 or more attributes (attributes), which determines how this property is used.
2, ECMAScript through the object to determine where the caller is to call, by invoking the object's properties and methods can be used to satisfy the caller how to invoke the object.
3. ECMAScript definition of an object: A collection of unordered attributes whose properties can contain basic values, objects, or functions.
4, the object is a collection of properties, and has a prototype object. The prototype can be a null value. Strictly speaking, this is equivalent to saying that the object is a set of values that do not have a particular order.
5. Each property or method of an object has a name, and each name is mapped to a value. Because of this, we can think of the ECMAScript object as a hash table: nothing more than a set of name-value pairs, where values can be data or functions.
6. JavaScript itself contains many objects, which are called built-in objects of JavaScript.
7, the browser itself is a model of the object collection, you can manipulate the browser object, browser object model (BOM), Document Object Model (DOM)
8. Object exists to solve the limitations of basic data types
9, the object is a self-contained data collection, the data contained in the object can be accessed in two forms-the property and method: A: A property is a variable that belongs to a particular object, and B: a function that only a particular object can call.
10, the instance is the concrete manifestation of the object: The object is collectively, the instance is an individual.
11. Some objects (e.g., person) do not exist in the JavaScript language. We can use the JavaScript language to create our own objects-the term is called user-defined objects (User-defined object). Some objects, such as arrays, Date, are pre-defined objects in the JavaScript language, and we can use these objects directly in our own scripts, which people refer to as built-in objects (native object).

2. Creation of objects (simple creation)
1. Objectvar person = new Objcet ();p erson.name = "Nicholas";p erson.age = 29;person.job = "Software Engineer";p Erson.sayn ame = function () {    console.log (this.name);} 2. Object literal var person = {    Name: "Nicholas";    age:29,    job: "Software Engineer",    sayname:function () {        console.log (this.name);    }}
3. Properties of an Object

There are two types of properties in ECMAScript: Data properties and accessor properties

3.1. Data properties
/* The    Data property contains the location of a data value, where the value can be read and written */var person = {    Name: "Nicholas"};>/action Data Properties *    /* Manipulate data properties via Delete, add    assigning an undeclared data property is equivalent to creating this property and then assigning the property a value of */' use strice ' var person = {    name: ' PersonName ',    age:23}delete Person.age;person.type = ' Chinese '; Console.log (person);//{name: "PersonName", type: "Chinese"}>/ Through Object.defineproperty ()//*    can be      object.defineproperty (ObjectName, Attritutename,                            {descriptor:true/ False});    To manipulate the object's properties */' use strice ' var person = {    name: ' PersonName ',    age:23}console.log (person);//{name: "PersonName", Age:23}person.name = "hh"; Console.log (person);//{name: "hh", age:23}//object.defineproperty (person, "name", {//     writable:false//}) Standard format object.defineproperty (person, "name", {    writable:false,    value: "KK"}) Console.log (person);//{name: "KK", Age:23}person.name = "GG"; Console.log (person);//{name: "KK", age:23}
3.2. Accessor properties
    /* Accessor properties do not contain data values: they contain a pair of child getter and setter functions (although neither of these functions are required).            in the read accessor property, the getter function is called, which is responsible for returning valid values, and        when writing accessor properties, the setter function is called and the new value is passed, which is responsible for deciding how to handle the data */>/action accessor properties//accessor properties: yyy ' Use Strice ' var book = {    _year:2004,    edition:1        };console.log [book], Object.defineproperty (book, "yyy", { C6/>get:function () {        return this._year;    },    set:function (newvalue) {        if (NewValue > 2004) {            This.edition + = Newvalue-this._year;            This._year = newvalue;}}    ); Console.log (book.yyy); book.yyy = 2008;console.log (book.yyy); Console.log (book);
4. Setting and Reading properties in an object
1. Set properties of a property Object.defineproperty (ObjectName, Attritutename,                       {descriptor:true/false});//2, Set properties for multiple properties Object.defineproperties (ObjectName, {attritutename: {descriptor:true/false}, Attritutename: {descriptor:t Rue/false});//3, read attribute var descriptors = Object.getownpropertydescriptor (objectname,attritutename); Console.log ( Descriptors.descriptorname);
5. A variable stores a reference to an object

1. When we create an object and assign the created object to a variable declared with VAR, this variable stores the address of the object, which is the object's reference, not the object's own assignment.
2, because the variable is stored in the object's reference, so some seemingly is the variable ' object ' operation, in fact, the variable contains a reference to the object pointed to the operation, rather than the operation of the variable, because this variable is not an object, but an address reference.

6. Basic data types can be automatically converted into object data types

1, for some basic data types, in fact, they also have the corresponding object type.
2, when some data can indicate what kind of basic data type, when called to the basic data type corresponding to the object data type, JavaScript will automatically convert the base data type to the object data type.

7. The difference between the point method and the Bracket method to get the object properties
1, the Bracket method can use the variable as the property name, and the point method can not; var obj = {};obj.name = ' Zhang San '; var myName = ' name '; Console.log (obj.myname);//undefined, No access to the corresponding attribute Console.log (Obj[myname]);//Zhang San//2, the Bracket method can be used as the name of the attribute, and the point syntax is not available,//3, the square bracket method can use the JS keyword and reserved word as the property name, and point syntax is not ( Try to avoid using keywords or reserved words in variables or attributes);

  

A summary of the concept of JavaScript objects

Related Article

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.