js object-oriented programming

hzc
Release: 2020-06-04 13:06:31
forward
2125 people have browsed it

1. Object-oriented programming

1. Process-oriented and object-oriented

## 1) Process-oriented: Focus on the process steps of how to solve a problem. The characteristic of programming is that each step of the process is implemented by functions, without the concept of classes and objects.

2) Object-oriented: Focus on which object solves the problem. The characteristic of programming is that a class appears, the object is obtained from the class, and this object solves the specific problem.

For the caller, process orientation requires the caller to implement various functions by himself. Object-oriented only requires the caller to understand the functions of specific methods in the object, but does not need to understand the implementation details of the method.

2. The three major characteristics of object-oriented

The three major characteristics of object-oriented are inheritance, encapsulation, and polymorphism. JS can simulate inheritance and encapsulation, but it cannot simulate polymorphism. Therefore, JS is an object-based language, rather than an object-oriented language in the traditional sense.


3. The relationship between classes and objects
## 1) Classes are abstract, objects are concrete (classes It is the abstraction of the object, and the object is the concretization of the class)


2) Class is an abstract concept. It can only be said that a class has attributes and methods, but it cannot be assigned specific attributes. For example, humans have names, but we cannot say what their names are.

2. Javascri

Creation method of pt object1.var obj = {}//Simple object plainObject object literal/object direct quantity

2. Constructor (feature: camel case naming rule)

1) The system’s own constructor

var obj = new Object();//Equivalent to var object = {}

2) Custom function encapsulation

// Only by adding parameters to the function can the function be customized, and you can change the corresponding functions in the function at will Parameter data must be a new object when producing an object

function Car(color){ this.color=color; this.name = "BMW"; this.height = "1400"; this.lang = "4900"; this.weight = 1000; this.health = 100; this.run = function(){ this.health -- ; } }
Copy after login

var car = new Car("red");

3. 3 steps of the internal principle of the object constructor

1) Implicitly add an object at the top of the function body: var this = {}

2) Execute the content in the function body

3) Implicitly return the this object

##3. Packaging class

1. It is impossible for original values to have properties and methods (undefined, null, number, boolean, string)

2. Some original values can be called after passing through the packaging class.

var num=123;//Not an object

var num1=new Number(123);//It is an object

3. The process of implicit packaging class is as follows

var num = 4; num.len = 3;//隐式发生转换,新建一个数字对象,然后添加属性并赋值,最后删除这个对象,所以在执行过程中没有报错 console.log(num.len);//再新建一个数字对象,添加属性,所以最后输出为undefined var str="abcd"; str.length=2; console.log(str.length); //输出为4,在输出时原始值str包装类之后输出的为new String('abcd').length,字符串自带length属性,所以输出长度为4
Copy after login
Recommended tutorial:

"JS Tutorial"

The above is the detailed content of js object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!