Home>

A function is an event-driven or reusable block of code that executes when it is called,Everything in JavaScript is an object:strings, numbers, arrays, functions. The following article introduces JavaScript function objects to everyoneInterested friends study together

function

A function is an event-driven or reusable block of code that executes when it is called.

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

Notes:

Formal parameters do not need to add type;

The return statement is optional,A function without a return statement returns undefined;

Local and global variables

statement Declared inside the function:local variables

statement Declaration outside the function:global variables

Endow Assigning a new variable name without using var:this variable will become 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 above two methods,Can make the browser prompt:one.

Object

Everything in JavaScript is an object:strings, numbers, arrays, functions. In addition, JavaScript allows custom objects.

Object reference

Time When assigning an object to a variable,This variable will contain a reference to this object,Not the object itself.

When calling a function to pass in an object,In fact, only the object reference is passed (copy a copy of the reference,Passed to formal parameters,Point to the object,Ie two references point to the same object)

Create object

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

Note:There must be a "," after each attribute (except the last one).

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

ps:

1. What is a constructor

Constructor is a special method.It is mainly used to initialize objects when they are created, Assign initial values ​​to object member variables,Always used with the new operator in statements that create objects.

This is my explanation of consulting relevant information,The explanation is very book-oriented but the meaning is still clear.Please see the small example below:

code show as below:

Var request=new xmlhttprequest ();

expression This expression is often used when we create a request object when using ajax technology.Then we can clearly see that "new xmlhttprequest ();" This sentence is a standard constructor! We "var" declare a "request" object, and initialize the "new xmlhttprequest ();" The request object assigns it an initial value.So we know:"The" function "used with the" new "operator to create and initialize an object is the constructor."

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

2. What is an instantiated object

code show as below:

var request=new xmlhttprequest ();

In object-oriented programming,The process of creating objects with classes is often called instantiation.

Above I have highlighted the points of explanation in red and blue.To put it plainly, instantiating an object is the process of creating it!

So what is "class"?According to the literal understanding, we can understand it as "type". For example "cake", it is a classification of desserts,Which is a type;Then cheesecake is a specific individual in the category cake in dessert,That is the object.

We know that in programming languages,"Class" is abstract,We have no way to manipulate it or use its methods and properties,Only by instantiating this class into an object,We can then call its series of methods and properties.Actually this is well understood,There is no way to see or capture something abstract in life,So naturally we have no way to use some of its features,Only the abstract things are specific to one by one,Individual or actual object,Can we clearly understand or recognize it;So is programming.Therefore, instantiating an object is a process from abstraction to concrete,This process is called instantiation.

  • Previous Example of multi-threaded operation of PHP using Pthread
  • Next Talking about the implementation of objects in JavaScript and Promise objects