A function is an event-driven or reusable code block that is executed when it is called. Everything in JavaScript is an object: String, value, array, and function, next, I will introduce JavaScript function objects through this article. If you are interested, let's study them together.
Function
A function is an event-driven or reusable code block that is executed when it is called.
function One(leve , leve){ //code return leve+leve }
Note:
The parameter type is not required;
The return Statement is optional. functions without return statements return undefined;
Local variables and global variables
Declare in the function: local variable
Declare a global variable outside the Function
Var is not used when assigning values to a new variable name: this variable becomes a new global variable.
Functions can be used as values.
Form 1:
function init(){ alert("One") } window.onload = init;
Form:
window.onload = function(){ alert("One"); }
Note: The browser prompts One in both of the preceding methods.
Object
All things in JavaScript are objects: strings, values, arrays, and functions. In addition, JavaScript allows custom objects.
Object Reference
When an object is assigned to a variable, the variable will contain a reference of the object, not the object itself.
When a function is called to pass in an object, only the object reference is actually passed (copy a reference copy and pass it to the form parameter to point to the object, that is, two references point to the same object)
Create object
var dog = { name : "myDog", weight : , bark :function(){ alert("woof!"); } } dog.bark();
Note: Each attribute (except the last one) must have ",".
Constructor object
Function Dog (name, weight) {this. name = name; this. weight = weight; this. bark = function () {if (this. weight>) {alert (this. name + "Woof! ");} Else {alert (this. name +" Yip! ") ;}}; // You cannot forget the semicolon} var myDog = new Dog (" hello "," "); myDog. bark ();
PS:
1. What is a constructor?
Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object.
This is my explanation of the relevant materials. The explanation is very book-oriented, but the meaning is still clear. The following is an example:
The Code is as follows:
The Code is as follows:
Var request = new XMLHttpRequest ();
This expression is often used when a request object is created using AJAX technology. We can clearly see that the phrase "new XMLHttpRequest ();" is a standard constructor! "Var" declares a "request" object, and uses the constructor "new XMLHttpRequest ();" to initialize this "request" object and assign it an initial value. Therefore, we can know that "the 'function' used with the 'new' operator to create an object and initialize the object is a constructor ".
For example, the common declared array is the standard constructor: var Array = new array ();
2. What is an instantiated object?
The Code is as follows:
var request = new XMLHttpRequest();
In object-oriented programming, the process of creating objects using classes is usually called Instantiation.
I marked the emphasis of the explanation in red and blue. To put it bluntly, instantiating an object is the process of creating an object!
So what is "class? Literally, we can be understood as "type ". For example, "cake" is a dessert classification, that is, a type. Then, the cheese cake is the specific individual of the cake classification in the dessert, that is, the object.
We know that in programming languages, "Classes" are abstract. We cannot operate on them or use its methods and attributes. Only by instantiating this class into an object, we can call a series of methods and properties. In fact, this is quite understandable. We cannot see or capture abstract things in our lives. Naturally, we cannot use some of its functions, only when abstract things are specific to individual, individual, or actual objects can we clearly understand or understand them. The same is true for programming. Therefore, the instantiation object is a process from abstraction to concrete, which is called Instantiation.