The-javascript authoritative guide to JavaScript Basic Learning--eighth Chapter function attribute method constructor __javascript

Source: Internet
Author: User
function Properties, methods, and constructors I. Function Properties

A function is a special object in JS and can have attributes.

Funcation.length Property: Represents the length of the formal parameter, read-only property, not allowed to change
Arguments.length represents the actual parameter length
Prototype property: Each function has a prototype property, and when the function is used as a constructor, the newly created object inherits the property from the prototype object. Second, function method

Unlike other languages, the ECMAScript function does not care about how many arguments are passed in, nor what type. Because an argument is internally represented by an array of classes, the function receives always an array of classes.
Named arguments (formal parameters) are convenient but not required.

Note: JS in the method itself is not overloaded, because:
1. The essence of the method is a function-type variable, which is overwritten if a variable of the same name appears.
2. But rely on function internal property argument to simulate overload.
Simulates a function overload.

function Testargument () {
if (arguments.length = = 1) {
        alert ("Haha ~" +arguments[0]);
    } else if ( Arguments.length = = 2) {
        alert ("haha ~2" + arguments[0] + "," + arguments[1]);
    } else {
        alert ("haha ~3");
    }
}
Testargument ();
Testargument (1);
Testargument (1, 2);

Prototype is where the "all instances" shared properties of an object are saved. That is, the methods such as ToString () and valueof () are actually stored in the prototype name, but only through instances of their respective objects. The
role of the prototype property is extremely important when creating custom reference types and implementing inheritance.

Call () and apply () method: As a method of an object, calls the function indirectly by calling the form of the method.
The first parameter is the parent object that calls the function, that is, the invocation context. Get a reference to him through this in the body of the function.

F.call (o);
F.apply (o);

Equivalent to
O.M = f ();
O.M ();
Delete o.m;

The first argument in the ES5 becomes the value of this, even if it is passed in null and undefined.
In ES3 and non strict mode, incoming null and undefined are replaced by global objects, while other original values are replaced by other corresponding wrapper objects.

Bind () Method: The new addition method in ES5, which is primarily to bind a function to an object.
When the bind () method is called on the function f () to pass in the object O, the method returns a new function, which is invoked as an O method when the new function is invoked. third, function constructor

function () constructor

The function () constructor can pass in any number of string arguments, and the text represented by the last argument is a body of functions. The function () constructor runs JavaScript to create and compile functions dynamically at run time, every time the function () constructor is invoked, the functional body is parsed and a new function object is created. If you create inefficiencies in loops. function () creates functions that do not use lexical scopes, whereas the compilation of function body code is always performed at the top-level function. Four, callable objects

A function is a callable object. All functions are callable, but not all callable objects are functions.

A callable object cannot be counted as a function in two JavaScript implementations. Window.alert () and Document.getelelmentbyid () use callable host objects but are not function objects in nature.

RegExp object. Determines whether an object is similar to a function and a isarray.

[JavaScript] View plaincopy
function isfunction (x) {return  
    Object.prototype.toString.call (x) = = "[Object Function] ";  
}  
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.