JavaScript Basic Learning-javascript Authoritative Guide--8.7 Function Property Method constructors

Source: Internet
Author: User

function properties, methods and constructors, function properties

A function is a special object in JS that can have properties.

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

Second, the function method

Unlike other languages, the ECMAScript function doesn't care about how many arguments are passed in or what type. Because an argument is internally represented by an array of classes, the function is always receiving an array of classes.
A named parameter (formal parameter) is only convenient, but not necessary.

Note: The method itself is not overloaded in JS because:
1. The essence of a method is a variable of type function, and if a variable with the same name is present, it will be overwritten!
2. But rely on the function internal property argument to simulate overloading!
Simulate function overloading!

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

prototype 是保存某个对象【所有实例】【共享属性】的地方。就是说,比如toString()和valueOf()等方法实际上都保存在prototype名下,只不过通过各自对象的实例访问罢了。在创建自定义引用类型及实现继承时,prototype属性的作用是极为重要的。

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

=delete o.m;

The first argument in ES5 becomes the value of this, even if null and undefined are passed in.
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, the main function is to bind the function to an object.
When the bind () method is called on the function f () to pass in the object O, this method returns a new function that is invoked as an O method when the new function is called.

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 the body.

    • The function () constructor runs JavaScript to create and compile functions dynamically at run time
    • Each call to the function () constructor parses the body of the functions and creates a new function object. If you create inefficiencies in the loop.
    • 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.

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

[JavaScript] View plaincopy function isfunction (x) {      return Object.prototype.toString.call (x) = = = "[Object Function]";  }  

JavaScript Basic Learning-javascript Authoritative Guide--8.7 Function Property Method constructors

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.