JavaScript Authority design--javascript function (Brief study note 10)

Source: Internet
Author: User

1. Function Naming conventionsA function names a phrase that is usually prefixed with a verb. Usually the first character is lowercase. When multiple words are included, one convention is to divide the words by an underscore, like "Like_zqz ()". There is also a "LIKEZQZ ()". Some functions are used internally or are usually prefixed with an underscore for private functions, like "_zqzname ()".   2. Functions defined in an expression mannersuch as:
var zqz=function  () {    return ' zhaoqize ';}
it must be assigned to a variable when it is used, and this is the ZQZ. D Here we just thought about the variable ahead of the JavaScript authoritative design--javascript type, value, variable (briefly study note IV). But assigning a variable is not advanced, so the function defined by the expression cannot be called until it is defined. The function above is returned with a return. Unlike a function that has no return value, this function is sometimes called a procedure.   3. function declaration Statements
function A () {    }
above is the function declaration statement . A function declaration statement is not a true statement. They can appear in the global code as top-level statements. or embedded in other functions, but they cannot appear in loop statements , conditional statements, etc. But function definition expressions can appear anywhere, including loop statements, conditional judgments, and so on. The following is a function-definition expression :
var a=function  () {}
  4. Four ways to call a function mode one: as a function
function A () {    }// call A ();
  Way Two: As Methodsuch as:
var calcu={    // Define object Direct volume    ope:1,    Opa:1,    add:  function() {        this. result=the. ope+this. Opa;    // This refers to the current object     }}// call calcu.add ();    // This method invokes the result of the calculation of the
This is a call expression, which includes the function expression Calcu.add, which is itself a property access expression, which means that the function is called as a method, not as a normal function.  
Calcu.result;    // 2
 method calls are important differences from function calls: Call contexta property access expression consists of two parts : an object (CALCU) and a property name (add). in such a method expression, the object CALCU the calling context, and the function body can refer to the object using the keyword this (the object is CALCU). look at the following two lines of code, assuming that the function is the same as for the object rect:
Rect.setsize (width,height);    Setrectsize (Rect,width,height);   
 The first sentence can be clearly seen that this function executes the vector is a Rect object, all operations in the function are based on this object.  when the method does not need to return a value, it is best to return this directly, if in the design of the API has been in this way every method returns this, using the API can be "chained call" style of programming, in this programming, as long as the development of the object to be called. The rest of the methods can be called based on this. A typical example is jquery.   the point of this:Example Description: (This example is suitable for deep thinking, combined with jquery)
varo={//object oM:function(){//method M () in the object        varself= This;//Save this value to a variableConsole.log ( This===o)//True,this is the object of ();//call nested function f         functionf () {Console.log ( This===O);//False:this points to a global object or undefinedConsole.log (self===o);//True:self The This value that points to an external function        }    }}//calledO.M ();
Myth: Many people mistakenly assume that this in the nested function f in the example above will point to the context of the outer function. if you want to access the external this, you need to save the value of this in the Yige variable. Both this variable and the intrinsic function are in the same scope.   Way Three: As a constructor (learn tomorrow) mode four: Indirect invocation via call () and apply () method (Learn tomorrow

JavaScript Authority design--javascript function (Brief study note 10)

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.