Function
A function is an event-driven or reusable block of code that executes when it is invoked.
function One (leve, leve) {
//code return
leve+leve
}
Comments:
Formal parameters need not be added to the type;
The return statement is optional, and a function with no returning statement returns undefined;
Local variables and global variables
Declaring within a function: local variables
Declaring outside a function: global variable
When you assign a value to a new variable name, VAR is not used: This variable changes to a new global variable
function can be used as a value
Form 1:
function init () {
alert ("One")
}
window.onload = init;
Form:
Window.onload = function () {
alert ("one");
}
Note: Both of these methods allow the browser to hint: one.
Object
Everything in JavaScript is an object: A string, a value, an array, a function, and, in addition, JavaScript allows you to customize the object.
References to Objects
When you assign an object to a variable, the variable contains a reference to the object, not the object itself.
When you call a function to pass in an object, you are actually passing only the object reference (copying a copy of the reference, passing it to the formal parameter, pointing to the object, where two references point to the same object)
Creating objects
var dog = {
name: "Mydog",
Weight:,
bark:function () {
alert ("woof!");
}
Dog.bark ();
Note: Each attribute (except the last one) must have ",".
Constructors Construct objects
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!");
}
};/ /here also can not forget the semicolon
}
var mydog = new Dog ("Hello", "");
Mydog.bark ();
Ps:
1. What is a constructor
constructor, which is a special method. It is primarily used to initialize objects when they are created, that is, to assign an initial value to an object member variable, always with the new operator in the statement that creates the object.
This is my interpretation of the relevant information, the interpretation of the book but the meaning of the expression is very clear. See below for a small example:
The code is as follows:
Copy Code code as follows:
var request = new XMLHttpRequest ();
This expression is often used when we create a request object when using AJAX technology. So we can see clearly that the phrase "new XMLHttpRequest ()" is a standard constructor! we "Var" declares a "request" object, using the constructor "new XMLHttpRequest ();" To initialize the "requ" EST object assigns it an initial value. So we know that the ' function ' that is used to create an object and initialize the object with the ' new ' operator is the constructor.
For example, our 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 a class is often referred to as instantiation.
Above I marked the point of interpretation with red and blue. To be honest, instantiating an object is the process of creating an Object!
So what is "class"? According to literal understanding we can be understood as "type". For example, "cake", it is a dessert classification, that is, a type; then cheese cake is the specific individual of the cake in the dessert, which is the object.
We know that in the programming language, "class" is abstract, we have no way to manipulate it or use its methods and attributes, only if we instantiate this class as an object, we can invoke its series of methods and properties. Actually this is also very good understanding, abstract things in life we have no way to see it or capture it, then naturally we do not have the means to use some of its functions, only the abstract of things specific to each individual, or the actual object, we can clearly understand or recognize it, programming is the case. Thus, an instantiated object is an abstraction to a specific process, and this process is called instantiation.