Object
create
Object
Constructor
Public, private, privileged, static members
this, call and apply
Exception handling
inherit
prototype
Object
In JavaScript, it can be said that everything is object, so what is an object? An object is a collection of variables and functions. In other object-oriented languages, objects are instantiated by classes. JavaScript is an object-oriented language based on prototypes. There is no concept of class, and objects are derived from copies of existing objects. Objects in JavaScript can be divided into two categories: Function and Object.
Create an object
In order to improve efficiency, JavaScript comes with built-in objects, such as Object, Function, Array, etc. All built-in objects can be created through new. Function objects are divided into two categories: instances and constructors. For example, alert('my name is X') is an instance of the Function; and the Function as the constructor must be instantiated through new. The syntax of the created object is divided into the following types:
var obj= new Object();var obj={};(Array, etc.)
var myFunction=new Function(){//code};function myFunction(){//code}
It should be noted that the first function declaration must be before use, while the second can be after use.
Constructor
Function is the starting point of the constructor, and creating a constructor is similar to the above-mentioned creation of the object Function
var myFunction=new Function('a',/*code*/)
function myFunction(a){
/*Code*/
}
However, due to the performance problems of the first type, the second type is recommended; the feature of the Function object is that its instance can also be used as a constructor.
Static members
The following code:
var myObj= new Object();
//Add name attribute
='LD';
//Add alertName method
=function(){
alert();
}
//Execute alertName
();
name and alertName only exist in myObj instance and do not exist in the constructor. This is easy to understand, but it is not so easy to understand for a function that can be a constructor or an instance, as follows:
var myConstructor=new function(){
//Add static properties
='LD';
//Add static method
=function(){
alert();
}
}
();
The code works fine because myConstructor can be an instance, but name and alertName will not be applied to any new instances by myConstructor.
Public Members
Members that can follow the instantiation of objects are called public members. To become a prototype of the function that needs to be modified, i.e. prototype. Public methods can be inherited with the constructor, as follows:
function myConstructor(){
}
//Add public attributes
='LD';
//Instantiation
var myObj=new myConstructor();
alert();
Objects instantiated by myConstructor can use myName, but myConstructor itself cannot, because we add public members to the underlying definition of myConstructor, rather than the myConstructor instance itself.
Private Members
Private members refer to variables and methods defined in constructors, similar to those defined in private in classes in other languages, such as:
function myConstructor(){
//Add private attributes
var myName='Ld'l
//Add a private method
var alertName=function(){
alert('LD');
}
alertName();
Privileged members
Privileged methods refer to the ability to be publicly accessed and can access private members. The method defined by this is always used in the constructor scope, similar to public methods in other languages, as follows:
function myConstructor(){
//Private Attributes
var sex='male';
// Privileged method
=function(){
alert(sex);
}
}
Object literal
The creation we used before were all points, such as =x;=x. We can also use object literals to achieve the same purpose, for example:
function myConstructor(){
}
//Add a public member
={
name:'LD',
sex:'male',
method:function(){}
}
Note that the separator in the object literal is a comma, and the last property or method has no comma at the end, in case of parsing errors.
this, call and apply
This is a keyword that depends on the execution environment and has nothing to do with the creation location. This keyword points to an object that uses functions that contain it. It is not difficult to understand after learning C++ and other languages.
Call and apply, both of which force the method to be attached to an object, for example:
//alertName is a function that has been created
// When the parameter is not required for the altName
('Object')
// When the parameter is required for the altName
('Object','parameter 1','parameter 2')
// When using the parameter array
('Object','Arguments Array Arguments')
Exception handling
Similar to C#, it consists of try and catch, as follows:
function myFunction(){
='red';
}
try{
myFunction();
}
catch{
alert('Exception information:'+ + )
}
There are many inheritance and prototypes, so I will put it in the next blog "Inheritance and prototypes in JavaScript"
create
Object
Constructor
Public, private, privileged, static members
this, call and apply
Exception handling
inherit
prototype
Object
In JavaScript, it can be said that everything is object, so what is an object? An object is a collection of variables and functions. In other object-oriented languages, objects are instantiated by classes. JavaScript is an object-oriented language based on prototypes. There is no concept of class, and objects are derived from copies of existing objects. Objects in JavaScript can be divided into two categories: Function and Object.
Create an object
In order to improve efficiency, JavaScript comes with built-in objects, such as Object, Function, Array, etc. All built-in objects can be created through new. Function objects are divided into two categories: instances and constructors. For example, alert('my name is X') is an instance of the Function; and the Function as the constructor must be instantiated through new. The syntax of the created object is divided into the following types:
Copy the codeThe code is as follows:
var obj= new Object();var obj={};(Array, etc.)
var myFunction=new Function(){//code};function myFunction(){//code}
It should be noted that the first function declaration must be before use, while the second can be after use.
Constructor
Function is the starting point of the constructor, and creating a constructor is similar to the above-mentioned creation of the object Function
Copy the codeThe code is as follows:
var myFunction=new Function('a',/*code*/)
function myFunction(a){
/*Code*/
}
However, due to the performance problems of the first type, the second type is recommended; the feature of the Function object is that its instance can also be used as a constructor.
Static members
The following code:
Copy the codeThe code is as follows:
var myObj= new Object();
//Add name attribute
='LD';
//Add alertName method
=function(){
alert();
}
//Execute alertName
();
name and alertName only exist in myObj instance and do not exist in the constructor. This is easy to understand, but it is not so easy to understand for a function that can be a constructor or an instance, as follows:
Copy the codeThe code is as follows:
var myConstructor=new function(){
//Add static properties
='LD';
//Add static method
=function(){
alert();
}
}
();
The code works fine because myConstructor can be an instance, but name and alertName will not be applied to any new instances by myConstructor.
Public Members
Members that can follow the instantiation of objects are called public members. To become a prototype of the function that needs to be modified, i.e. prototype. Public methods can be inherited with the constructor, as follows:
Copy the codeThe code is as follows:
function myConstructor(){
}
//Add public attributes
='LD';
//Instantiation
var myObj=new myConstructor();
alert();
Objects instantiated by myConstructor can use myName, but myConstructor itself cannot, because we add public members to the underlying definition of myConstructor, rather than the myConstructor instance itself.
Private Members
Private members refer to variables and methods defined in constructors, similar to those defined in private in classes in other languages, such as:
Copy the codeThe code is as follows:
function myConstructor(){
//Add private attributes
var myName='Ld'l
//Add a private method
var alertName=function(){
alert('LD');
}
alertName();
Privileged members
Privileged methods refer to the ability to be publicly accessed and can access private members. The method defined by this is always used in the constructor scope, similar to public methods in other languages, as follows:
Copy the codeThe code is as follows:
function myConstructor(){
//Private Attributes
var sex='male';
// Privileged method
=function(){
alert(sex);
}
}
Object literal
The creation we used before were all points, such as =x;=x. We can also use object literals to achieve the same purpose, for example:
Copy the codeThe code is as follows:
function myConstructor(){
}
//Add a public member
={
name:'LD',
sex:'male',
method:function(){}
}
Note that the separator in the object literal is a comma, and the last property or method has no comma at the end, in case of parsing errors.
this, call and apply
This is a keyword that depends on the execution environment and has nothing to do with the creation location. This keyword points to an object that uses functions that contain it. It is not difficult to understand after learning C++ and other languages.
Call and apply, both of which force the method to be attached to an object, for example:
Copy the codeThe code is as follows:
//alertName is a function that has been created
// When the parameter is not required for the altName
('Object')
// When the parameter is required for the altName
('Object','parameter 1','parameter 2')
// When using the parameter array
('Object','Arguments Array Arguments')
Exception handling
Similar to C#, it consists of try and catch, as follows:
Copy the codeThe code is as follows:
function myFunction(){
='red';
}
try{
myFunction();
}
catch{
alert('Exception information:'+ + )
}
There are many inheritance and prototypes, so I will put it in the next blog "Inheritance and prototypes in JavaScript"