Detailed interpretation of Js OOP programming to create objects
Below I will bring you some comprehensive understanding of Js OOP programming to create objects. Let me share it with you now and give it as a reference for everyone.
Object-oriented is a method of understanding and abstracting the real world. It is the product of the development of computer programming technology to a certain stage.
The meaning of the object
The object can be a car, a person, an animal, a text, a form or anything that exists, etc.
Objects have:
Attributes -------Some specific properties of the object.
Method-------What the object can do.
Events ------- can respond to things that happen to objects.
We can understand object-oriented by creating a human object
People:
Two hands, two feet, one head, and Can run.
Hands, feet, and head are human attributes, and running is human method.
First, let’s create an object in the simplest way
var person = { head: "one", hand: "two", foot: "two", run : function(){ console.log("running"); } }
This method is not practical at all, because it creates a separate object, and this object is different from any common data The structure has no connection.
Then, we create an object using the constructor function
var Person = function(){//注意,首字母大写 this.head = "one", this.hand = "two", this.foot = "two", this.run = function(){ alert("running"); } } var Joan = new Person(); document.write(Joan.run())// "running"
This is the object created using the constructor function, and then we add a line of code to see
var Niki = new Person(); alert(Joan==Niki) //false;
Yes, two different object instances are now created.
Every function in JavaScript has a prototype attribute. If a function is used as a constructor, this attribute will automatically be called through new to create the prototype of the object
console.log(Joan)
You can see that there is a __proto__:Person, where __proto__ is the prototype chain of Joan. It points to the prototype of Person.
When JS creates an object (whether it is a normal object or a function object), There is a built-in property called __proto__ that points to the prototype object prototype of the function object that created it.
Some understandings of the prototype chain are written in great detail in the book JavaScript Advanced Programming. If you are interested, you can check it out. There are also pdf documents available online. However, it is recommended to buy this book and support the original version.
Then any changes to the prototype attribute of prototype can be applied to every instance object constructed with new Person(), regardless of whether it is created before or after the change. Add a new function for Person.prototype. Specifically As follows:
var Person = function(){//注意,首字母大写 this.head = "one", this.hand = "two", this.foot = "two" } Person.prototype.run = function(){ alert("running"); } var Joan = new Person(); Joan.run()// "running" alert(Joan.__proto__===Person.prototype)//'true'
You can see that the method created in the prototype can be called, and Joan's prototype chain points to the prototype of Person.
Look again:
var Niki = new Person();//"runing" Person.prototype.run = function(){ alert("running running") } Joan.run()//"running running" Niki.run()//"running running"
Look, modify the prototype method of Person. All methods in the object instances created by new Person() have been modified, because the same is shared by all instances. A prototype method run. This is an application of prototypes.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Details for you Interpretation of JavaScript character set encoding and decoding (graphic tutorial)
Native js implements holiday time countdown function (code attached)
The above is the detailed content of Detailed interpretation of Js OOP programming to create objects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to display file suffix under Win11 system? Detailed explanation: In the Windows 11 operating system, the file suffix refers to the dot after the file name and the characters after it, which is used to indicate the type of file. By default, the Windows 11 system hides the suffix of the file, so that you can only see the name of the file in the file explorer but cannot intuitively understand the file type. However, for some users, displaying file suffixes is necessary because it helps them better identify file types and perform related operations.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

With the continuous development of the Internet, people are increasingly inseparable from browsers. In browsers, everyone will use cookies more or less. However, many people don’t know which folder the cookie data is in. Let’s explain it in detail today. First, we need to understand what cookies are. Simply put, a cookie is a piece of text information stored by the browser, which is used to save some of the user's personal settings in the browser or record the user's historical operations, etc. When the user opens the same website again, c

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
