Summary of JavaScript functions (ii)

Source: Internet
Author: User

    1. constructors typically do not use the return keyword, they initialize a new object, and when the function body of the constructor finishes executing, it displays the return. In this case, the result of the constructor call expression is the value of the new object. If, however, the constructor displays the return statement with an object, or returns an original value, this ignores the return value and uses the new object as the result of the call.

    2. Callee and Caller: Two properties of theargument object (arguments), in ECMAScript5 Strict mode, the read and write operations on both properties produce a type error. The former refers to the function that is currently executing. Caller is non-standard, but most say the browser implements this property, which refers to the function that invokes the currently executing function. callee are often used on recursive invocations of anonymous functions. eg:

Varfactorial=function (x) {

if (x<=1) return 1;

Return X*arguments.callee (x-1);

}

Console.log (factorial (3));

3. Function declarations are loaded into scope (global scope) before code is executed--there is a declaration elevation

The function expression is defined when the code executes into that line--no function declaration promotion

function functionname (ARG1,ARG2) {// This method is called a functional declaration

function Body

}

Varfunctionname = function (ARG1,ARG2) {// This method is called a functional expression

function Body

}

4. Function value passing: In JS , the function teacher points to a pointer to a function object, which is itself a variable, and the function can also be used as a return value. That is, not only can one function be passed to another function as a parameter, but one function can be used as another function, and a function can be returned as a result of another function.

but there is only one way to pass in JS: All parameters are passed by value. This means that numbers, strings, etc. are passed by value, and arrays, objects, and so on are also passed by value . (Although the pass-by value of arrays, objects, and so on is different from numbers, strings, and so on.) )

var v1 =[1,2,3];

var v2 ={};

var v3 ={a:123};

Functionfn1 (v1, v2, v3) {

V1 = [1];

v2 = [2];

V3 = {A:3};

}

FN1 (V1,v2, V3);

Alert (v1); [A]

Alert (v2); [Object Object]

alert (V3.A); 123

observe the above code to see:V1,V2,V3Have not been changed,V1is still a value of[A]the array,V2is a blank object,V3is a value of123of the PropertiesAthe object. Because arrays, objects, etc. are passed by value, this is the value of the variable address. Inside the function we areV1,V2,V3value, which means we putV1,V2,V3pointer to a new array and object, pointing to a change. Such an internalV1,V2,V3and the externalV1,V2,V3the connection was completely broken. The plot is:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/0B/wKiom1RJyVvwJJRrAAG8LG0G2j0963.jpg "title=" 1.png " alt= "Wkiom1rjyvvwjjrraag8lg0g2j0963.jpg"/>

The value of the global variable will also change if, in a child function, the reference to the local variable is not changed, and the array or object is manipulated directly. eg:

var v1 =[1,2,3];

var v2 ={};

var v3 ={a:123};

Functionfoo (v1, v2, v3)

{

V1.push (1);

v2.a = 2;

v3.a = 33;

}

Foo (V1,V2, V3);

Alert (v1); [1,2,3,1]

alert (v2.a); 2

alert (V3.A); 33

It follows that, although the parameters of a function are passed by value, the value-passing of reference types such as arrays, objects, and the values of the basic types of numbers, strings, and so on, are mainly expressed in:

1) numbers, strings are copied directly into the value to achieve;

2) Array, object is to copy the variable address in order to achieve.

5. We describe the scope as a list of objects, not a stack of bindings. Each time a Javascript function is called , a new object is created to hold the local variable and add the object to the scope chain. When the function returns, the object of the bound variable is removed from the scope chain. If there are no nested functions, and no other references point to the bound object, it will be garbage collected. If a nested function is defined, each nested function corresponds to a scope chain, and the scope chain points to a variable binding object. However, if these nested function objects are saved in an external function, they will also be garbage collected as the variable-bound objects they point to. But if the function defines a nested function and returns it as a return value or stores it in a property somewhere, there is an external reference to the nested function. He will not be treated as garbage collection, and the variable-bound object He points to will not be garbage collected.

6. In the function body,arguments.length represents the number of arguments passed into the function. the Length property of the function itself has different meanings. The length property of a function is a read-only property, which represents the number of function parameters, which can be obtained by Arguments.length , by Argume.callee.length Get the number of formal parameters

7. In the strict mode of Ecmascript5 call< Span style= "font-family: ' The song Body '; > () and apply null undefined ecmascript3 and undefined

8.bind() method:bind() is a new method in ECMAScript5, but bind () can be easily simulated in ECMAScript3. As you can see from the name, the main function of this method is to bind to an object. when the function F () call bind on () method and pass in an object o as a parameter, this method returns a new function. Invoking the new function (in the same way as a function call) will invoke the original function F() as an O method. Any arguments passed into the new function are passed into the original function, for example:

function f (Y) {return this.x+y;} this is a function to bind.

var o={x+1};// The object that will be bound

var g=f.bind (o);//Call O.F (x ) by calling G(x);

G (2)//=>3

in the The bind () method in ECMAScri5 isnot just about binding a function to an object, it also comes with some other apps: The arguments to the incoming bind () are bound to this, in addition to the first argument. eg

Varsum=function (x, y) {

Console.log (X+y);

}

creates a new function like sum, but the value of this is bound to null

and the first parameter is bound to 1, the new function expects to pass in only one argument

Varsucc=sum.bind (null,1);

SUCC (2);//=>3

in the Standard bind() method for ECMASCRIPT3 simulation

if (! Function.prototype.bind) {

Function.prototype.bind=function (o) {

var self=this,boundargs=arguments;

return function () {

var args=[],i;

for (Var i=1;i<boundargs.length;i++) {

Args.push (Boundargs[i]);

}

for (var i = 0; I <arguments.length; i++) {

Args.push (Arguments[i]);

};

Return self.apply (O,args);

};

};

};

we notice thatthefunction returned by the bind () method is a closure. the self and Boundargs variables are declared in the outer function of the closure , and the variable is called in the closure. Although the inner function that defines the closure has been returned from an external function, the time to call the closure logic is to be returned after the external function returns (the two variables can be accessed correctly in the closure).

The bind () method defined by ECMAScript5also has some characteristics that cannot be simulated by the above ECMASCRIPT3 code. First, the real bind() method returns a function object that has the length property of the binding function minus the number of binding arguments ( The value of length cannot be less than 0). Furthermore , the bind() method of ECMAScript5 can be constructed by passing the constructor function. If thefunction returned by bind () is used as a constructor, thefunction returned by the incoming bind () method is ignored and does not contain the prototype property (normal function intrinsic prototype properties are not deleted), and the objects created when these bound functions are used as constructors inherit prototype from the original unbound constructor . Similarly, when using the instanceof operator, the binding constructor and the unbound constructor are no different.


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1567490

Summary of JavaScript functions (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.