This call and apply

Source: Internet
Author: User

This pointer always points to an object that can be roughly divided into the following four types:

1, method invocation as an object (this points to the object)

2, as a normal function call

When a function is not called as an object's property, that is, the normal function, at which point this always points to the global object. In the browser's JavaScript, the global object is Windows.

1 ' globainame '; 2 var getname=function () {3     return;4};5 console.log (GetName ());
This points to the global object

3, constructor call

There are no classes in JavaScript, but you can create objects from the constructor and also provide the new operator, which makes the constructor look more like a class.

When a function is called with the new operator, the function always returns an object, usually the this in the constructor points to the returned object

var MyClass () { ' Sven ';}; var obj=new MyClass (); alert (;
This in the constructor

However, if the constructor shows an object of type objects returned, the result will eventually return the object.

var MyClass () {  this. name= ' Sven ';    return {                     // Display Returns an object   name: ' Anne ';}}; var obj=New  MyClass (); alert (;             // output Anne                            
constructor Display returns object Type

4, and Function.prototype.apply

Their role is the same, the difference is in the form of incoming parameters. Apply accepts two parameters, the first parameter specifies the pointer to the this object in the function body, and the second parameter acts as an indexed collection, either as an array or as an array of classes.

The number of arguments passed in the call is not fixed, the first parameter specifies the pointer to the this object in the function body, starting with the second argument, each of which is passed to the function sequentially.

If the first argument we pass in is null, the this in the body of the function points to the default host object, which is window in the browser.

1document.getElementById = (function(func) {2      return function(){3         returnfunc.apply (document,arguments);4 }5 }) (document.getElementById);6 varGetId =document.getElementById;7 vardiv = getId (' Div1 ');8alert (;//Output Div1
use apply to fix this point

This call and apply

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: 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.