JavaScript notes-Basic knowledge (II)

Source: Internet
Author: User

function Type

function functions do not need a return type (not a return value), the parameter does not need to specify the type, more special is that the function is a class, can be out of new

var New Function (' num1 ', ' num2 ', ' return NUM1 + num2 ');

This is completely correct, but is not recommended, because it will parse two times (first parse JavaScript code, second parse parameter) cause performance problem

function can be used as a parameter

( It's weird, the method as a parameter can be interpreted as a reference to a function as a parameter.

 function Box (sumfunction,num) {            // functions as a parameter using the    return  sumfunction ( num);} function sum (num) {    return num+10;                        // need to return }alert (Box (sum,10));
function Internal Properties

Inside the function, there are two special objects:arguments and this

Arguments is a class array object that contains all of the parameters passed into the function, with the main purpose of saving the function arguments. But this object also has a property called Callee, which is a pointer to the function that owns the arguments object

A recursive method (you can use the Arguments.callee method instead of the call itself)

function box (num) {    if(num<=1) {        return 1;    } Else {        //return num*box (num-1);
return Num*arguments.callee (num-1);} } Alert (Box (3));

Another special object inside the function is this, which behaves roughly like this in Java. This refers to the object where the function is to perform the action, or the scope where the function call statement is located, and when the function is called in the global scope, the This object refers to the window

var color= ' Red ';             // Scope alert (this. color); var box={    color:' Blue ',    saycolor:function() {        alert (  this. color);}    ; Box.saycolor ();

The first one refers to the global window, and the second one refers to the object of box itself

function Properties and methods

Functions in JavaScript are objects, so functions also have properties and methods. Each function consists of two properties: length and prototype

The Length property represents the number of named arguments that the function expects to receive

function+//2

The prototype property, which is the true location of all instance methods, that is, prototypes. There are two methods under prototype:apply () and call (), each of which contains the two non-inherited methods. The purpose of both methods is to invoke the function in a specific scope, which is actually equal to setting the value of the This object in the function body

Apply () Method:

function Box (num1,num2) {    return num1+num2;} function Saybox (num1,num2) {    return box.apply (this, [num1,num2]);        The second argument is the array}function  sayBox2 (num1,num2)    {return box.apply (this  , arguments);      You can use arguments instead of the parameter list}alert (SayBox2 (10,10));

Call () Method:

function Box (num1,num2) {    return num1+num2;} function Saybox (num1,num2) {    return box.call (this, num1,num2);               // using the call method to pass a value cannot use arguments }alert (Saybox (10,10));

The call () method is the same as the Apply () method, except that they differ only in the way they receive parameters. For the call () method, the first parameter is the scope, there is no change, the change is just the way the parameter is passed

But in fact, passing parameters is not really where the apply () and call () methods work; they are often used to extend the scope in which functions run

var color= ' Red '; var box={    color:' Blue '}; function Saycolor () {    alert (this. color);} Saycolor.call (this);                 // Red saycolor.call (window);               // Blue saycolor.call (box);                  // the real purpose of the apply and call methods is to decouple the methods in the object from the object.
Variables and Scopes

A JavaScript variable may contain values for two different data types: the base type value and the reference type value. Primitive type values refer to simple data segments that are stored in the stack's memory, meaning that this value is completely stored in a single location in memory. The reference type value refers to the object that is stored in the heap memory, meaning that the variable is actually just a pointer to another location in memory where the object is saved (similar to how Java is handled)

variables

There is not much difference between the variable type and the operation and other languages, but you need to be aware of the difference between the base type and the reference type assignment operation

//Basic TypevarBox= ' Lee '; varBox2=box;//different, independent two spacesbox= ' alert ';//change doesn't affect each other .alert (box); alert (box2);//Reference typevarbox={name:' Lee '}; varBox2=box;//different spaces, same content (same reference address)Box.name= ' alert ';//change will affect each otheralert (box.name); alert (box2.name);

In fact, the types in Java do the same thing.

Variable types can be detected, using typeof or Instanceof

vararr=[1,2,3];//alert (typeof arr); The typeof detects that the reference type will appear array, object, and Null are all problems with object, the specific type does not knowAlert (arrinstanceofArray);//using instanceof to detect reference typesvarbox= ' LZ ';//var box=new String (' LZ ');Alert (BoxinstanceofString);//However, the instanceof detection base type returns FALSE. Here if the string is new, it also returns True//alert (typeof box); It is recommended to use TypeOf for basic type detection and to use instanceof for application type detection//Java also uses instanceof detection type, consistent usage, but no typeof
Scope

The scope of the action is actually different from other languages, and the curly braces of the judging and looping statements do not qualify the scope.

if(true){//the curly braces of the judgment statement do not qualify the scope    vari=10;}                alert (i); //Ten for(vari=0;i<10;i++) {//the curly braces of the loop statement are also not able to qualify the scope of the    varbox= ' LZ ';}                        alert (i); //Tenalert (box);//LZfunctionBox () {//only curly braces for function functions can qualify scopes    vari=10;}                 alert (i); //Error
Passing Parameters

The parameters of all functions in JavaScript are passed by value, and parameters are not passed by reference, although the variables have a basic type and a reference type, in fact C++,java are passed by value, PHP is both ways have

function // passed by value, the parameter passed is the base type // num Here is a local variable, and the global is invalid return num;} var num =; var result =/////

In the above code, the passed parameter is a basic type of value. and num in the function is a local variable, and there's no connection to num outside.

Easy to confuse is the following code

function // passed by value, the parameter passed is the reference type obj.name = ' LZ ';} var New Object (); box (p); alert (p.name)             ; // LZ

The result is changed because we pass a reference to the value of an address (as we can understand it), but there are different spaces, the same content (the same reference address) that affects each other.

Packing class

To facilitate operation of the base type value, JavaScript provides 3 special reference types: Boolean, number, and String. These types are similar to other reference types, but also have special behaviors corresponding to their respective base types. In fact, every time a primitive type value is read, the background creates an object of the corresponding basic wrapper type, so that it can invoke some methods to manipulate the data, so that it works as follows:

// var str= ' mr.li '; // Alert (str.substring (2)); Alert (' Mr.li '. SUBSTRING (2));        // Intercept Backward According to the specified position

These three wrapper classes provide a rich range of methods and properties to manipulate their underlying types, which are not documented here.

JavaScript notes-Basic knowledge (II)

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.