JS advanced capture exception, for-in loop, object-oriented and prototype

5 catch exception

The try/catch/finally statement is used to handle error messages that may appear in the code.
The try statement defines a block of code that is tested for errors when executed.
The catch statement allows us to define a block of code that is executed when an error occurs in the try block.
The finally statement is executed after the try and catch regardless of whether there is an exception.

        try {
    
    
            try {
    
    
                var str = undefined;
                var result = str.trim();
            } catch (ex) {
    
    
                console.error("内层:", ex);
            } finally {
    
    
                console.log("finally");
            }
        } catch (ex) {
    
    
            console.error(ex);
        } finally {
    
    
            console.log("外层的finally");
        }

6 for-in loop

Function: loop object or array

grammar:for (变量名 in 数组名/对象名){}

The for- in loop will also loop the properties on the prototype chain, but it will not loop when enumerable is false

var obj = {
    
     x: 1, y: 2 };
Object.defineProperty(obj, 'z', {
    
    
	value: 3,
    enumerable: true // 可枚举
});
for (var key in obj) {
    
    
    console.log(key); // x y z
	console.log(obj[key]); // 1. 2. 3
}

7 object-oriented

​Object-oriented programming (abbreviation: OOP) is a programming paradigm with object concepts, and it is also an abstract policy for program development. It may contain data, properties, and methods. Objects are instances of classes . It regards the object as the basic unit of the program, and encapsulates the program and data in it to improve the reusability, flexibility and scalability of the software. The program in the object can access the data related to the object.

7.1 Understanding Object Orientation

Object-oriented is a kind of development thought .

Process-oriented : In the development process, it is a development method that focuses on the process. During development, it pays attention to every detail, step and sequence.

Advantages and disadvantages: better performance, but due to the large consumption of instantiation during debugging, it is not suitable for maintenance, difficult to reuse and difficult to expand

Object-oriented : In the development process, we only need to find an object to help us complete things.

Advantages and disadvantages: easy to maintain, easy to reuse, easy to expand, because object-oriented has the characteristics of encapsulation, inheritance, and polymorphism, it is possible to design a low-coupling system

classes and objects :

All things in js are objects, strings, Boolean values, numbers, functions, etc. are all objects.

Class: It is a collection with the same properties and methods, and the class is abstract

Object: It is a concrete instance, and the object actually exists

7.2 Methods of creating objects

1. Objects are created literally, and attributes and methods can be added dynamically later, but they cannot be mass-produced

	var obj = {
    
    
            name: "Alice", age: 18, show: function () {
    
    
                console.log("我叫。。。。。。");
            }
        };

        obj.gender = "男";
        obj.run = function () {
    
    
            console.log("我能跑!!");
        }

2. Objects are created by means of built-in constructors, and properties and methods can be dynamically added later

	var obj3 = new Object();
        obj3.name = "Alice";
        obj3.age = 18;
        obj3.show = function () {
    
    
            console.log("我叫。。。。。。");
        }

3. The factory function creates the object

function factory(name,age){
    
    
	var obj =();
	obj.name="张三";
	obj,age=18;
	obj.show=function(){
    
    
	console.log("展示")
	}
	return obj;
}

4. To create an object with a custom constructor, it needs to be used in conjunction with the new keyword, and the function name uses the big hump nomenclature

function Factory(name,age){
    
    
    this.name=name;
    this.age=age;
    this.show=function(){
    
    
    	console.log("展示")
    }
}
var obj1 = new Factory("李四",17);

8 Prototypes and prototype chains

8.1 Prototype

The problem solved by the prototype: When you want to add a method to the instance object, it is not good to write it directly in the function body. In order to solve this problem, we write the method on the prototype, and only create one method in memory for the object experimenting

concept:

Each function has a prototype property, which is an object

Each object has a __proto__ attribute, which points to the prototype of the constructor

When accessing the properties or methods of an object, first search on itself, if not, it will go to __proto__ to find

Function: Add a method to the prototype, and only one method will be created in memory, which is specially used by the object

8.2 Prototype chain

Prototype chain: Use the __proto__ attribute to concatenate the object chain structure

Guess you like

Origin blog.csdn.net/weixin_54026054/article/details/128987593