JS object-oriented usage examples

小云云
Release: 2018-03-06 14:06:02
Original
1380 people have browsed it

An object is a collection of properties and methods. What is object-oriented? Object-oriented is a programming idea and a concept. In js, object-oriented programming is implemented through a method called prototype.

  创建对象
Copy after login

Create objects through literals

Create a student object
{Attribute 1: value 1, attribute 2: value 2, attribute 3: value 3}
json data format: It is a data conversion format
01. It is in the form of a key-value pair
02. The object is saved in {}
03. The collection is saved in []
04. Data is separated by commas, and attributes and attribute values are separated by colons

  通过字面量来创建对象
Copy after login

Create multiple objects through constructors

  通过构造函数创建多个对象
Copy after login

Prototype objects

Note point:
01. All objects have a constructor attribute! Points to the constructor!
02. When we create a function, the function will have a prototype attribute,
This attribute points to the prototype object created through the constructor! Student.prototype
03. A prototype object is an object in memory that provides shared properties and methods for other objects!
04.The prototype attribute is only available to functions!
05. Each object has a__proto__attribute, which points to the prototype object!

  原型对象
Copy after login

01. The prototype attribute is only available to functions!
02. Each object has a__proto__attribute, which points to the prototype object!

  深入原型对象
Copy after login

Prototype chain

Prototype chain:
01. A prototype object is an instance of another prototype object! A kitten is an example of an animal!
02. The related prototype chains progress layer by layer, forming a chain of instances and prototype objects. We call it the prototype chain!
The black cat squatting in the corner (example)
inherit
Cat class (01. Equivalent to a black cat, it is a prototype object 02. Compared to an animal, it is an instance)
inherit
Animal class (01. Prototype object of all animals 02. Instance of object)
inherit
Object (the top level of all prototype objects)
As long as it is an object, it has the__proto__attribute, pointing to the prototype object!
question:
Object is an object!
It has__proto__attribute!
The attribute value is null!

  原型链
Copy after login

Borrow constructor

  借用构造函数的同时传递参数
Copy after login

Combined inheritance

Combined inheritance:
Sometimes called pseudo-classical inheritance, it combines the techniques of prototype chaining and borrowing constructors,
An inheritance pattern that leverages the best of both worlds uses the prototype chain to inherit prototype properties and methods.
Inheriting instance attributes is achieved by borrowing constructors

  组合继承
Copy after login

Related recommendations:
Detailed explanation of js object-oriented inheritance knowledge

Detailed description of JS object-oriented programming

JavaScript object-oriented design_js object-oriented

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

Related labels:
source:php.cn
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!