


Detailed explanation of five common ways to create objects in Javascript
Five common ways to create objects in Javascript:
There are seven ways to create objects. Here are examples of the five common ways, among which There are two methods that are frequently used, please be sure to master them:
1. Directly create object method
The creation syntax is as follows:
var 对象变量名 = new Object(); 对象变量名. property1 = value1; …; 对象变量名. propertyN = valueN; 对象变量名. methodName1 = function([参数列表]){ //函数体} …; 对象变量名. methodNameN = function([参数列表]){ //函数体}
Create an object first , define and set the values of the properties and functions
Reference examples and calls are as follows:
var student =new Object();student.name="小李";student.age=21;student.dohomework=function(){ console.log(this.name+"正在做作业");} console.log(student.name);student.dohomework();
2. Object initializer method
The creation syntax is as follows:
var 对象变量名 = { property1 : value1, property2 : value2, …, propertyN : valueN, methodName1 : function([parameter_list]){ //函数体 }, …, methodNameN : function([parameter_list]){ //函数体 } }
Directly assign values to the properties of the object and define function functions within the curly brackets
See the reference examples and calls below:
var teacher = { name:"Mr Li", age:21, teach:function(){ console.log(this.name+"正在授课ing"); } } teacher.teach();
Note : Different attribute values are directly separated by commas, and there is usually no punctuation mark after the last value
3. Constructor method
The creation syntax is as follows:
function 构造函数([参数列表]){ this.属性 = 属性值; … this.属性N = 属性值N; this.函数1 = method1; … this.函数N = methodN; }function method1([参数列表]){ //函数体} …function methodN([参数列表]){ //函数体}
or
function 构造函数([参数列表]){ this.属性 = 属性值; … this.属性N = 属性值N; this.函数1 = function([参数列表]){ //函数体 } ; … this.函数N = function([参数列表]){ //函数体 } ; }
Note: Some values can be set to default values, and some values can be given in the parameter list as needed and assigned directly
Summary:
1. Compared with the above two methods, using the constructor method to create objects can effectively save code;
2. Using the constructor method to create objects, this cannot be omitted, which is the difference between ordinary functions;
3 , use the constructor method to create objects, the method on the left is more preferable, and improves code reuse;
Reference examples and calls are shown below:
function Student(name){ this.name=name; this.age=21; this.play=function(){ console.log(this.name+"正在玩"); } }var student=new Student("仔仔"); student.play();
4.prototype prototype method
The creation syntax is as follows:
function 对象构造器( ){} 对象构造器.prototype.属性名=属性值; 对象构造器.prototype.函数名 = function([参数列表]){ //函数体}
After declaring a new function, the function (in JavaScript, the function is also an object) will have a prototype attribute, through which you can Add new properties and methods to objects.
Reference examples and calls are as follows:
function Student(){} Student.prototype.name="仔仔"; Student.prototype.age=21; Student.prototype.dohomework=function(){ console.log(this.name+"正在做作业"); }var student=new Student(); student.dohomework();
5. Mixed constructor/prototype method
The creation syntax is as follows:
function 对象构造器([参数列表]){} 对象构造器.prototype.函数名 = function([参数列表]){ //函数体}
The constructor method is convenient for dynamically assigning values to properties, but this method also defines the method in the constructor method body, making the code more complicated; while the prototype method is not convenient for dynamically assigning values to properties, but the properties and methods defined in this way are Separation is achieved; so learn from each other's strengths and weaknesses - constructors define properties and prototypes define methods.
See reference examples and calls below:
function Student(name){ this.name=name; this.age=21; } Student.prototype.dohomework=function(){ console.log(this.name+"正在做作业"); }var student=new Student("仔仔"); student.dohomework();
There are two other methods. Dynamic prototype method and factory mode are not common, so I won’t explain them here. Thank you for watching my work!
Related recommendations:
#4 ways to create objects in JavaScript
Illustration Seven ways to create objects in javascript
The above is the detailed content of Detailed explanation of five common ways to create objects in Javascript. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools