Reference Type in JavaScript
Reference Type in JavaScript
There are two types of data in JavaScript: basic type and reference type.! The basic types include Boolean, String, and Number. The reference types include Object, Array, Function, and Date (commonly used ).
Reference Type
The value (object) of the reference type is an instance of the reference type.. In JS, the reference type is a data structure used to organize data and functions together. It is also often called a class, but this name is not appropriate. Although Javascript is technically an object-oriented language, it does not have basic structures such as classes and interfaces supported by traditional object-oriented languages.The reference type can also be called an object definition.Because they describe the attributes and methods of a class of objects.
Object Type
There are two ways to create an object-type instance. First, use the new operator followed by the Object constructor:
var person =new object();person.name="bluce";person.sex="man";person.age=58;
The second is to useObject literalRepresentation ,(Key: Value)
var person ={ name:'bluce', sex:'man', age:58}
The second method is recommended,Concise, with less code, there is a feeling of encapsulating data. The preferred way to pass a large number of parameters to a function (this method is used in the previous blog ).
Array type
The Object type is the most common data type in JS, followed by the Array type. The Array type has the following features:
Each item of the array can save data of the type. {38, 'bluce ', {name: 'bluce', sex: 'man '}; the array length is dynamically changed, similar to the generic type in java.
There are two ways to create an array,
The first is to use Array to construct a function.The Code is as follows:
var persons =new Array();var persons=new Array(20);var persons=new Array('bluce','james','jhon');
In addition, the new operator can be omitted when Array constructor is used. The result of the omitted operator is the same as that of the above Code:
var persons = Array();var persons = Array(20);var persons = Array('bluce','james','jhon');
The second is to useArray literalThe array is enclosed by []. Multiple data items are separated by commas.
var players = [];var players = ['kurry','kobe','james'];
Indexes are the same as those in Java, C #, and other languages, starting from 0. Item = players [0];
ArrayCommon Methods:
| Method |
Function |
| Push () |
Add data to the end of the array, which can be any number of items; return the modified array Length |
| Pop () |
Remove the entry at the end of the array and return the removed entry. length-1 |
| Shift () |
Removes the first entry of the array and returns the removed entry. length-1 |
| Unshift () |
Add any number of entries at the front end of the array and return the modified array length. |
| Reverse () |
Reorder |
| Sort () |
Reorder |
| Concat () |
Reconstructs an array based on input parameters |
| Slice () |
Split Array |
| Splice () |
|
| IndexOf |
|
| LastIndexOf () |
|
| Iteration Method |
Every (), some (), map (), forEach (), filter () |
There are many methods, and I will summarize some of my frequently used methods later.
Function Type
Functions in JS are actually objects. Each Function is a Function-type instance and has attributes and methods like other reference types. Because the function is an objectThe function name is actually a pointer to the function object.Is not bound to a function (the function is not overloaded ). There are three methods to define a function:
First --Function Declaration:
function sum(num1,num2){ return num1+num2;}
The second is a function expression. The code below defines the variable sum and initializes it as a function.
var sum = function(num1,num2){ return num1+num2;};
The third is to use Function to construct a Function. The Function constructor can receive any number of parameters, but the last parameter is always regarded as the Function body. The preceding parameter enumerates the parameters of the new Function. For example:
var sum=Function('num1','num2','return num1+num2');
The third method is not recommended, but this method indicates that the function is actually an object. In the second or second method, function declaration uses more than function expressions.
The function name is only a pointer to a function. Therefore, the function name is no different from other variables containing the Object Pointer. That is to say, a function may have multiple names. Use a function name without parentheses to access the function pointer instead of calling the function. For example:
function sum(num1,num2){ return num1+num2;} alert(sum(2,2)); //4var anotherSum=sum;alert(anotherSum(2,2));//4sum=null;alert(anotherSum(2,2));//4
Function is not overloaded
Function names are pointers to functions. JS does not have the concept of function overloading.
function add(num){ return num+10;}function add(num){ return num+20;}var result=alert(add(10)); //30
The above Code declares two functions with the same name. The result is that the subsequent functions overwrite the previous functions. The above code is essentially equivalent to the following code:
var add=function(num){ return num+10;};add =function(num){ return num+20;};var result=alert(add(10)); //30
"Add" is only a pointer. repeated declarations only change the function to which "add" points.
Function Attributes and Methods
Functions in JS are objects, so functions also have attributes and methods. Each function has two attributes: length and prototype. The length attribute indicates the number of name parameters that the function wants to receive. Prototype (prototype), which will be summarized later when you relearn js object-oriented programming.