


What are JavaScript objects? Detailed explanation of how to create object code
What is an object
According to the definition of JavaScript, an object is a collection of unordered properties, and its properties can contain basic values, objects or functions. That is to say, an object is a set of attributes in no particular order. Each attribute is mapped to a value, which is a set of key-value pairs. The value can be data or an object.
The simplest object
A pair of curly brackets {} in JavaScript can define an object. This way of writing is actually the same as calling the constructor of Object
var obj={}; var obj2=new Object();
It is constructed in this way The object only contains a pointer to the prototype of the Object. You can use some methods such as valueOf and hasQwnProperty, which have little practical effect. Custom objects always have some custom properties and methods.
var obj={}; obj.a=0; obj.fn=function(){ alert(this); } var obj2={ a:0, fn:function(){ alert(this); } }
You can add properties and methods to it through "." after defining the object, or you can use the literal assignment method to add properties and methods to it when defining the object. The object created in this way has its methods. And attributes can directly use object references, similar to static variables and static functions of classes. There is an obvious flaw in creating objects in this way - it is very laborious to define a large number of objects, and it is necessary to write almost repetitive code over and over again.
It’s time for function to appear again. In JavaScript, function is an object. When creating an object, you can discard the createObj method above and directly use function as an object. How to achieve reuse? This lies in function as an object. The particularity of the object.
1. function can accept parameters, and can create objects of the same type and different values according to the parameters
2. When function is used as a constructor (called through the new operator), it will return an object. Some basic knowledge about constructors has been mentioned in the poor and lower-middle peasant version of jQuery. Let’s simply copy it.
The return value of the constructor is divided into two situations. When the function has no return statement or returns a basic type (bool, int, string, undefined, null), return an anonymous object created by new, which is the function instance; if the function body returns a reference type object (Array, Function, Object, etc.), the object will overwrite the creation of new An anonymous object as the return value.
3. So how to use function to solve the problem of type identification? Each function instance object will have a constructor attribute (not "has", but can correspond). This attribute can indicate who its constructor is. , you can also use the instanceof operator to determine whether the object is an instance of XXX.
The code is as follows:
function Person(name){ this.name=name; this.fn=function(){ alert(this.name); } } var person1=new Person('Byron'); console.log(person1.constructor==Person);//true console.log(person1 instanceof Person); //true
This would be perfect, no! Although the constructor can be object-specific, the methods must be repeated in every instance of the object!
function Person(name){ this.name=name; this.fn=function(){ alert(this.name); } } var person1=new Person('Byron'); var person2=new Person('Frank'); console.log(person1.fn==person2.fn);//false
Look, although the fn of the two instances are exactly the same, they are not the same thing. If a function object has a thousand methods, then each of its instances must contain these methods. copy, it makes the memory speechless.
Is there a nearly perfect way to construct an object that does not require repeated work, is stylish, and does not need to repeat the common methods of objects? In fact, it can be found that using function is already close to the requirements and is just a little short of it - it requires a container shared by all instances of the function object, and the shared attributes and methods that need to be shared by the instance in this container happen to be ready-made - —prototype, students who don’t know prototype can look at JavaScript prototype
function Person(name){ this.name=name; } Person.prototype.share=[]; Person.prototype.printName=function(){ alert(this.name); } var person1=new Person('Byron'); var person2=new Person('Frank'); console.log(person1.printName==person2.printName);//true
In this way, each instance of Person has its own attribute name, and also has the attribute share and method printName shared by all instances. The basic problems are solved. , for general object processing, you can always use this stylish and loving object creation mode.
The above is the detailed content of What are JavaScript objects? Detailed explanation of how to create object code. 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



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Title: Realme Phone Beginner’s Guide: How to Create Folders on Realme Phone? In today's society, mobile phones have become an indispensable tool in people's lives. As a popular smartphone brand, Realme Phone is loved by users for its simple and practical operating system. In the process of using Realme phones, many people may encounter situations where they need to organize files and applications on their phones, and creating folders is an effective way. This article will introduce how to create folders on Realme phones to help users better manage their phone content. No.

This article will interest you if you are interested in using GIMP for pixel art creation on Windows. GIMP is a well-known graphics editing software that is not only free and open source, but also helps users create beautiful images and designs easily. In addition to being suitable for beginners and professional designers alike, GIMP can also be used to create pixel art, a form of digital art that utilizes pixels as the only building blocks for drawing and creating. How to Create Pixel Art in GIMP Here are the main steps to create pixel pictures using GIMP on a Windows PC: Download and install GIMP, then launch the application. Create a new image. Resize width and height. Select the pencil tool. Set the brush type to pixels. set up

Many friends expressed that they want to know how to create a family in Gree+ software. Here is the operation method for you. Friends who want to know more, come and take a look with me. First, open the Gree+ software on your mobile phone and log in. Then, in the options bar at the bottom of the page, click the "My" option on the far right to enter the personal account page. 2. After coming to my page, there is a "Create Family" option under "Family". After finding it, click on it to enter. 3. Next jump to the page to create a family, enter the family name to be set in the input box according to the prompts, and click the "Save" button in the upper right corner after entering it. 4. Finally, a "save successfully" prompt will pop up at the bottom of the page, indicating that the family has been successfully created.

In iOS17, Apple has added a contact poster feature to its commonly used Phone and Contacts apps. This feature allows users to set personalized posters for each contact, making the address book more visual and personal. Contact posters can help users identify and locate specific contacts more quickly, improving user experience. Through this feature, users can add specific pictures or logos to each contact according to their preferences and needs, making the address book interface more vivid. Apple in iOS17 provides iPhone users with a novel way to express themselves, and added a personalizable contact poster. The Contact Poster feature allows you to display unique, personalized content when calling other iPhone users. you

Start the journey of Django project: start from the command line and create your first Django project. Django is a powerful and flexible web application framework. It is based on Python and provides many tools and functions needed to develop web applications. This article will lead you to create your first Django project starting from the command line. Before starting, make sure you have Python and Django installed. Step 1: Create the project directory First, open the command line window and create a new directory

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

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
