Alternative implementation _javascript techniques for JScript overloading

Source: Internet
Author: User
JScript is not an object-oriented language, but is based on objects. It does not overload the concept, but there are some techniques to implement overloading in some sense.

First, you define a base class Testa, which overrides the ToString method inherited by object.

Note: The ToString method is used to serialize an object, such as alert (a) equivalent to alert (a.tostring ());


Reference:
function Testa (Name)
{
This. name = name;
this.tostring = function ()
{
return this. Name;
}
}

Next we implement a derived class of Testa class Testb:
Reference:
function Testb ()
{
Testa.apply (this, arguments);
}

Run the following code to see that TESTB has inherited the members of Testa:
<ptml> <pead> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> test Code </title> </pead> <body> <script> function Testa (Na Me) {this. name = name; this.tostring = function () {return this. Name; } function Testb () {testa.apply (this, arguments); var B = new Testb ("Weeping Red Pavilion"); alert (B); Alert (b.tostring ()); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Now we're going to add our own ToString method to Testb, but the problem is that if Testb's ToString is defined, then Testa inherits the ToString that is overwritten. My practice is to save the _testa_tostring of the Testa toString as Testb before rewriting, and then judge the _testa_ by parameters in Testb ToString. ToString or Testb own ToString code


Reference:
function Testb ()
{
Testa.apply (this, arguments);
this._testa_tostring = this.tostring; Save the Testa's ToString.
this.tostring = function (ISTESTB)
{
if (ISTESTB)
{
Return "TESTB's name is" + this. Name;
}
Else
{
return this._testa_tostring (); Here call Testa's toString, i.e. _testa_tostring
}
}
}

The above ToString is not only testb own ToString, also based on the parameters of the original ToString call, the implementation of the overload.

The final code is:


Reference:
function Testa (Name)
{
This. name = name;
this.tostring = function ()
{
return this. Name;
}
}
function Testb ()
{
Testa.apply (this, arguments);
this._testa_tostring = this.tostring; Save the Testa's ToString.
this.tostring = function (ISTESTB)
{
if (ISTESTB)
{
Return "TESTB's name is" + this. Name;
}
Else
{
return this._testa_tostring (); Here call Testa's toString, i.e. _testa_tostring
}
}
}
var B = new Testb ("Weeping Red Pavilion");
alert (B);
Alert (B.tostring (true));

See how it works:



<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Test Code </title>


<body>
<script>
function Testa (Name)
{
This. name = name;
this.tostring = function ()
{
return this. Name;
}
}
function Testb ()
{
Testa.apply (this, arguments);
this._testa_tostring = this.tostring; Save the Testa's ToString.
this.tostring = function (ISTESTB)
{
if (ISTESTB)
{
Return "TESTB's name is" + this. Name;
}
Else
{
return this._testa_tostring (); Here call Testa's toString, i.e. _testa_tostring
}
}
}
var B = new Testb ("Weeping Red Pavilion");
alert (B);
Alert (B.tostring (true));

</script>
</body>


Tip: You can modify some of the code before running

Run Result:


Reference:
alert (B); That is alert (b.tostring ()), the result is "Weeping Red Pavilion"

Alert (B.tostring (TRUE))//That ISTESTB is true, and the result is "TESTB name is the Weeping Red Pavilion"

In fact, this is not a real overload, but first save the original member as a copy, and then rewrite the member, in the new member code based on the parameters to judge whether to invoke the old member code.

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.