Detail the JavaScript function object

  • 2020-11-03 22:02:00
  • OfStack

function

A function is a reusable block of code that is driven by an event or executed when it is called.


function One(leve , leve){
  //code
  return leve+leve
 }

Note:

Formal parameters do not need to be typed;

The return statement is optional. Functions without return statements return undefined.

Local variables versus global variables

Declare a local variable within a function

Declaration of a global variable outside a function

var is not used when assigning a new variable name: the variable becomes the new global variable

A function can be a value

Form 1:


 function init(){
  alert("One")
 }
 window.onload = init;

Form:


window.onload = function(){
  alert("One");
 }

Note: Either way, the browser will prompt One.

object

Everything in JavaScript is an object: strings, values, arrays, functions, and JavaScript allows you to customize objects.

Object reference

When an object is assigned to a variable, the variable contains a reference to the object, not the object itself.

When you call a function and pass in an object, you are actually passing in only the object reference (make a copy of the reference, pass it to the parameter, point to the object, i.e. two references point to the same object)

Create an object


 var dog = {
  name : "myDog",
  weight : ,
  bark :function(){
   alert("woof!");
  } 
 }
 dog.bark();

Note: Every attribute (except the last one) should have a ", "after it.

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!");
   }
  };// You can't forget the semicolon here 
 }
 var myDog = new Dog("hello","");
 myDog.bark();

PS:

1. What is a constructor

The constructor is a special method. It is mainly used to initialize the object when the object is created, that is, to assign an initial value to the object member variable. It is always used with the new operator 1 in the statement of creating the object.

This is the explanation That I look up relevant material, the explanation is very book style but the meaning expression is very clear. Here's a quick example:


 var request = new XMLHttpRequest();

This expression is often used when creating request objects using the AJAX technique. Then we can see clearly "new XMLHttpRequest();" This sentence is a standard constructor! We "var" declare 1 "request" object, using the constructor "new XMLHttpRequest();" To initialize the "request" object to give it an initial value. So we know that "the 'function' used to create and initialize objects with the 'new' operator 1 is the constructor".

For example, our common declaration array is the standard constructor: var array = new Array();

2. What is an instantiation object

The code is as follows:


var request = new XMLHttpRequest();

In object-oriented programming, the process of creating an object with a class is often referred to as instantiation.

I've highlighted the key points in red and blue above. To put it bluntly, instantiating an object is the process of creating an object!

So what is a class ? According to the literal meaning, we can think of it as "type". For example, "cake", which is the classification of 1 dessert, namely 1 type; Then cheesecake is the specific individual, the object, of the classification of cakes in desserts.

We know that in the programming language, "class" is abstract, and we have no way to manipulate it or use its methods and properties. Only by instantiating the class as an object can we call its 1 series of methods and properties. In fact, this is also very easy to understand, in the life of abstract things we can not see it or capture it, then naturally we have no way to use some of its functions, only concrete abstract things to 1, individual or actual objects, we can clearly understand or recognize it; The same is true for programming. Thus, instantiating an object is the process of moving from an abstraction to a concrete object. This process is called instantiation.


Related articles: