The call method of ExtJS and JavaScript

Source: Internet
Author: User
Tags extend

Today, looking at someone else's code, you find that the function you called is not what you think it is. So take a serious look at the code, and then combine the previous JS about call notes. Know the reason for this

The code is as follows Copy Code
Son.superclass.initComponent.call (this);

Call here is to invoke the InitComponent function of the "Son" parent class and pass this in, this is the Son instance object.
Here is a JS code to explain what this call is

The code is as follows Copy Code

function Dog (name) {
THIS.name = name;
}
function Test () {
alert (this.name);
}
var dog = new Dog (' Lil ');
Test.call (dog)//This sentence is equivalent to dog.test (), so test () inside of this is dog, that is, THIS.name is dog.name, that is, pop-up ' Lil '

As shown in the above code, Test.call (dog) is equivalent to Dog.test ();
Here's an example of ExtJS code

The code is as follows Copy Code

Son = Ext.extend (father,{
Initcomponent:function () {
Son.superclass.initComponent.call (this);//This invokes the InitComponent method of the parent class, that is, the Father InitComponent method, and put this, The son instance object is passed as a parameter
},

Test:function () {
Alert (' Test in Son ');
}
}

Father = ext.extend (ext.panel,{
       initcomponent:function () {  &       nbsp;                
Father.superclass.initComponent.call (this);
       this.test (); This is the instance object of son, equivalent to son son = new son (); Son.test ();
      //So the test method of the subclass is invoked instead of the test method of the parent class, which I thought was the test method for invoking the parent class        
      },
      test:function () {
         alert (' Test in Father ');
}
}

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.