


Analysis and comparison of js objects and print objects_javascript skills
JS object introduction:
1. Basic concepts
1, Custom object.
According to the object extension mechanism of JS, users can customize JS objects, which is similar to the Java language.
Corresponding to custom objects are JS standard objects, such as Date, Array, Math, etc.
2. Prototype
In JS, this is a way to create object properties and methods. New properties and methods can be added to objects through prototype.
Through prototype we can add new properties and methods to JS standard objects, for example, for String objects, we can add a new method trim().
Unlike strict programming languages such as Java, we can dynamically add new properties to JS objects during runtime.
2. Grammar rules
1, object creation method
1) Object initializer method
Format: objectName = {property1:value1, property2:value2,…, propertyN:valueN}
property is the property of the object
value is the value of the object. The value can be a string, a number or an object. One of them
For example: var user={name:“user1”,age:18};
var user={name:“user1”,job:{salary:3000,title:programmer}
The method of the object can also be initialized in this way, for example:
var user={name:"user1",age:18,getName:function(){
> }
The following will focus on the constructor method, including the definition of attributes and methods, etc., and also explain the constructor method.
2) Constructor method
Write a constructor and create the object through new. The constructor can have construction parameters
For example:this.age=age;
this.canFly= false;
}
var use=new User();
2, define object properties
1) Three types of properties can be defined for objects in JS: private properties, instance properties and class properties. Similar to Java, private properties can only be used inside the object, and instance properties must be referenced through the instance of the object. Class attributes can be referenced directly by the class name.
2) Private property definition
Private properties can only be defined and used inside the constructor.Syntax format: var propertyName=value;
For example:
var isChild=age this.isLittleChild=isChild;
}
var user=new User (15);
alert(user.isLittleChild);//The correct way
alert(user.isChild);//Error: The object does not support this property or method
3 ) instance attribute definition, there are also two ways:
this method, syntax format: this.propertyName=value, pay attention to the position of this in the following examples
The value above can be characters, numbers and objects.
For example:
User.prototype.age=18;
var user=new User();
alert(user.age);
———— ——————————–
function User(name,age,job){
this.name="user1";
this.age=18;
this.job =job;
}
alert(user.age);
3) Class attribute definition
For example:
User.MIN_AGE=0;
alert(User.MAX_AGE);
Refer to the class attributes of JS standard objects:
Number.MAX_VALUE //Maximum numerical value Math.PI //Pi
4) For the definition of attributes, in addition to the more formal way above, there is also a very special way of definition, the syntax format: obj[index]=value
Example:
function User(name){
this.name=name;
this.age =18;
this[1]=“ok”;
this[200]=“year”;
}
var user=new User(“user1”);
alert( user[1]);
In the above example, please note: the age attribute cannot be obtained through this[1], and the name attribute cannot be obtained through this[0], that is, through the index method Those defined must be referenced using the index method, and those defined without the index method must be referenced in the normal way
3. Define object methods
1) Three types of methods can be defined for objects in JS: private methods, instance methods and class methods, similar to Java:
Private methods can only be used inside the object
Instance methods must be in the object instance
Class methods can be used directly through the class name.
Note: The method cannot be defined through the index method mentioned above.
2) Define private methods
Private methods must be defined within the constructor body and can only be used within the constructor body.
Syntax format: function methodName(arg1,…,argN){ }
For example:
function User(name){
this.name=name; This .nameLength=getNameLength(this.name);
}
3) Define instance methods. Currently, you can use two methods:
prototype method, used outside the constructor, syntax Format:
functionName.prototype.methodName=method;
functionName.prototype.methodName=function(arg1,…,argN){};
This method is used inside the constructor, syntax Format:
this.methodName=method;
or
this.methodName=function(arg1,…,argN){};
In the above syntax description, method is an external method that already exists , the method of the object to be defined by methodName means directly assigning an external method to a certain method of the object.
Defining object methods in the form of function(arg1,…,argN){} is what developers should master.
Some examples of defining instance methods: Example 1
Copy the code
}
function getUserName(){
return this .name;
}
Function setUserName(name){
this.name=name;
}
Some examples of defining instance methods: Example 2
Copy code
};
this.setName=function(newName){
this.name=newName;
};
}
Some examples of defining instance methods: Example 3
Copy code
function User(name){
this.name=name;
}
User.prototype.getName=getUserName;
User.prototype.setName=setUserName();
function getUserName(){
return this.name;
}
Function setUserName(name){
this.name=name;
}
Some examples of defining instance methods: Example 4
function User( name){
this.name=name;
}
User.prototype.getName=function(){
return this.name;
};
User.prototype.setName =function(newName){
this.name=newName;
};
4) Define class methods
Class methods need to be defined outside the constructor and can be directly constructed through The function name refers to it.
Syntax format:
functionName.methodName=method;
or
functionName.methodName=function(arg1,…,argN){};
Example:
function User(name){
this.name=name;
}
User.getMaxAge=getUserMaxAge;
function getUserMaxAge(){
return 200;
}
or
User.getMaxAge=function(){return 200;};
alert (User.getMaxAge());
4, references to properties and methods
1) In terms of visibility:
Private properties and methods can only be referenced within the object.
Instance properties and methods can be used anywhere, but must be referenced through objects.
Class attributes and methods can be used anywhere, but cannot be referenced through instances of objects (this is different from Java, where static members can be accessed through instances).
2) From the object level:
is similar to Java bean references and can be referenced in depth.
Several ways:
Simple properties: obj.propertyName
Object properties: obj.innerObj.propertyName
Index properties: obj.propertyName[index]
For deeper references, similar to above .
3) In terms of definition:
Attributes defined through index must be referenced through index.
Attributes defined through non-index methods must be referenced through normal methods.
Also note: Object methods cannot be defined through index.
5. Dynamic addition and deletion of attributes and methods
1) For an instantiated object, we can dynamically add and delete its attributes and methods. The syntax is as follows (assuming the object instance is obj):
Dynamically add object properties
obj.newPropertyName=value;
Dynamicly add object methods
obj.newMethodName=method or =function(arg1,…,argN){}
Dynamicly delete object properties
delete obj.propertyName
Dynamic deletion of object method
delete obj.methodName
2) Example:
function User(name){
this.name=name;
this.age=18;
}
var user= new User(“user1”);
user.sister=“susan”;
alert(user.sister);//Run through
delete user.sister;
alert(user.sister) ;//Error: The object does not support this attribute
user.getMotherName=function(){return “mary”;}
alert(user.getMotherName());//Run through
delete user.getMotherName;
alert(user.getMotherName( ));//Error: The object does not support this method
JS object printing:

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment