JavaScript's function object's apply (), call (), bind (), method, and Arguments,caller,length properties

Source: Internet
Author: User

Note: This article is in the original: http://www.jb51.net/article/30883.htm

Self as study, re-write.

First, write in front of the words

Front-end JavaScript programming is just a little bit of fur, Dom know a little, Jquey will use a little bit, know the solid foundation, understand the front-end of the system of Knowledge for Web development (their main do is PHP background development) important. Of course, what to use, just check what (now learn to sell) very reasonable, but for the long run, just in the shoe to itch, it is difficult to have a practical act.

Therefore, it should be a lesson to the Web front-end, especially JavaScript development (process-oriented objects-and various JavaScript libraries, jquery, JavaScript framework, anjular) These things should have some comprehensive system, Take a closer look. Only a comprehensive system to understand, in-depth, it will not always be temporary to the Buddha foot, to their own initiative to find problems, their own initiative to improve, open horizons.

Second, JavaScript's function object

Official API documentation (IE Microsoft): https://msdn.microsoft.com/zh-cn/library/x844tc74 (v=vs.94). aspx

Because of the properties and methods of the function object, our custom functions automatically have these properties and methods, which may be different when viewing Api,ie, FireFox, and Chrome.

Main talk about apply (), call ():

1, apply (), call () the same use, all in a specific scope to invoke the function. The + + calland apply methods are often used to implement similar inheritance functions to achieve code reuse . Their differences are mainly reflected in the parameters.

2, accept the parameters, apply () to receive two parameters, one is the function run scope (this), and the other is a parameter array.
The first parameter of the call () method is the same as the Apply () method, but the arguments passed to the function must be enumerated.

Example 1:

The result of the operation is:
Hello Diz song glad to meet you!
Hello my Object glad to meet you!

Example 2: With parameters

Analysis: By example 1, it is found that the real use of apply () and call () is to be able to extend the scope of function survival, if we want to implement the traditional method, the code is as follows:

We found that in order for the Helloname () function to be scoped to the object MyObject, we needed to dynamically create the Helloname property of the MyObject, which pointed to the helloname () function as a pointer , so that When we call Myobject.helloname (), the This variable inside the function points to myobjecT, and it is possible to invoke other public properties inside the object.

Example 2, we can see the call () and apply () function of the real application, in the actual project, but also according to the actual flexibility to deal with!
A small problem: Take a look at the case of the this variable when defining a function in a function :

The execution results are the same as the following

Example 3:

Let's look at an example:

Define a animal class

    

3. Bind () method

Browsers that support this approach are ie9+, firefox4+, safari5.1+, opera12+, and Chrome. It is a method that belongs to ECMAScript5. See examples directly:

Here, Saycolor () calls the Bind () method, passes in the O object, returns the Osaycolor () function, and in Osaycolor (), the value of this is an O object.

JavaScript's function object's apply (), call (), bind (), method, and Arguments,caller,length properties

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.