Function expressions are a powerful and confusing feature definition function in JavaScript in two ways, one is function declaration, the other is function expression
An important feature of the function declaration is the function declaration promotion, which means that the function declaration is read before executing the code, meaning that the function declaration can be placed after the statement that called it
Sayhi (); function Sayhi () {Alert ("HI");}
The second way to create a function is to use a function expression, which has several different syntactic forms
One of the most common ways is
var functionname = function (ARG0,ARG1,ARG2) {//body};
The key to understanding function promotion is to understand the difference between a function declaration and a function expression, such as the result of executing the following code, which can be unexpected.
if (conditon) {function sayhi () {alert ("yes");} else function Sayhi () {alert ("No");}}
In most cases, the browser returns a second declaration, ignoring the condition
Closed Package
A lot of developers don't understand. The concept closure of anonymous functions and closures is a function that has access to variables in another function scope, and the common way to create closures is to create another function inside one function
Private variables: Strictly speaking, there is no concept of private members in JavaScript; All object properties are public, but there is a concept of a private variable, any variable defined in the function can be considered a private variable, because the variables cannot be accessed outside the function, and the private variable includes the parameters of the function. Local variables and other functions defined inside the function
function Add (num1,num2) {
var sum = num1 + num2;
return sum;
}
Inside this function, there are three private variables, num1,num2,sum within the function can be bearing these variables, but outside the function cannot access them, if you create a closure within the function, then the closure through their own scope chain can also access these variables, and to take advantage of this, You can create public methods for accessing private variables.
We call public methods that have access to private variables and private functions as privileged methods, and there are two ways to create privileges on objects, the first of which is to define privileged methods in constructors, the basic schema is as follows
function person (name) {this.getname = function () {return name;}; this.setname = function (value) {name = value;};} var person = new Person ("Niko"), Alert (Person.name),//person.setname ("gred"), alert (person.name);
The code above defines two privileged methods, both of which can be used externally by the constructor.
A static private variable can also create a privileged method by defining a private variable or function in the private scope
(function () {var privateviarable = 10;//Private variable and private function Privatefunction () {return false;} MyObject = function () {};//constructor MyObject.prototype.publiceMethod = function () {Privateviarable++;return Privatefunction ();};}) ();
Module mode: The preceding pattern is used to create a private variable and a privileged method for a custom type and Douglas's module mode is to create private variables and privileged methods for the singleton, so-called Singleton, which refers to an object with only one instance, by convention, JavaScript is a way to create a singleton object in the form of an object literal.
var singleton = {Name:value;method:function () {//Here is the method of Code}};
var singleton = function () { var privatevariable = ten; function Privatefunction () {return false;} privileges, public methods and properties Return{publicproperty:true, publicmethod:function () {Privatevariable++;return Privatefunction ();}}; };();
JavaScript Advanced programming 7 function Expressions for learning notes