JavaScript Tutorials-Common JS functions (1/2)

Source: Internet
Author: User

1. When the number of actual parameters does not match the number of formal parameters, no error is caused.
If the actual parameter value is too high, the parameter value that is exceeded is ignored, and if the actual parameter value is too small, the missing value is replaced with undefined.

2. There are four modes of invocation in Web effects: Method invocation mode, function call mode, constructor call pattern, and apply invocation pattern. These patterns differ in how to initialize the key parameter this.
A. Method invocation pattern:
When a function is saved as an attribute of an object, we call it a method. The method invocation pattern can use this to access the object, so it can take values from the object or modify the object.
B. function call Pattern:
When a function is not a property of an object, it is called as a function. When a function is called in this mode, this is bound to a global variable, which is an error on the language design. (Correct situation: When an internal function is called, this should still be bound to the this variable of the external function.) Workaround: If the method defines a variable and assigns the value to this, then the internal function can access to this by the variable, and by convention, name that variable:

Add a double method to the MyObject

myobject.double = function ()
{
var that = this; Solving method

var helper = function ()
{
That.value = Add (That.value, that.value);
}

Helper (); Invokes the helper in the form of a function.
}

Call double in the form of a method
Myobject.double ();

C. constructor invocation pattern:
Called with new on the front of a function, a new object is created that hides the prototype member connected to the function, and this will be bound to the new object

Create a constructor named Quo. It constructs an object with the status attribute

var quo = function (string)
{
This.status = string;
}

Provide a public method named Get_status for all instances of quo
Quo.prototype.get_status = function ()
{
return this.status;
}

Construct a Quo instance
var myquo = new Quo ("confused"); Constructor Mode call
Document.writeln (Myquo.get_status ());

Constructor functions are saved in a variable named in uppercase format.
D.apply Call Mode:
The Apply method accepts two parameters, the first is the value that will be bound to this, and the second is the parameter array. It lets us construct a parameter array and use it to invoke the function.

Construct a Quo instance
var myquo = new Quo ("confused"); Constructor Mode call
Document.writeln (Myquo.get_status ());

Build an array that contains two numbers and add them together
Add = function (A, B)
{
return a+b;
}

var Anarray = [3, 4];
var sum = add.apply (null, Anarray); 7

Building an object that contains a status member
var statusobject = {
Status: "A-ok"
};

Statusobject did not inherit from Quo.prototype, but we can then call Statusobject on the
Get_status method, although Statusobject does not have a method named Get_status.
var status = quo.prototype.get_status.apply (Statusobject);

3. Parameters:
When a function is called, there is a default argument, which is arguments "array".


Construct a function that adds many values

Note the variable sum defined inside the function does not conflict with the sum defined outside the function.
The function can only see the internal variable.

var sum = function ()
{
var i, sum=0;
for (i = 0; i < arguments.length; i+=1)
{
Sum + + arguments[i];
}
return sum;
};

Document.writeln (SUM (4, 8, 15, 16, 23, 42)); 108

Note: Arguments is not a real array. It knowledge of an "analogous array" object. Arguments a length property, but it lacks all of the array methods.

4. Returns:
A function always returns a value. Returns undefined if there is no sticky return value.
Returns this (the new object) if the function is invoked as a constructor and the return value is not an object.

Home 1 2 last page

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.