Basic knowledge of JavaScript (Keep for myself)

Source: Internet
Author: User

1. Function expressions

JavaScript functions can be defined by an expression. eg. var x = function (A, b) {return a * b};

So:var x = function (A, b) {return a * b};
var z = x (4, 3);

The above function is actually an anonymous function (the function has no name).

Functions are stored in variables and do not require a function name, usually called by a variable name.

2. In JavaScript, there are times when you need to avoid using the new keyword.

3. Function elevation (hoisting): actually refers to the scope of the extension. Definitions can be used after use

4. Self-calling function: a function that is actually called anonymously

eg.

<script>
(function () {
document.getElementById ("Demo"). InnerHTML = "hello! I was called by myself;
})();
</script>

5. The function is an object:

Using the typeof operator in JavaScript to determine the function type will return "functions".

However, JavaScript functions are described as an object that is more accurate.

6. The JavaScript function has a built-in object: Arguments object. It contains an array of arguments for the function call.

7. Value Pass parameter: parameter initial value is not modified; object Pass parameter: Similar reference, can be used outside the function.

8. A function call in JavaScript is a bit of a saying: 4 ways to call

(1) called directly as a function

eg.

function MyFunction (A, b) {
return a * b;
}
MyFunction (10, 2); MyFunction (10, 2) returns 20

This is similar to:

function MyFunction (A, b) {
return a * b;
}
Window.myfunction (10, 2); Window.myfunction (10, 2) returns 20

Cause: The above function does not specify an object, but belongs to the entire HTML page, so it belongs to the global object, which belongs to the Window object in the browser.

Although this method of function invocation is more commonly used, it is not a good programming habit, and it is easy to create a naming conflict.

When a function is not called by its own object , the value of this becomes a global object.

  (2) Call a function as a method in an object (similar to an object in Java)

eg.

var myObject = {
FirstName: "John",
LastName: "Doe",
Fullname:function () {
return this.firstname + "" + this.lastname;
}
}
Myobject.fullname (); Return to "John Doe"

At this point, the value of this in the instance is the myObject object.

  (3) Calling a function using the constructor function

If the new keyword is used before a function call, the constructor is called.

The call to the constructor creates a new object. The new object inherits the properties and methods of the constructor.

  (4) Invoking functions using the Apply () and call () methods

Reason: In JavaScript, functions are objects as well as properties and methods. That is, you can invoke the function object using the Apply () method in the function object or the call () method.

eg.

function MyFunction (A, b) {
return a * b;
}
Myfunction.call (MyObject, 10, 2); Returns 20

MyArray = [10,2];
Myfunction.apply (MyObject, MyArray); Returns 20

Under JavaScript Strict mode (strict mode), when a function is called, the first parameter becomes the value of this, even if the parameter is not an object.

In JavaScript's non-strict mode, if the value of the first parameter is null or undefined, it uses the global object override. non-strict

9. In JavaScript, if a variable has no var keyword at the time of declaration, it is a global variable

Ten. JavaScript closures (?? )

Basic knowledge of JavaScript (Keep for myself)

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.