Deep understanding of JavaScript functions _ Basics

Source: Internet
Author: User

Methods & Functions

Difference

1.function is a more general concept, such as math, programming

2.method is an object-oriented concept that typically appears in pairs with classes or objects

Relationship

1. The properties of an object can be any type

2. The object's property, if it is a function type, is called the method of the object

3. So the essence of the method is still the function

Call to function

1.fun ()
2.obj.fun ()
3.fun.call ()

Properties and methods of a function

1.name
2.length
3.toString

Scope

Variables are not available anywhere

Global variables (cross files)

var n = 1;
function fn () {
  console.log (n);//1
}

Local Variables (accessible only within functions)

function fn1 () {
  var n = 2;
}
Console.log (n); Uncaught Referenceerror:number is not defined

function scopes

• Functions can be independent of a scope

var n = 1;
function f () {
  var n = 2;
  Console.log (n); In the current scope of the variable search
}
f ();
Console.log (n); To search for variables in global scope

• Functions can be accessed outside the function

var n = 1;
var x = function () {
  console.log (n);
};

function f () {
  var n = 2;
  X ();
}
f ();

• Self-Invoke anonymous function

!function () {
  var n = 1;
  Console.log (n);
};

~function () {
  var n = 1;
  Console.log (n);
};

(function () {
  var n = 1;
  Console.log (n);
} ());

• Closures

<! DOCTYPE html>
 
 

This in-depth understanding of the JavaScript function is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.