JavaScript Basics Objects

JavaScript Basics Objects

1. Object

1.1 What is an object?

In JavaScript, an object is an unordered collection of related properties and methods. Everything is an object, such as strings, numbers, arrays, functions, etc.

Objects are composed of properties and methods

  • Attribute: The characteristics of an object, represented by attributes in the object
  • Method: The behavior of things, represented by methods in objects

1.2 Why do we need objects?

To store a value, you use a variable; to store a set of values, you use an array. What if you want to save a person's complete information?

For example, the way to save Zhang San's personal information in an array is:

var arr = ['张三','男',123,156];

It is clearer to save Zhang San's personal information in the form of an object.

2. Three ways to create objects

2.1 Creating objects using object literals {}

Object literal: { } contains the properties and methods of the (object) that expresses this specific thing.

    <script>
        //Use object literals to create objects {}
        var obj = {}; //Creates an empty object var obj = {
            uname: '张三',
            age: 18,
            sex: 'male',
            sayhi: function () {
                console.log('hi');
            }
        };
        //(1) The properties or methods inside are in the form of key-value pairs: key property name: value property value //(2) Multiple properties or methods are separated by commas //(3) The method is followed by an anonymous function //2. Using objects //(1) To call the properties of an object, we use the object.property name method console.log(obj.uname);
        //(2) Call the object's attribute object name ['attribute name']
        console.log(obj['age']);
        //(3) Call the method of the object object name.method name obj.sayhi();
    </script>

2.2 Creating an object using new Object

  // //Use new Object to create an object var obj = new Object(); //Create an empty object obj.uname = '张三';
        obj.age = 18;
        obj.sex = 'male';
        obj.sayhi = function () {
            console.log('hi~');
        }
        console.log(obj['uname']);
        console.log(obj.sex);
        obj.sayhi();
        //(1) We use the equal sign = assignment method to add object properties and methods //(2) Use ; between each property and method to end //Case var Object = new Object();
        Object.uname = 'Naruto';
        Object.sex = 'male';
        Object.age = 19;
        Object.skill = function () {
            console.log('Shadow Clone Technique');
        }
        console.log(Object.uname);
        Object.skill();

2.3 Creating objects using constructors

 //Why do we need to use a constructor? //Because our first two ways of creating objects can only create one object at a time. //Because we create an object at a time, many of the properties and methods in it are the same. Use functions to reuse code. This function is called a constructor. //The constructor encapsulates the object. //The constructor abstracts some of the same properties and methods in our object and encapsulates them in the function.

Constructor : A special function that is mainly used to initialize objects, that is, to assign initial values ​​to object member variables. It is always used with the new operator. We can abstract some common properties and methods in the object and encapsulate them into this function.

    <script>
 
        //Use the constructor to create an object //Constructor syntax // function constructor name() {
        // this.property = value;
        // this.method = function() {}
        // }
        // new constructor name();
 
        function Star(uname, age, sex) {
            this.name = uname;
            this.age = age;
            this.sex = sex;
            this.sing = function (song) {
                console.log(song);
            }
        }
        var ldh = new Star('Andy Lau', 18, 'Male'); //The function call returns an object console.log(typeof ldh);
        console.log(ldh.name);
        console.log(ldh['sex']);
        ldh.sing('Ice Rain');
        var zxy = new Star('Jacky Cheung', 36, 'Male');
        console.log(zxy.name);
        console.log(zxy['sex']);
        zxy.sing('Li Xianglan');
            //1. The first letter of the constructor name should be capitalized //2. Our constructor can return results without return //3. We must use new to call the constructor
            //4. We just need to call the new Srart() function to create an object //5. Our properties and methods must be preceded by this
   </script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • A detailed introduction to JavaScript primitive values ​​and wrapper objects
  • Analysis of JavaScript primitive values ​​and object reference examples
  • Introduction to JavaScript built-in objects
  • Detailed explanation of JavaScript object conversion to primitive value

<<:  Basic operations on invisible columns in MySQL 8.0

>>:  The effect of CSS dynamic gradient border rotating around the content area (example code)

Recommend

JavaScript canvas to achieve raindrop effects

This article example shares the specific code of ...

Several ways to introduce pictures in react projects

The img tag introduces the image Because react ac...

Docker enables multiple port mapping commands

as follows: docker run -d -p 5000:23 -p 5001:22 -...

Detailed explanation of how to quickly build a blog website using Docker

Table of contents 1. Preparation 2. Deployment Pr...

Dynamic SQL statement analysis in Mybatis

This article mainly introduces the dynamic SQL st...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

HTML page header code is completely clear

All the following codes are between <head>.....

A brief discussion on how to elegantly delete large tables in MySQL

Table of contents 1. Truncate operation 1.1 What ...

Steps for IDEA to integrate Docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

Detailed explanation of CSS multiple three-column adaptive layout implementation

Preface In order to follow the conventional WEB l...

Application nesting of HTML ul unordered tables

Application nesting of unordered lists Copy code T...